Doublecheck, find occurrences of an element, in a list

In summary, the conversation was about utilizing basic functionalities of python to find the number of times a certain element appears in a list. The code provided seemed to work, but the group discussed more efficient ways to perform this task using built-in functionality and libraries in Python. They also explored using dictionaries and combining for loops to achieve the desired result. One member encountered a syntax error when using a certain function, but it was suggested to use a standard Python interpreter instead.
  • #1
late347
301
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
  • #2
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 late347
  • #3
Fightfish said:
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
Yes perform task as fast as possible (efficient) I will give it a read next weekend
 
  • #4
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 late347
  • #5
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]
 
  • #6
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 ChrisVer
  • #7
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')
 
  • #8
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 ChrisVer
  • #9
your guess is right
 
  • #10
I used idle and python 3.3.5 ;32bit
 

Related to Doublecheck, find occurrences of an element, in a list

1. What is the purpose of using "Doublecheck, find occurrences of an element, in a list"?

The purpose of using "Doublecheck, find occurrences of an element, in a list" is to verify the number of times a specific element appears in a given list.

2. How do you double check and find occurrences of an element in a list?

To double check and find occurrences of an element in a list, you can use a loop to iterate through the list and count the number of times the element appears. Alternatively, you can also use built-in functions such as .count() or .index() to find the occurrences of an element in a list.

3. Can I use this method for any type of list?

Yes, this method can be applied to any type of list, including lists of numbers, strings, or even nested lists.

4. What if the element I am looking for is not in the list?

If the element you are looking for is not in the list, the method will return a count of 0 or an error message depending on the programming language being used.

5. Is there a faster way to find occurrences of an element in a list?

Yes, you can use more advanced algorithms such as binary search or hash maps to find occurrences of an element in a list in a more efficient manner. These methods have a faster time complexity compared to using a loop or built-in functions.

Similar threads

  • Programming and Computer Science
Replies
2
Views
749
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
2
Replies
43
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
Replies
9
Views
2K
  • Programming and Computer Science
Replies
34
Views
3K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
16
Views
2K
Back
Top