Doublecheck, find occurrences of an element, in a list

  • Context: Python 
  • Thread starter Thread starter late347
  • Start date Start date
  • Tags Tags
    Element List
Click For Summary

Discussion Overview

The discussion revolves around finding and counting occurrences of an element in a list using Python. Participants explore various methods, including basic loops and dictionary usage, while considering efficiency and built-in functionalities.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant shares a simple loop method to count occurrences of "citrus" in a list and seeks more efficient alternatives.
  • Another participant suggests that there are built-in functionalities in Python for counting list items and provides a link for reference.
  • A different participant expresses interest in both algorithm formulation and efficient execution, indicating a preference for speed.
  • One participant proposes using dictionaries to count occurrences and questions if two loops can be combined for efficiency.
  • Another participant confirms that the two loops can be combined using a dictionary comprehension.
  • Concerns are raised about compatibility with different Python versions, particularly regarding syntax errors encountered in an online interpreter.
  • A participant confirms the syntax error and suggests using a standard Python interpreter instead.

Areas of Agreement / Disagreement

Participants express varying opinions on the best approach to count occurrences, with some favoring built-in methods and others exploring custom implementations. There is no consensus on a single best method, and discussions about compatibility issues indicate differing experiences with Python versions.

Contextual Notes

Some participants mention specific Python versions and environments, highlighting potential limitations in functionality or syntax support that may affect the implementation of proposed solutions.

late347
Messages
300
Reaction score
15
I was making a little bit of a thought experiment with a simple program and wanted to double check it with you guys.

The idea was to utilize basic functionalities of python, regarding lists. Then the goal is to find out and print the number of times that a certain element occurs inside a particular list.

I think the code seems to work.

Is there a more specified way to test out whether or not a certain element appears in a list, and count how manys times that element appears in the list?

Python:
lista = [1, 2, 66, 77, 66, 54, -9, "citrus", 66, "citrus"]
citrusNumberOf = 0

for x in lista:
    if x == "citrus":
        citrusNumberOf = citrusNumberOf + 1
print(citrusNumberOf)
 
Technology news on Phys.org
late347 said:
Is there a more specified way to test out whether or not a certain element appears in a list, and count how manys times that element appears in the list?
Are you more interested in the formulation of the algorithms, or just how to perform such a task efficiently in Python? If it's the latter, there's a plethora of in-built functionality and libraries to achieve it, as succinctly summarised here: http://stackoverflow.com/questions/2600191/how-can-i-count-the-occurrences-of-a-list-item-in-python
 
  • Like
Likes   Reactions: late347
late347 said:
Is there a more specified way to test out whether or not a certain element appears in a list, and count how manys times that element appears in the list?

Yes to both, just read the documentation on list. Google finds it immediately. You can write "citrus" in lista or lista.count("citrus").
 
  • Like
Likes   Reactions: late347
Wanted to try dictionaries, what do you think about this? In particular, does anyone think that the two fors can be combined [right now I'm blind]?
Python:
lista= [1, 2, 66, 77, 66, 54, -9, "citrus", 66, "citrus"]
choice = 77
#choice = "citrus"
#choice= 66
#choice= 123456789
A=dict()
for element in lista: A[element]=0
for element in lista: A[element] += 1
if choice not in A.keys(): A[choice]=0
print A[choice]
 
ChrisVer said:
Wanted to try dictionaries, what do you think about this? In particular, does anyone think that the two fors can be combined [right now I'm blind]?

They can:
Python:
A = { k : 1 for k in lista }
 
  • Like
Likes   Reactions: ChrisVer
hmm I guess it's a python version that can support it? Because I tried it in my fast python-resort: http://www.codeskulptor.org/
but it says
SyntaxError: bad input ('for')
 
ChrisVer said:
hmm I guess it's a python version that can support it? Because I tried it in my fast python-resort: http://www.codeskulptor.org/
but it says
SyntaxError: bad input ('for')
At a guess this is a failure of Skulpt. If my guess is right, that's terrible. Use a standard Python interpreter.
 
  • Like
Likes   Reactions: ChrisVer
your guess is right
 
  • #10
I used idle and python 3.3.5 ;32bit
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 43 ·
2
Replies
43
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K
Replies
9
Views
3K
  • · Replies 34 ·
2
Replies
34
Views
6K
  • · Replies 23 ·
Replies
23
Views
3K