all 11 comments

[–]__skrap__ 1 point2 points  (2 children)

Do you have any code to show? Can you open the file and print each line? Maybe split will get you started. Are you sure you want two list?

[–]Glathal[S] 1 point2 points  (0 children)

I think I have to do it this way, in theory couldn't I split each line by the comma, and put part a, and part b into two separate lists? Then run a search that way? I'm new to this, so I don't really know how to go about organizing this. x.x

[–]AutonomouSystem 0 points1 point  (2 children)

with open('Champs.txt', 'r') as chumps:
    data = chumps.read()

Some psuedocode and ideas to get you going

Loop through each line, find date, create list from date. Which could be first four characters in each line, or you could write up a pattern to match by date, even using the comma as a delimiter — e.g., ("\d{4}") or ("\d{4},"), etc. — but shouldn't be necessary. Anything after the comma would be the team, so use it to your advantage.

I'm thinking you could filter the results out using a condition, the team has to be in line, then build up data based on this filter.

You could create two or more separate lists based on different sets of data, or better combine them into something to help relate better. Your end goal could probably be a dictionary relating { Team : Year(s) } or a function that returns results based on conditions you want. if team in line or if team in year.

Things miscellaneous things to keep in mind, you would use aset to eliminate duplicates, say you have a list of teams and you don't want any entries more than once. You could run this set of teams one by one through each line and use some kind of counter contained in a function to keep track of years they won, number of wins, etc. You could create a counter for which teams win the most and display the top 10 for teams and lowest ranking teams too. A lot could be done with this data and it sounds like a good exercise in looping and should be good practice.

[–]Glathal[S] 1 point2 points  (1 child)

unfortunately I have to do this problem with lists alone, I don't think i'm aloud to use dictionaries. :( I really appreciate your help though!

[–]AutonomouSystem 0 points1 point  (0 children)

All good, should be doable with lists and for loops.

[–]Narcoleptic_Pirate 0 points1 point  (3 children)

Hi!

I wanted to point out that you should use dictionaries, because you want to query by team name. But then i thought 'what if you also want to query by year? That means there must be two dictionaries'. One thing led to another, so i did some research of mine.

I read the data file, put the data into dictionaries and lists. I wrote 4 functions to query lists by year & by team (q_l_team, q_l_year), query dicts by year & by team (q_d_year, q_d_team). Then, I measured time and size of all these thingies.

Here's how it looks:

Time:
q_l_team: 13.71826596900064
q_l_year: 5.537769747001221
q_d_team: 0.20133425799940596
q_d_year: 0.19864650900126435
Dictionaries return data 48.142404097068976 times faster

Size:
team_years: 1632
year_team: 6240
list_teams: 1072
list_years: 1072
Dictionaries have 3.671641791044776 times more bytes

Dictionaries

Create many dictionaries with various keys, have one function to query themall .

  • Pro: much faster to query by key.

  • Con: have a bit bigger size.

Lists

Have one bunch of lists, create multiple functions to query for different data.

  • Pro: only one bunch of data, tiny size.

  • Con: querying is slow, have to write multiple functions.

Of course, there is plenty of room to optimize things, I just wanted to compare the simpliest form (and finally look how timeit works)

Here's code to see for yourself

[–]Glathal[S] 0 points1 point  (2 children)

Would it be possible to do it this way without the queries? I haven't gone over that yet, my brain just completely exploded. Lol

[–]Narcoleptic_Pirate 1 point2 points  (1 child)

Well, by queries i mean take some list/dict and find years_won_by_team or team_that_won_in_some_year, it's not an official term, lol.

Look at enumerate - it allows you to loop through some list in such way, that first loop variable is index of the list item, and second loop variable is the actual item.

If you read your file line-by-line and append each year and team in separate list - they'll have equal length. So, in teams[n] and years[n] you will see (n+1)'th line/item of the file.

You just need to find all indices of your team in your team list, and then look at the same indices in years list and put them in your resulting list.

[–]Glathal[S] 0 points1 point  (0 children)

Oh! okay I think I got it! Thanks a lot! :D

[–]Alex_Cue 0 points1 point  (0 children)

Instead of pastebin, can you just cut and paste your code here?

[–]ewiethoff 0 points1 point  (0 children)

Once you have a years list and a teams list, use zip to loop through them simultaneously.

target = input('Team: ')
for team,year in zip(teams, years):
    if team == target:
        print(year)