0

I am needing to convert a list of lists of strings into a three column table where the first column is 1 space longer than the longest string. I have figured out how to identify the longest string and how long it is, but getting the table to form has been quite tricky. Here is the program with the lists in it and it shows you that the longest one is 26 characters long.

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']]

    col_width = max(len(item) for sub in mycities for item in sub)
    print(col_width)


main()

Now I am just needing to get it to print off like this:

Cape Girardeau      MO 63780
Columbia            MO 65201
Kansas City         MO 64108
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
Wes Doyle
  • 2,199
  • 3
  • 17
  • 32
  • You have a good start going! Now try using your "col_width" number to pad the ends of the strings – Alexander Nov 18 '18 at 04:07
  • I'm truly struggling with this. I am taking this as an on-line course, and do not understand what you mean by "pad the ends of the strings". Do you have any examples of what that is? – Stephen Dangel Nov 18 '18 at 04:15
  • Well you want each string to be `col_width` wide. So for any string that's only `len(string)` characters wide, you'll want to add `col_width - len(string)` spaces to the end – Alexander Nov 18 '18 at 18:19

3 Answers3

0

"String name".ljust(26) will add spaces to the end of your string. For example,

Ames.ljust(26) will result in 'Ames (22 spaces here)', and then the next column will print after. If you are not sure what the longest city will be, you could replace the 26 with len(cities[-1]) after ordering the cities in a list by length. To do this, you can do sortedCities = sorted(cityListVariable, key=len)

0

You're off to the right start -- as an example given the specific structure to the lists you have, you can use the col_width you calculated to determine the number of spaces you'd need after the name of each city to append to the end of each city name:

for city in mycities:
    # append the string with the number of spaces required
    city_padded = city[0] + " " + " "*(col_width-len(city[0]))
    print(city_padded + city[1] + " " + city[2])

Given your example, this will produce:

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

Note in the original version of your question, you are missing commas in your sublists in your mycities variable, for which I've added in an edit.

As a side note, it is convention in Python that words be separated by underscores in variable names for readability, so you might rename mycities to my_cities.

pep8 ref: (https://www.python.org/dev/peps/pep-0008/#function-and-variable-names)

Wes Doyle
  • 2,199
  • 3
  • 17
  • 32
  • Is it possible to change the source from the list of strings to a .txt file with the info printed as it would normally appear in a list. The file would look like this: Rolla, MO 65402 Springfield, MO 65897 etc, and I would need for the output to be the same. – Stephen Dangel Nov 24 '18 at 03:47
  • yes, you can iterate through a text file to parse the addresses. https://stackoverflow.com/questions/17949508/python-read-all-text-file-lines-in-loop – Wes Doyle Nov 24 '18 at 04:06
0
def main():
    cities = ['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', 
              'Charlotte, NC 28241', 'Upper Marlboro, MD 20774', 'Camdenton, MO 65020', 'San Fransisco, CA 94016']   #create list of information

    for x in cities:
        col = x.split(",")
        if(len(col) == 2):
            city = col[0].strip()
            temp = col[1].strip()
        else:
            city = x[:15].strip()
            temp = c[15:].strip()

        state = temp[:2]
        zipCode = int(temp[-5::])
        print("%-20s\t%s\t%d"%(city, state, zipCode)) 
main()
Das_Geek
  • 2,775
  • 7
  • 20
  • 26