Python Forum
Trying to print an uneven list to a even table
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to print an uneven list to a even table
#4
Doing that will be a bit more complicated than the code that you currently have. I'll write and explain the code for you.
def main():
    mycities = ['Cape Girardeau,MO,63780','Columbia,MO,65201',
                'Kansas City,MO,64108','Rolla,MO,65402',
                'Springfield,MO,65897','St Joseph,MO,64504',
                'St Louis,MO,63111', 'Ames,IA,50010', 'Enid,OK,73773',
                'West Palm Beach,FL,33412',
                'International Falls,MN,56649',
                'Frostbite Falls,MN,56650'
                ]
    formattedCities = []
    comparisonCities = []
    for city in mycities:
        formattedCities.append(city.split(',')) #Each city is put in a list, and in that list, everything that was seperated by a comma, got split into different strings
        comparisonCities.append(city.split(',')[0]) #The same thing here, but instead of keeping the list, the code just grabs the first string of the list, or the name part.
    longestString = len(max(mycities)) #This is a number representing how many character long, the longest name is
    # Print the table
    for c in formattedCities:   
        print(c[0] + ' '*(longestString - len(c[0]) + 1) + '%s %s' %(c[1], c[2])) #The "c[0]" prints the first elements of the script, the name, then the spaces are calculated using the longest character length compared to the length of the name currently being printed. The plus one is for to make sure a space is printed when printing the longest name. Finally, the last two elements of the list, the state abbreviation and zip code, are printed separated by space
 
main()
I hope you understand everything.
Reply


Messages In This Thread
RE: Trying to print an uneven list to a even table - by SheeppOSU - Nov-19-2019, 02:51 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How do you get Python to print just one value in a list? 357mag 3 1,085 May-17-2023, 09:52 PM
Last Post: rob101
  store all variable values into list and insert to sql_summary table mg24 3 1,205 Sep-28-2022, 09:13 AM
Last Post: Larz60+
  Print List to Terminal DaveG 2 1,492 Apr-02-2022, 11:25 AM
Last Post: perfringo
  List to table issue robdineen 2 1,511 Nov-07-2021, 09:31 PM
Last Post: robdineen
  Print max numbers in a list jimmoriarty 1 2,193 Sep-25-2020, 07:29 AM
Last Post: DPaul
  Fetch Oracle DB rows & print it in HTML file with table's col headers in table format tssr_2001 1 3,048 Sep-04-2020, 01:39 PM
Last Post: ibreeden
  Print variable values from a list of variables xnightwingx 3 2,685 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  Print the number of items in a list on ubuntu terminal buttercup 2 1,986 Jul-24-2020, 01:46 PM
Last Post: ndc85430
  taking input doesnt print as list bntayfur 2 2,174 Jun-04-2020, 02:48 AM
Last Post: bntayfur
  Taking brackets out of list in print statement pythonprogrammer 3 2,464 Apr-13-2020, 12:25 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020