So I need to convert a list of lists:
[[1, 3, 5, 7, 9, 11], [2, 4, 6, 8], [1, 2, 3, 4, 10, 20, 30, 40, 50], [-1, -2, -3, -4, -5, -6]]
into separate integer sequences:
Sequence # 1
1 3 5 7 9 11
Sequence # 2
2 4 6 8
Sequence # 3
1 2 3 4 10 20 30 40 50
Sequence # 4
-1 -2 -3 -4 -5 -6
in a function. So far, I have
def printIntegerSequences(integerSequences):
for i in len(integerSequences):
print("Sequence #", i 1,' '.join(str(e) for e in integerSequences[i]))
where integerSequence is the list presented above, but I keep getting this error:
TypeError: 'int' object is not iterable
What am I doing wrong?
CodePudding user response:
Your code:
def printIntegerSequences(integerSequences):
for i in len(integerSequences):
print("Sequence #", i 1,' '.join(str(e) for e in integerSequences[i]))
should instead be:
def printIntegerSequences(integerSequences):
for i in range(len(integerSequences)):
print("Sequence #", i 1,' '.join(str(e) for e in integerSequences[i]))
or even better yet:
def printIntegerSequences(integerSequences):
for i, seq in enumerate(integerSequences, start=1):
print("Sequence #", i,' '.join(str(e) for e in seq))
Relevant documentation:
CodePudding user response:
This seemed to work for me
a = [[1, 3, 5, 7, 9, 11], [2, 4, 6, 8], [1, 2, 3, 4, 10, 20, 30, 40, 50], [-1, -2, -3, -4, -5, -6]]
def printIntegerSequences(integerSequences):
for i in range(len(integerSequences)):
print("Sequence #", i 1,' '.join(str(e) for e in integerSequences[i]))
result = printIntegerSequences(a)