我有一组垂直值
2,4
4,7
5,8
9
我希望在垂直方向上对每两个值进行二进制组合,例如2和4,2和5等。同样适用于第二个
2 4
2 5
4 5
4 7
4 8
.
.
.
好吧看起来很复杂,所以我试着让它变得更容易 我将我的数据转换为地平线
我有 2,4,5 4,7,8,9我想要第一行的二进制组合
2 4
2 5
4 5
和第二行的二进制组合
4 7
4 8
4 9
7 8
7 9
8 9
答案 0 :(得分:0)
我想我明白了。试试这段代码:
<强> test.py 强>
#!/bin/python
# put items side by side
# take first item and put the next item besides it
# if there are any more items after the next, put that item besides the first item
# if there are no more items after the next, switch to the next item in the list
# repeat
def two_items_side_by_side(mylist):
list_len = len(mylist)
for i in range(list_len):
for j in range(i+1, list_len):
print '{} {}'.format(mylist[i], mylist[j])
# -------------------------------------------------------------------
# these are two lists
list1 = [2, 4, 5]
list2 = [4, 7, 8, 9]
two_items_side_by_side(list1)
two_items_side_by_side(list2)
当你运行它时,你的结果将如下所示:
<强>结果强>
python test.py
2 4
2 5
4 5
4 7
4 8
4 9
7 8
7 9
8 9
如果你的测试用例是一个字符串,每行包含这样的逗号分隔文本,你可以使用test2.py作为例子
2,4
4,7
5,8
,9
<强> test2.py 强>
#!/bin/python
# put items side by side
# take first item and put the next item besides it
# if there are any more items after the next, put that item besides the first item
# if there are no more items after the next, switch to the next item in the list
# repeat
def two_items_side_by_side(mylist):
list_len = len(mylist)
for i in range(list_len):
for j in range(i+1, list_len):
print '{} {}'.format(mylist[i], mylist[j])
# -------------------------------------------------------------------
# process the data and store them into a list
# then do the same work as we did in the first example
def convert_data_into_lists():
lines = data.split('\n')
for line in lines:
# ignore empty lines
if len(line.strip()) < 1:
continue
# split by comma and ignore if we don't get 2 or more values
items = line.split(',')
if len(items) < 2:
continue
# put first item in list1 and second item in list2
if len(items[0].strip()) > 0: list1.append(items[0].strip())
if len(items[1].strip()) > 0: list2.append(items[1].strip())
# -------------------------------------------------------------------
# this is my string
data = """
2,4
4,7
5,8
,9
"""
list1 = []
list2 = []
convert_data_into_lists()
two_items_side_by_side(list1)
two_items_side_by_side(list2)
<强>结果强>
python test2.py
2 4
2 5
4 5
4 7
4 8
4 9
7 8
7 9
8 9
有更优雅的方法来编写此代码。我的编写方式可以帮助您理解代码并自行尝试。
根据需求的变化,数据位于文本文件中。我们将采取三个测试用例(参见结果)。为了满足要求,我将使用我在test2.py
中使用的相同代码。我们将在文本文件中创建一个列表,动态地包含与文本文件中的列一样多的列表,而不是为每个列创建单独的列表。
<强>代码强>
#!/bin/python
# put items side by side
# take first item and put the next item besides it
# if there are any more items after the next, put that item besides the first item
# if there are no more items after the next, switch to the next item in the list
# repeat
def two_items_side_by_side(mylist):
list_len = len(mylist)
for i in range(list_len):
for j in range(i+1, list_len):
print '{} {}'.format(mylist[i], mylist[j])
# -------------------------------------------------------------------
# process the data and store them into a list
# then do the same work as we did in the first example
def convert_data_into_lists():
with open(data) as f:
lines = f.readlines()
for line in lines:
# ignore empty lines
if len(line.strip()) < 1:
continue
# split by comma and ignore if we don't get 2 or more values
items = line.split(',')
counter = 0
for item in items:
if len(mylist) < counter + 1:
mylist.append([])
if len(item.strip()) > 0:
mylist[counter].append(item.strip())
counter += 1
# -------------------------------------------------------------------
# this is my string
data = 'test.txt'
mylist = []
convert_data_into_lists()
for individual_list in mylist:
two_items_side_by_side(individual_list)
<强>结果强>
案例1
Data:
2,4
4,7
5,8
,9
Results:
2 4
2 5
4 5
4 7
4 8
4 9
7 8
7 9
8 9
案例2
Data:
2,4
4,7
5,8
6,9
Results:
2 4
2 5
2 6
4 5
4 6
5 6
4 7
4 8
4 9
7 8
7 9
8 9
案例3
Data:
2,4,10
4,7,11
5,8,
,9,13
Results:
2 4
2 5
2 6
4 5
4 6
5 6
4 7
4 8
4 9
7 8
7 9
8 9
10 11
10 13
11 13
答案 1 :(得分:0)
如果您有两个收藏品中的值存储,请使用列表解析
from itertools import izip_longest
a = [(1,'a'),(2,'b'),(3,None)]
b,c = izip_longest(*a)
d = [(i, j) for i in b if i for j in c if j]
答案 2 :(得分:0)
修改
通过修改上面的代码只使用单个参数,我们可以读取csv文件的内容并(使用某种形式的分隔符)提供整个数据集的组合。只需调用total_zipper()并替换&#39; filename.txt&#39;用你的文件名。
def total_zipper():
def zipper(a):
lst = []
for i in range(1,len(a)+1):
lst+=zip(a,a[i:])
return sorted(lst)
f = open('filename.txt','r')
return [zipper(line) for line in f]
这会将所有行视为可迭代(字符串)。为了使readline()起作用,我相信你需要在txt中每行末尾的return语句。有关详情,请参阅input/output page for Python。
这是我能提出的最短版本。您可以使用内置的zip()
功能。这与列表切片结合使用时,会以pythonic方式按所需顺序对值进行配对。
def zipper(a,b):
lst = []
for i in range(1,len(b)+1):
lst+=zip(a,b[i:])
return sorted(lst)
现在只需在各行数据上调用zipper即可。
>>> a = [2,4,5]
>>> b = [4,7,8,9]
>>> print(zipper(a,a))
[(2, 4), (2, 5), (4, 5)]
>>> print(zipper(b,b))
[(4, 7), (4, 8), (4, 9), (7, 8), (7, 9), (8, 9)]
作为旁注,我尝试使用列表理解来缩短代码。例如,以下代码与zipper(a):
的功能相同def zipper(a):
return list(zip(a,a[i:]) for i in range(1,len(a)+1))
然而,使用zip()在Python 3中返回生成器对象,结果不是&#34; clean&#34;作为上述版本的输出。我必须在zipper输出的列表中的每个生成器对象上使用next()
才能获得相同的输出,但这是一个繁琐的过程。有人建议让列表理解工作吗?