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
#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


Messages In This Thread
RE: Count number of occurrences of list items in list of tuples - by deanhystad - Nov-03-2020, 05:37 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 185 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,170 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Function to count words in a list up to and including Sam Oldman45 15 6,573 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,327 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Delete strings from a list to create a new only number list Dvdscot 8 1,532 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,209 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  List all possibilities of a nested-list by flattened lists sparkt 1 919 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Finding combinations of list of items (30 or so) LynnS 1 876 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  Adding values with reduce() function from the list of tuples kinimod 10 2,658 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,837 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