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
#1
def main():
    mycities = ['Cape Girardeau, MO 63780','Columbia, MO  65201',
                'Kansas City, MO 64108','Rolla,MO   65402',
                'Springfield, MO65897','St Joseph, MO64504',
                'St Louis,MO63111', 'Ames,   IA 50010 ', 'Enid, OK 73773',
                'West Palm Beach, FL 33412',
                'International Falls, MN 56649',
                'Frostbite Falls,MN               56650     '
                ]
            
    # Print the table
    for c in mycities:      
        print(c)

main()
Reply
#2
We need some kind of context as to what the problem with your code is. I have no idea what this code is supposed to do, and it looks as if it will work perfectly.
Reply
#3
it should look like this I am confused on how to add and remove spaces as well as left and right adjust individual parts

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 F MN 56649
Frostbite Falls MN 56650
Reply
#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
#5
I was wrapping my brain trying to figure out how to add and remove spaces. I didn't realize you can create list with in a single list and then combine the data later. that make way more sense that what I was trying to do. thanks for all the help.

if I wanted to do this with out adding commas or removing the spaces is this possible?
Reply
#6
or use:
def display_list(listname):
    for element in listname:
        items = element.split(',')
        print(f"{items[0]:20} {items[1]:2} {items[2]:5}")

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'
                ]
    display_list(mycities)

if __name__ == '__main__':
    main()
results:
Output:
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
Reply
#7
(Nov-19-2019, 03:01 AM)Mike2607 Wrote: if I wanted to do this with out adding commas or removing the spaces is this possible?

I think that this assignment more about cleaning string than printing table. As there are strings which don't have spaces between state abbreviation and zip-code one should normalise them (split to chars and construct new string without spaces) and use index (all states have two-letter abbreviation):

for row in mycities: 
    city, state_and_zip = row.split(',') 
    location = ''.join(char for char in state_and_zip.split()) 
    print(f'{city:20}{location[:2]:3}{location[2:]:10}') 
Output:
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do you get Python to print just one value in a list? 357mag 3 968 May-17-2023, 09:52 PM
Last Post: rob101
  store all variable values into list and insert to sql_summary table mg24 3 1,099 Sep-28-2022, 09:13 AM
Last Post: Larz60+
  Print List to Terminal DaveG 2 1,408 Apr-02-2022, 11:25 AM
Last Post: perfringo
  List to table issue robdineen 2 1,435 Nov-07-2021, 09:31 PM
Last Post: robdineen
  Print max numbers in a list jimmoriarty 1 2,120 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 2,921 Sep-04-2020, 01:39 PM
Last Post: ibreeden
  Print variable values from a list of variables xnightwingx 3 2,576 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  Print the number of items in a list on ubuntu terminal buttercup 2 1,899 Jul-24-2020, 01:46 PM
Last Post: ndc85430
  taking input doesnt print as list bntayfur 2 2,059 Jun-04-2020, 02:48 AM
Last Post: bntayfur
  Taking brackets out of list in print statement pythonprogrammer 3 2,341 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