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


Messages In This Thread
Count number of occurrences of list items in list of tuples - by t4keheart - Nov-03-2020, 04:23 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to parse and group hierarchical list items from an unindented string in Python? ann23fr 0 179 Mar-27-2024, 01:16 PM
Last Post: ann23fr
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,156 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Function to count words in a list up to and including Sam Oldman45 15 6,541 Sep-08-2023, 01:10 PM
Last Post: Pedroski55
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,321 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Delete strings from a list to create a new only number list Dvdscot 8 1,511 May-01-2023, 09:06 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,187 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  List all possibilities of a nested-list by flattened lists sparkt 1 914 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Finding combinations of list of items (30 or so) LynnS 1 869 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  Adding values with reduce() function from the list of tuples kinimod 10 2,634 Jan-24-2023, 08:22 AM
Last Post: perfringo
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,833 Oct-26-2022, 04:03 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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