Reputation: 41
I'm new to python so I have been trying to figure out how to print the following list in the format below. I have tried to get them through indexes but it doesn't seem to work out.
So I have a list of tuples that I need to print out in the following format:
1. A1 to B1
2. C1 to D2 , C1 to B2
This is the following list I have that I need to convert to that format.
[['A1', 'B1'], ['C1', 'D2', 'C1', 'B2', 'C1', 'E3', 'C1', 'A3', 'C1', 'F4', 'C1', 'G5', 'C1', 'H6'], ['E1', 'D1', 'E1', 'F2', 'E1', 'D2', 'E1', 'G3', 'E1', 'H4']]
Upvotes: 1
Views: 138
Reputation: 8709
Here is one way of doing this:
>>> l = [['A1', 'B1'], ['C1', 'D2', 'C1', 'B2', 'C1', 'E3', 'C1', 'A3', 'C1', 'F4', 'C1', 'G5', 'C1', 'H6'], ['E1', 'D1', 'E1', 'F2', 'E1', 'D2', 'E1', 'G3', 'E1', 'H4']]
>>>
>>>
>>> for i,m in enumerate(l):
... print '%s. %s' %(i+1, ' , '.join([' to '.join(n) for n in zip(m[::2], m[1::2])]))
...
1. A1 to B1
2. C1 to D2 , C1 to B2 , C1 to E3 , C1 to A3 , C1 to F4 , C1 to G5 , C1 to H6
3. E1 to D1 , E1 to F2 , E1 to D2 , E1 to G3 , E1 to H4
Upvotes: 0
Reputation: 52133
>>> tuples = [['A1', 'B1'],
... ['C1', 'D2', 'C1', 'B2', 'C1', 'E3', 'C1', 'C2'],
... ['E1', 'D1', 'E1', 'F2']]
>>> for i, sequence in enumerate(tuples, 1):
... it = iter(sequence)
... print(i, ', '.join('{} to {}'.format(x, y) for x, y in zip(it, it))
1 A1 to B1
2 C1 to D2, C1 to B2, C1 to E3, C1 to C2
3 E1 to D1, E1 to F2
You must replace zip()
with itertools.izip_longest()
if number of elements in the list is odd:
>>> from itertools import izip_longest
>>> tuples = [['A1', 'B1', 'T1'],
['C1', 'D2', 'C1', 'B2', 'C1', 'E3', 'C1', 'C2', 'Z1'],
['E1', 'D1', 'E1', 'F2']]
>>> for i, sequence in enumerate(tuples, 1):
... it = iter(sequence)
... print(i, ', '.join('{} to {}'.format(x, y)
... for x, y in izip_longest(it, it, fillvalue='X'))
1 A1 to B1, T1 to X
2 C1 to D2, C1 to B2, C1 to E3, C1 to C2, Z1 to X
3 E1 to D1, E1 to F2
Upvotes: 5
Reputation: 28983
start_list = [['A1', 'B1'], ['C1', 'D2', 'C1', 'B2', 'C1', 'E3', 'C1', 'A3',
'C1', 'F4', 'C1', 'G5', 'C1', 'H6'], ['E1', 'D1', 'E1', 'F2', 'E1', 'D2',
'E1', 'G3', 'E1', 'H4']]
for line, sublist in enumerate(start_list):
pairs_list = [ (a + " to " + b) for (a, b) in zip(sublist, sublist[1:]) ]
print str(line)+'.', ', '.join(pairs_list)
OK, enough dense advanced code only answers.
start_list = [['A1', 'B1'], ['C1', 'D2', 'C1', 'B2', 'C1', 'E3', 'C1', 'A3',
'C1', 'F4', 'C1', 'G5', 'C1', 'H6'], ['E1', 'D1', 'E1', 'F2', 'E1', 'D2',
'E1', 'G3', 'E1', 'H4']]
#Loop over the inner lists, with a counter
for line_num, sublist in enumerate(start_list):
# Build up a string to print with a line number and dot
output = str(line_num) + '. '
# Count through the list of pairs, step 2
# skipping every other one
for i in range(0, len(sublist), 2):
# take the current and next sublist item, into the output string
# and add a trailing comma and space, to give the form
# "A to B, "
output += sublist[i] + " to " + sublist[i+1] + ", "
# remove trailing comma after last pair in the line
output = output.rstrip(', ')
print output
e.g.
0. A1 to B1
1. C1 to D2, C1 to B2, C1 to E3, C1 to A3, C1 to F4, C1 to G5, C1 to H6
2. E1 to D1, E1 to F2, E1 to D2, E1 to G3, E1 to H4
Upvotes: 2
Reputation: 8335
Using loop and list comprehension
This would also work for lists with odd elements
Code:
lst=[['A1', 'B1'], ['C1', 'D2', 'C1', 'B2', 'C1', 'E3', 'C1', 'A3', 'C1', 'F4', 'C1', 'G5', 'C1', 'H6','H5'], ['E1', 'D1', 'E1', 'F2', 'E1', 'D2', 'E1', 'G3', 'E1', 'H4']]
for count,line in enumerate(lst ) :
print str(count)+" , ".join([" %s to %s" %(tuple((line[i:i+2]))) if i+1 <len(line) else "Single king %s"%(line[i]) for i in range(0,len(line),2)])
Output:
0 A1 to B1
1 C1 to D2 , C1 to B2 , C1 to E3 , C1 to A3 , C1 to F4 , C1 to G5 , C1 to H6 , Single king H5
2 E1 to D1 , E1 to F2 , E1 to D2 , E1 to G3 , E1 to H4
Upvotes: 0
Reputation: 2776
The easier way to do this for you will be to iterate over the inner lists using an index and an offset.
Something like this (not tested):
for j, inner in enumerate(list_of_lists):
print(j, '.', sep='', end=' ')
for i in range(0, len(inner)-1, 2):
print(inner[i], 'to', inner[i+1], ',', end='')
Upvotes: 0