0

I was given this project to work on in class and the one major issue that I'm having trouble with is getting the first indexes of each list from a text file separated into their own category and creating an indefinite list of said category. The first indexes are supposed to be their own data set for the user to choose from, and this will be applied regardless of how many lists there will be.

An example of this would be taking a text file such as this:

Silver Lake, 32, 35, 25
Pasadena, 55, 75, 20
Los Angeles, 3500, 3000, 500

and being able to categorize it as:

The cities in our available data set:
A - City 1
B - City 2
C - City 3
Please select your status: A

Where the data set could be expanded with the options selection input automatically adjusting to it I've only managed to get this far:

def title():
    print("City Population Projector")
    print("Select a city from the list: ")

def list():
    with open("population.txt", "r") as projections:
        cities = projections.read()
        print(cities)


if __name__ == "__main__":
    title()
    list()
Sandra
  • 11
  • 1
  • two options: 1: Use Pandas to read it. Column 1 will be city so it will help you. If you do col1.values.tolist(), you will get a list of all the cities. Then you can print it out easily. Option 2: You can read each line, split on comma separator, use index 0 for cities and store them into a dictionary (assuming each row is unique), then iterate over the dict to print the cities. This looks like a homework or exercise. I strongly recommend you to do look at online materials to do this project instead of asking SO members to do this for you – Joe Ferndz Mar 05 '21 at 00:19
  • I will check back again later and if you are still struggling and no one has responded, I will share code. In the meanwhile, let me help you with some StackOverflow links to get you ways to solve this. – Joe Ferndz Mar 05 '21 at 00:21
  • reading text files with comma separator: [1](https://realpython.com/python-csv/), [2](https://stackoverflow.com/questions/3277503/how-to-read-a-file-line-by-line-into-a-list). The second one will help you. It is from SO – Joe Ferndz Mar 05 '21 at 00:25
  • print data from a list: [1](https://www.geeksforgeeks.org/print-lists-in-python-4-different-ways/), [2](https://www.tutorialspoint.com/python/python_lists.htm), [3](https://stackoverflow.com/questions/15769246/pythonic-way-to-print-list-items), [4](https://stackoverflow.com/questions/51989352/python-how-to-print-specific-items-from-lists-in-lists), [5](https://stackoverflow.com/questions/55001800/printing-a-specific-item-in-a-python-list) – Joe Ferndz Mar 05 '21 at 00:31
  • check for value in a list [1](https://www.geeksforgeeks.org/check-if-element-exists-in-list-in-python/), [2](https://stackoverflow.com/questions/7571635/fastest-way-to-check-if-a-value-exists-in-a-list), [3](https://stackoverflow.com/questions/11251709/check-if-item-is-in-an-array-list) – Joe Ferndz Mar 05 '21 at 00:32
  • I am using the string before the links as search text on google to look for information. Hope these will help you complete the assignment – Joe Ferndz Mar 05 '21 at 00:32
  • store data into a dictionary [1](http://www.compciv.org/guides/python/fundamentals/dictionaries-overview/), [2](https://www.geeksforgeeks.org/python-dictionary/), [3](https://stackoverflow.com/questions/21767245/how-to-store-a-list-as-a-dictionary-python) – Joe Ferndz Mar 05 '21 at 00:34
  • hi, so thank you for the responses! I just want to let you know that this was an in-class thing and I'm just solving it to learn more. It's not an assignment that's due-- just an in class quiz I want to finish myself in order to understand the material more! I've done extensive googling and searching online but because my class is limited to using David Kay's formula, I've found python to be a little more difficult-- I taught myself the basics through the book Automate the Boring Stuff while taking a C++ course at the same time so it's not so easy for me to understand all of this right now. – Sandra Mar 05 '21 at 03:03
  • Asking for an answer to a project that I have been assigned to work on isn't my approach to solving solutions like this since it's a little against my work ethic(nor will I be able to make any points up for this) and I'm not looking for anyone to actually do the whole code for me as I've left a lot of other large components included in the assignment out, but regardless, thank you for all the helpful material you've given me! – Sandra Mar 05 '21 at 03:08
  • oh, also one more thing! this info that I'm using isn't in a list that's already been coded/is supposed to be coded in list form, it's information stored in a .txt file which is why I'm having trouble because it's just a text file of the first example I gave. – Sandra Mar 05 '21 at 03:21

1 Answers1

0

I’m not sure what the rules are here for assignments, but here is a little guidance.

I assume by ‘first indexes’ you mean the first column. I’m not sure what you are referring to being expanded, but I am assuming you are referring to presenting the user with more field values for the selected city.

Google the following:

  • split function for splitting strings. You can use this to split a string into a list of values. The first value would be the city name.
  • format function for formatting output. This would help you present the options to the user.
  • reading one line at a time. This might simplify your life.
  • dictionaries. This could be used to hold on to data you want to use later when the user selects a city.

Is this an assignment for a python class? What subjects have you covered so far? For much of this, a quick intro to Python tutorial would be a big help.

David Moreau
  • 148
  • 8
  • Yep, its an intro to python class! what we're doing is taking information from a text file like the one in the example above and we're supposed to be outputting the projections for each city. The computation will be based on the total sum of births deaths and people moving into the city (didn't mention it because it's not so important to the main question) and trust me I've been googling every variation of the question I have to no avail. Thank you for your response thought!! – Sandra Mar 05 '21 at 00:03