0

I write a function that takes as input a list and returns the most common item in the list.

##Write the function
def most_frequent(List): 
    dict = {} 
    count, itm = 0, '' 
    for item in reversed(List): 
        dict[item] = dict.get(item, 0) + 1
        if dict[item] >= count : 
            count, itm = dict[item], item 
    return(item) 
  
    return num 

# verfiy the code 

list = [5,42,34,6,7,4,2,5]
print(most_frequent(list)) 

and then download two text file to get the most frequent words.

# Download the files restaurants.txt and restaurant-names.txt from Github
!curl https://raw.githubusercontent.com/ipeirotis/introduction-to-python/master/data/restaurant-names.txt -o restaurant-names.txt
!curl https://raw.githubusercontent.com/ipeirotis/introduction-to-python/master/data/restaurants.txt -o restaurants.txt



# create the list from the restaurants.txt
  List = open("restaurants.txt").readlines()

# get the most most frequent restaurant names
print("The most frequent restaurant names is ",most_frequent(List))

print(most_common(List))

but when i try to find the most frequent words that appear in the restaurant names. I got the same result. Could you help to check whether this is correct or not? Thanks

 # create the list from the restaurants.txt
List = open("restaurants.txt").readlines()

# get the most most frequent restaurant names
print("The most frequent restaurant names is ",most_frequent(List))
pythonnew
  • 23
  • 1
  • 4
  • 1
    There is a built-in class which does that for you... `collections.Counter(List).most_common(1)[0][0]` – zvone Jul 22 '20 at 19:29
  • When you test your function with known values does it work? – wwii Jul 22 '20 at 20:08
  • Related: [Is it bad practice to use a built-in function name as an attribute or method identifier?](https://stackoverflow.com/questions/9109333/is-it-bad-practice-to-use-a-built-in-function-name-as-an-attribute-or-method-ide), – wwii Jul 22 '20 at 20:15

3 Answers3

1

It's return itm (most common item) instead of return item (last part of your reversed list)

Blacksad
  • 11
  • 2
0

It seems as though you might be using the wrong filename for the restauarant names file. Judging from your curl command:

!curl https://raw.githubusercontent.com/ipeirotis/introduction-to-python/master/data/restaurant-names.txt -o restaurant-names.txt

The filename you should be using is restaurant-names.txt so your code should be:

 # create the list from the restaurants.txt
List = open("restaurants-names.txt").readlines()

# get the most most frequent restaurant names
print("The most frequent restaurant names is ",most_frequent(List))
Spencer Bard
  • 1,015
  • 6
  • 10
0

It might be the function that is wrong, what if you try the same test data but in a different order, for example: list = [42,5,34,6,5,7,4,2] instead of list = [5,42,34,6,7,4,2,5], is the output still 5?

MrPostduif
  • 61
  • 4