Python Forum
Count number of occurrences of list items in list of tuples
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Count number of occurrences of list items in list of tuples
#1
Having a hard time with this one guys-

I need to write a function that takes 3 arguments: data, year_start, year_end.

The data is a list of tuples. year_start and year_end are input by user.

The function needs to count the number of occurrences in 'data', where any year in the date range is in position [0] (position [0] in data is the year).

I need to generate lists of tuples for earthquake_count_by_year = [], and total_damage_by_year = [] in the format [(year, value), (year, value)] for each year in the range.

Here's what I have:

def summary_statistics(data, year_start, year_end):
    earthquake_count_by_year = []
    total_damages_by_year = []
    casualties_by_year = []
    count = 0
    years = []
    year_start = int(year_start)
    year_end = int(year_end)
    
    if year_end >= year_start:
        # store range of years into list
        years = list(range(year_start, year_end+1))
        for index, tuple in enumerate(data):
            if tuple[0] in years:
                count[tuple[0]] += 1
        print(count)
The above is just my attempt to count the number of occurrences in the input for each year. I feel like if I can get this much, I can figure out the rest.

Here is the input for "data":
[(2020, 1, 6.0, 'CHINA:  XINJIANG PROVINCE', 39.831, 77.106, 1, 0, 2, 0), (2020, 1, 6.7, 'TURKEY:  ELAZIG AND MALATYA PROVINCES', 38.39, 39.081, 41, 0, 1600, 0), (2018, 1, 7.7, 'CUBA: GRANMA;  CAYMAN IS;  JAMAICA', 19.44, -78.755, 0, 0, 0, 0), (2019, 2, 6.0, 'TURKEY: VAN;  IRAN', 38.482, 44.367, 10, 0, 60, 0), (2018, 3, 5.4, 'BALKANS NW:  CROATIA:  ZAGREB', 45.897, 15.966, 1, 0, 27, 6000.0), (2020, 3, 5.7, 'USA: UTAH', 40.751, -112.078, 0, 0, 0, 48.5), (2020, 3, 7.5, 'RUSSIA:  KURIL ISLANDS', 48.986, 157.693, 0, 0, 0, 0)]
Reply
#2
Start with writing the test to determine if a date is in the specified range. Can you do that? You don't need anything fancy like datetime, just check if a number is in a range. What you have is not going to work. It would only match the year_start or year_end, not the values inbetween. item in
  • returns True if item matches something in the list, it is not interpreting the list as a range.

    The way you were trying to keep count wasn't going to work either. You treat count as an integer and as a list. You can't do that. You will probably want count to be a collection of some kind. I would use a dictionary where the key is the year and the value is the count.
    years={}
    if tuple[0] in years:
        count[tuple[0]] = count.get(tuple[0], 0) + 1
    Like the comparison I would work this part independently too. Try adding a few years to your count and then print the count to see if it works.

    It is easier to solve small problems than big problems. If you cannot figure out how to write a program to solve a big problem, first write a program to solve a small piece of the big program. Then write another programe to solve a different piece.

    That's how I solved this problem. First I wrote something that generated a random list of numbers in the range 1900 to 2020 to represent your earthquake data. Then I write a little loop to print True or False based on if the year in the list was in the range 1996 to 2020. Then I wrote the dictionary part to save the years that are in that range, and finally I wrote the part that incremented the count each time a year was found. A bunch of little programming tasks that were fairly simple but added up to a good working example.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Use one list as search key for another list with sublist of list jc4d 1 136 Nov-30-2021, 09:07 AM
Last Post: Gribouillis
  deleting select items from a list Skaperen 13 792 Oct-11-2021, 01:02 AM
Last Post: Skaperen
  Getting All Items From A List knight2000 4 467 Sep-25-2021, 12:56 AM
Last Post: knight2000
  count item in list korenron 8 937 Aug-18-2021, 06:40 AM
Last Post: naughtyCat
  Why changing data in a copied list changes the original list? plumberpy 3 640 Aug-14-2021, 02:26 AM
Last Post: plumberpy
  Compile list of dictianories out of another list of dictianories by certain keys CatorCanulis 10 1,291 Jun-10-2021, 08:35 PM
Last Post: perfringo
  Feed List items with Integer euras 9 1,312 May-19-2021, 07:45 PM
Last Post: snippsat
Question convert unlabeled list of tuples to json (string) masterAndreas 4 2,573 Apr-27-2021, 10:35 AM
Last Post: masterAndreas
  Parse String between 2 Delimiters and add as single list items lastyle 5 1,111 Apr-11-2021, 11:03 PM
Last Post: lastyle
  Order a list with successive permutations based on another list yvrob 3 999 Mar-19-2021, 08:20 AM
Last Post: supuflounder

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020