Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python Doublecheck, find occurrences of an element, in a list

  1. Oct 13, 2016 #1
    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?

    Code (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)
     
  2. jcsd
  3. Oct 13, 2016 #2
    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
     
  4. Oct 13, 2016 #3
    Yes perform task as fast as possible (efficient) I will give it a read next weekend
     
  5. Oct 13, 2016 #4
    Yes to both, just read the documentation on list. Google finds it immediately. You can write "citrus" in lista or lista.count("citrus").
     
  6. Oct 14, 2016 #5

    ChrisVer

    User Avatar
    Gold Member

    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]?
    Code (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]
     
     
  7. Oct 14, 2016 #6
    They can:
    Code (Python):

    A = { k : 1 for k in lista }
     
     
  8. Oct 14, 2016 #7

    ChrisVer

    User Avatar
    Gold Member

    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')
     
  9. Oct 14, 2016 #8
    At a guess this is a failure of Skulpt. If my guess is right, that's terrible. Use a standard Python interpreter.
     
  10. Oct 14, 2016 #9

    ChrisVer

    User Avatar
    Gold Member

    your guess is right
     
  11. Oct 14, 2016 #10
    I used idle and python 3.3.5 ;32bit
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted