Pythonic way to print list items

后端 未结 11 1124
渐次进展
渐次进展 2020-11-22 10:29

I would like to know if there is a better way to print all objects in a Python list than this :

myList = [Person(\"Foo\"), Person(\"Bar\")]
print(\"\\n\".joi         


        
11条回答
  •  故里飘歌
    2020-11-22 11:15

    For Python 2.*:

    If you overload the function __str__() for your Person class, you can omit the part with map(str, ...). Another way for this is creating a function, just like you wrote:

    def write_list(lst):
        for item in lst:
            print str(item) 
    
    ...
    
    write_list(MyList)
    

    There is in Python 3.* the argument sep for the print() function. Take a look at documentation.

提交回复
热议问题