简体   繁体   中英

Getting how many times the most repetitive element repeats in a list

I have to store in a variable how many times the most repetitive element repeats in a list. But I should not use any list comprehension, or dictionary or counting by dictionaries. The farthest I am able to use is list methods.

the_max_occurrence = max([my_list.count(x) for x in range(1,11)])

For example, something like this giving the same value.

The following should work, given that my_list.count() is allowed, as you clarified in comments:

l=[]
for i in set(my_list):
    l.append((i, my_list.count(i)))

the_max_occurrence=max(l, key=lambda x: x[1])[1]

If you don't want to use any new list at all, you could do the following:

the_max_occurrence=0

for i in set(my_list):
    the_max_occurrence=max(the_max_occurrence, my_list.count(i))

Based on your requirement,

  • We take counts for each element
  • Counts are saved into a count list
  • We find Maximum of the count list
  • We find the index of the maximum of the counts
  • we retrieve the element which is the same index of list A
counts =[]

listA = [2,4,5,3,5,5,5,4,4,5,5,4,2]

for i in range(len(listA)):
  counts.append(1)
  for j in range(i+1,len(listA)-1):
     if listA[i] == listA[j]:
        counts[i] = counts[i]+1
print(counts) # [1, 4, 6, 1, 5, 4, 3, 3, 2, 2, 1, 1, 1]

max = 0
for k in counts:
  if k>max:
    max = k
print(max) #6

for i in range(len(counts)):
    if counts[i] == max:
       index = i
       break
print(index) #2

high_occurace_element = listA[index]
print(high_occurace_element)  #5 

You can sort the list based on the number of occurrences of each element, then get the count of occurrences of the first element.

lst = []
lst = sorted(lst, key=lst.count, reverse=True)
print(lst.count(lst[0]))

Or, as Peter's comment suggested, a simple way would be

max(my_list.count(value) for value in set(my_list))

If you don't want to use list,dict etc. Then you are writing the child's code. The best I came up with which uses basic things.

lst = [1, 4, 56, 2, 2, 1, 2, 4, 6, 7]
maxcount = 0
for i in range(len(lst)):
    mycount = 0
    for j in lst[i:]:
        if lst[i] == j:
            mycount += 1
    if mycount > maxcount:
        maxcount = mycount
print(maxcount)

You better not use python if you want to write this kind of code

One solution I found. (Can only sort numbers) Does this meet your requirements?

def most_frequency(List):
     work_list = [int(x) for x in List]
     work_list.sort()
     counter = 0
     num = List[0] 
       
     for i in work_list: 
         curr_frequency = work_list.count(i) 
         if(curr_frequency> counter): 
             counter = curr_frequency 
             num = i 
   
     return str(num) + "," + str(curr_frequency)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM