0

Through searching similar questions I've found how to use in to check if a list contains a specific element. But I wasn't able to find how to check and count if an element exists within a certain range of a list.

Example:

Inventory = [1, 2, 3, 5, 1, 2, 3, 3, 5, 0, 1, 5, 3, 6, 1, 0, 2, 4, 2, 8]

In the list there are 20 elements and 2 instances of 0. I'd like to count how many instances of zero are contained within every 5 elements with an output similar to this:

Elements 1–5 contain 0 zeroes.
Elements 6-10 contain 1 zeroes.
Elements 11-15 contain 0 zeroes.
Elements 16-20 contain 1 zeroes.

Any help is very much appreciated.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Vice Chemist
  • 109
  • 1
  • 1
    Does this answer your question? [How do you split a list into evenly sized chunks?](https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks) – wjandrea Feb 23 '21 at 18:51
  • 2
    You want to ask yourself two questions: 1. How do I extract only a certain range of elements of a list, and 2. How do I count the occurrences in these smaller "sub-lists". Much like real life, programming is actually pretty easy if you break your big problem down into smaller, manageable tasks. – Pranav Hosangadi Feb 23 '21 at 18:56
  • Does this answer your question? [How can I count the occurrences of a list item?](https://stackoverflow.com/questions/2600191/how-can-i-count-the-occurrences-of-a-list-item) – wjandrea Feb 23 '21 at 18:58
  • I tried the even size chunk method but eventually got stuck on trying to count elements of a specific list nested in another list. Pranav – your suggestion was very helpful and I solved my issue by first slicing the list and then counting the 'sub-lists,' Cheers! – Vice Chemist Feb 23 '21 at 23:58

2 Answers2

1

Python lists are indexed from zero (zero-based) so the indexes mentionned in your output will be artificial.

You can traverse your list in chunks of 5 using a step in the index range and a subscript from these indexes for a length of 5:

Inventory = [1, 2, 3, 5, 1, 2, 3, 3, 5, 0, 1, 5, 3, 6, 1, 0, 2, 4, 2, 8]
counts    = [(i,Inventory[i:i+5].count(0)) for i in range(0,len(Inventory),5)]

for i,count in counts:
    print(f"Elements {i+1}–{i+5} contain {count} zeroes.")

Elements 1–5 contain 0 zeroes.
Elements 6–10 contain 1 zeroes.
Elements 11–15 contain 0 zeroes.
Elements 16–20 contain 1 zeroes.
Alain T.
  • 40,517
  • 4
  • 31
  • 51
  • Thanks for the insight! Working with slicing was how I solved the issue. Python is my first coding language so even basic things like this are new to me. – Vice Chemist Feb 24 '21 at 00:00
0

You can slice the list into the range that you want and search for the element there, like so:

if element in Inventory[start:end]:
    # Do something
Manusman
  • 57
  • 6
  • This doesn't do any counting. And what are `start` and `end` exactly? – wjandrea Feb 23 '21 at 19:19
  • Slicing was how I ended up solving my issue. So even though this response was very vague, it did put me on the path which allowed me to figure it out. Thanks Manusman! – Vice Chemist Feb 23 '21 at 23:56