Pythonic way to print list items

后端 未结 11 1006
渐次进展
渐次进展 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:00

    OP's question is: does something like following exists, if not then why

    print(p) for p in myList # doesn't work, OP's intuition
    

    answer is, it does exist which is:

    [p for p in myList] #works perfectly
    

    Basically, use [] for list comprehension and get rid of print to avoiding printing None. To see why print prints None see this

    0 讨论(0)
  • 2020-11-22 11:05

    Assuming you are using Python 3.x:

    print(*myList, sep='\n')
    

    You can get the same behavior on Python 2.x using from __future__ import print_function, as noted by mgilson in comments.

    With the print statement on Python 2.x you will need iteration of some kind, regarding your question about print(p) for p in myList not working, you can just use the following which does the same thing and is still one line:

    for p in myList: print p
    

    For a solution that uses '\n'.join(), I prefer list comprehensions and generators over map() so I would probably use the following:

    print '\n'.join(str(p) for p in myList) 
    
    0 讨论(0)
  • 2020-11-22 11:11

    I use this all the time :

    #!/usr/bin/python
    
    l = [1,2,3,7] 
    print "".join([str(x) for x in l])
    
    0 讨论(0)
  • 2020-11-22 11:11

    I recently made a password generator and although I'm VERY NEW to python, I whipped this up as a way to display all items in a list (with small edits to fit your needs...

        x = 0
        up = 0
        passwordText = ""
        password = []
        userInput = int(input("Enter how many characters you want your password to be: "))
        print("\n\n\n") # spacing
    
        while x <= (userInput - 1): #loops as many times as the user inputs above
                password.extend([choice(groups.characters)]) #adds random character from groups file that has all lower/uppercase letters and all numbers
                x = x+1 #adds 1 to x w/o using x ++1 as I get many errors w/ that
                passwordText = passwordText + password[up]
                up = up+1 # same as x increase
    
    
        print(passwordText)
    

    Like I said, IM VERY NEW to Python and I'm sure this is way to clunky for a expert, but I'm just here for another example

    0 讨论(0)
  • 2020-11-22 11:13

    Expanding @lucasg's answer (inspired by the comment it received):

    To get a formatted list output, you can do something along these lines:

    l = [1,2,5]
    print ", ".join('%02d'%x for x in l)
    
    01, 02, 05
    

    Now the ", " provides the separator (only between items, not at the end) and the formatting string '02d'combined with %x gives a formatted string for each item x - in this case, formatted as an integer with two digits, left-filled with zeros.

    0 讨论(0)
  • 2020-11-22 11:13

    To display each content, I use:

    mylist = ['foo', 'bar']
    indexval = 0
    for i in range(len(mylist)):     
        print(mylist[indexval])
        indexval += 1
    

    Example of using in a function:

    def showAll(listname, startat):
       indexval = startat
       try:
          for i in range(len(mylist)):
             print(mylist[indexval])
             indexval = indexval + 1
       except IndexError:
          print('That index value you gave is out of range.')
    

    Hope I helped.

    0 讨论(0)
提交回复
热议问题