Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Function for counting and returning the 2 most instances in a given list parameter
- def count(list1):
- # initialize dictionary (dict is not recommended name for the dictionary as it might cause some errors)
- dict = {}
- # count
- for element in list1:
- if element in dict:
- dict[element] += 1
- else:
- dict[element] = 1
- # initialize elements and corresponding values to compare
- element1 = "None" # String None to annotate
- element2 = "None"
- first = 0
- second = 0
- if len(dict) > 0:
- for key in dict:
- if dict[key] > first:
- element2 = element1
- second = first
- element1 = key
- first = dict[key]
- elif dict[key] > second:
- element2 = key
- second = dict[key]
- # put results in a list
- output = [element1, element2]
- return output
- # Main part
- list1 = ["a","a","a","b","c","c","d","d","d","d","b","a"]
- output = count(list1)
- print("The two items with most instances are "+output[0]+" and "+output[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement