Python Forum
Trying to print an uneven list to a even table - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Trying to print an uneven list to a even table (/thread-22591.html)



Trying to print an uneven list to a even table - Mike2607 - Nov-19-2019

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()



RE: Trying to print an uneven list to a even table - SheeppOSU - Nov-19-2019

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.


RE: Trying to print an uneven list to a even table - Mike2607 - Nov-19-2019

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



RE: Trying to print an uneven list to a even table - SheeppOSU - Nov-19-2019

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.


RE: Trying to print an uneven list to a even table - Mike2607 - Nov-19-2019

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?


RE: Trying to print an uneven list to a even table - Larz60+ - Nov-19-2019

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



RE: Trying to print an uneven list to a even table - perfringo - Nov-19-2019

(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