0

わかりました。CSV をインポートしています。次に、反復されたすべての値row[0]を の反復された値と組み合わせたいと思いますrow[1]

csvfile = csv.reader(open(filename, 'rb'), delimiter=',')

for row in csvfile:
    row[0] + row[1]

そのように、同じ行になくても、row[0]のすべての値を のすべての値と結合したいことを除いて。row[1]

したがって、2 つの列があるとしましょう。1 つの列は次のとおりです。

asparagus
beets
corn
cucumbers
tomatoes

もう1つは次のとおりです。

pineapple
orange
apple
raspberry
blueberry

アスパラガスをリスト2.のすべてと組み合わせたい、つまり:

asparagus pineapple
asparagus orange
asparagus apple
asparagus raspberry
asparagus blueberry

そしてビーツ・パイナップルなどに

4

2 に答える 2

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')]
于 2012-07-02T18:27:18.893 に答える
0
csvfile = csv.reader(open(filename, 'rb'), delimiter=',')

combinations = []

for rowa in csvfile:
    for rowb in csvfile:
        combinations.append(rowa[0] + ' ' + rowb[1])
于 2012-07-02T18:21:06.603 に答える