好的,我正在导入CSV,然后我想将row[0]
中的所有迭代值与row[1]
中的迭代值合并。
csvfile = csv.reader(open(filename, 'rb'), delimiter=',')
for row in csvfile:
row[0] + row[1]
就像那样,除了我希望row[0]
的所有值与row[1]
的所有值组合,即使它们不在同一行。
所以我想说我有两列,一列是:
asparagus
beets
corn
cucumbers
tomatoes
另一个是:
pineapple
orange
apple
raspberry
blueberry
我希望芦笋与清单2中的所有清单相结合,即:
asparagus pineapple
asparagus orange
asparagus apple
asparagus raspberry
asparagus blueberry
然后是甜菜菠萝等
答案 0 :(得分:2)
In [1]: import csv
In [2]: from itertools import product
In [3]: csvfile = csv.reader(open('filename', 'rb'), delimiter=',')
In [4]: list(product(*zip(*list(csvfile))))
Out[4]:
[('asparagus', 'pineapple'),
('asparagus', 'orange'),
('asparagus', 'apple'),
('asparagus', 'raspberry'),
('asparagus', 'blueberry'),
('beets', 'pineapple'),
('beets', 'orange'),
('beets', 'apple'),
('beets', 'raspberry'),
('beets', 'blueberry'),
('corn', 'pineapple'),
('corn', 'orange'),
('corn', 'apple'),
('corn', 'raspberry'),
('corn', 'blueberry'),
('cucumbers', 'pineapple'),
('cucumbers', 'orange'),
('cucumbers', 'apple'),
('cucumbers', 'raspberry'),
('cucumbers', 'blueberry'),
('tomatoes', 'pineapple'),
('tomatoes', 'orange'),
('tomatoes', 'apple'),
('tomatoes', 'raspberry'),
('tomatoes', 'blueberry')]
答案 1 :(得分:0)
csvfile = csv.reader(open(filename, 'rb'), delimiter=',')
combinations = []
for rowa in csvfile:
for rowb in csvfile:
combinations.append(rowa[0] + ' ' + rowb[1])