Here is a problem I seem to be stuck on. I am trying to get python to print every combination of two lists.
# Test use case: This does what I expect:
lista = ['1', '2', '3', '4']
listb = ['a', 'b', 'c']
for x in lista:
for y in listb:
print x, y
##
## result summary -
## 1 a
## 1 b
## 1 c
## 2 a
## 2 b
## 2 c
## 3 a
## 3 b
## 3 c
## 4 a
## 4 b
## 4 c
# actual use case:
# test files:
## file_a contents =
## this is group 1:
## this is group 2:
## this is group 3:
##
##
## file_b contents =
## red,1,1,1
## blue,2,2,2
## green,3,3,3
## yellow,4,4,4
##
import csv
with open('file_b', 'r') as f:
reader = csv.reader(f)
with open('file_a', 'r') as template:
for line in template:
for row in reader:
print line, row[0]
The result starts out like (what I want) above, but it only iterates through the first line of file_a and stops.
Any suggestions? Thoughts on why the behavior is different from case A to B?
I've been trying out itertools also, but it treats each character as an an individual string.
Thanks!!
readeris an iterator here, so it'll be exhausted after the first iteration. Writef.seek(0);reader = csv.reader(f)between the two loops.