打印嵌套列表的格式为" XX到XX"

时间:2015-09-24 05:54:44

标签: python list

我是python的新手,所以我一直试图弄清楚如何以下面的格式打印以下列表。我试图让他们通过索引,但它似乎没有成功。

所以我有一个元组列表,我需要以下列格式打印出来:

1. A1 to B1
2. C1 to D2 , C1 to B2 

以下是我需要转换为该格式的列表。

[['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']]

5 个答案:

答案 0 :(得分:5)

>>> 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

如果列表中的元素数量为奇数,则必须将zip()替换为itertools.izip_longest()

>>> 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

答案 1 :(得分:2)

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)

好的,足够密集的高级代码只能回答。

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

答案 2 :(得分:0)

更简单的方法是使用索引和偏移量迭代内部列表。

像这样(未经测试):

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='')

答案 3 :(得分:0)

使用循环和列表理解

这也适用于具有奇数元素的列表

<强>代码:

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)])

<强>输出:

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

答案 4 :(得分:0)

以下是执行此操作的一种方法:

>>> 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