Reputation: 15
I have a file with a 3D list containing states, the number of counties in each state, and how much it rained in each county. For example
[['Georgia',['county1', 3], ['county2', 1], ['county3', 2]], ['Florida' , ['county1', 0], ['county2', 3]]]
I have data like this for each state, and each state has a different number of counties. I have to take the input from a user on which state they want to get data from, but the states are in no particular order in my list. How do I access the list in my program so that I can extract the data for the state that the user wants?
I have made the 3D list but really do not know how to get the data for the state that the user inputs without making 50 while loops
I want the user to be able to input a state and have the program go through the list and find the state that the user wants information for so that it can print that information.
Ex. Input a state: Florida
In Florida, there were 0 inches of rain in county1 and 3 inches of rain in county2
Edit: I cannot use a dictionary for this assignment
Upvotes: 0
Views: 87
Reputation: 5774
It would be highly advisable to use a dictionary instead considering you have natural keys (the state names). In its current implementation one solution would be to iterate over the data until the first item in the list matches the state. For instance:
data = [['Georgia',['county1', 3], ['county2', 1], ['county3', 2]], ['Florida' , ['county1', 0], ['county2', 3]]]
def get_subset(full_data, some_input):
for item in full_data:
if item[0].lower() == some_input.lower(): # case insensitive
return item
return None
florida = get_subset(data, 'Florida')
print(florida)
Yields:
['Florida', ['county1', 0], ['county2', 3]]
From here you should be able to figure out how to interpret the data into a printable format to suit your needs!
Upvotes: 1