简体   繁体   English

在python中打印列表输出的最佳方法

[英]Best way to print list output in python

I have a list and a list of list like this 我有一个list和这样的list of list

>>> list2 = [["1","2","3","4"],["5","6","7","8"],["9","10","11","12"]]
>>> list1 = ["a","b","c"]

I zipped the above two list so that i can match their value index by index. 我压缩了上面的两个列表,以便我可以通过索引匹配它们的值索引。

>>> mylist = zip(list1,list2)
>>> mylist
[('a', ['1', '2', '3', '4']), ('b', ['5', '6', '7', '8']), ('c', ['9', '10', '11', '12'])]

Now I tried to print the output of above mylist using 现在我尝试使用打印上面的mylist的输出

>>> for item in mylist:
...     print item[0]
...     print "---".join(item[1])
...

It resulted in this output which is my desired output . 它产生了这个输出,这是我desired output

a
1---2---3---4
b
5---6---7---8
c
9---10---11---12

Now, my question is there a more cleaner and better way to achieve my desired output or this is the best(short and more readable) possible way. 现在,我的问题是有一种更cleaner and better方法来实现我想要的输出,或者这是best(short and more readable)可能的方式。

Well, you could avoid some temporary variables and use a nicer loop: 好吧,你可以避免一些临时变量并使用更好的循环:

for label, vals in zip(list1, list2):
    print label
    print '---'.join(vals)

I don't think you're going to get anything fundamentally "better," though. 不过,我认为你不会从根本上获得任何“更好”的东西。

The following for loop will combine both the print and join operations into one line. 以下for循环将打印和连接操作合并为一行。

 for item in zip(list1,list2):
     print '{0}\n{1}'.format(item[0],'---'.join(item[1]))

It may not be quite as readable as a full loop solution, but the following is still readable and shorter: 它可能不像完整循环解决方案那样可读,但以下内容仍然可读且更短:

>>> zipped = zip(list1, list2) 
>>> print '\n'.join(label + '\n' + '---'.join(vals) for label, vals in zipped)
a
1---2---3---4
b
5---6---7---8
c
9---10---11---12

Here's another way to achieve the result. 这是实现结果的另一种方式。 It's shorter, but I'm not sure it's more readable: 它更短,但我不确定它更具可读性:

print '\n'.join([x1 + '\n' + '---'.join(x2) for x1,x2 in zip(list1,list2)])

What you might consider clean, but I do not, is that your the rest of your program needs to now the structure of your data and how to print it. 您可能认为干净,但我不认为,您的其余程序现在需要您的数据结构以及如何打印它。 IMHO that should be contained in the class of the data, so you can just do print mylist and get the desired result. 恕我直言,应该包含在数据类中,这样你就可以print mylist并获得所需的结果。

If you combine that with mgilson's suggestion to use a dictionary ( I would even suggest an OrderedDict) I would do something like this: 如果你把它与mgilson的建议结合起来使用字典(我甚至建议使用OrderedDict)我会做这样的事情:

from collections import OrderedDict

class MyList(list):
    def __init__(self, *args):
        list.__init__(self, list(args))

    def __str__(self):
        return '---'.join(self)

class MyDict(OrderedDict):
    def __str__(self):
        ret_val = []
        for k, v in self.iteritems():
            ret_val.extend((k, str(v)))
        return '\n'.join(ret_val)

mydata = MyDict([
    ('a', MyList("1","2","3","4")),
    ('b', MyList("5","6","7","8")),
    ('c', MyList("9","10","11","12")),
])

print mydata

without requiring the rest of the program needing to know the details of printing this data. 不需要程序的其余部分需要知道打印此数据的细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM