473,387 Members | 1,529 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Indicate what integer element appears the most in a list

list = [1,5,3,6,22,45,63,30,344,22,12,25,10]
I have to print the element that appears the most in my list. In this case it will be the 22.

I need some help understand where i begin.

Thanks
Nov 28 '17 #1
3 2474
numberwhun
3,509 Expert Mod 2GB
Once could use a dictionary for this. Use the value of each element in the list as the key and the count as the number for each. For each, check if its in the dictionary. If not, add it and set the value to 1. If it exists, increment the value by 1. Grab the one with the highest count.
Dec 5 '17 #2
Luuk
1,047 Expert 1GB
Ok, I am new to python, but 30 minutes worth trying I got this:

Expand|Select|Wrap|Line Numbers
  1. del a
  2. a = {}
  3. list = [1,5,3,6,22,45,63,30,344,22,12,25,10] 
  4. m = 0
  5.  
  6. for i in list:
  7.     if str(i) in a:
  8.         a[str(i)] = a[str(i)]+1
  9.     else:
  10.         a[str(i)] = 1
  11.     print i, a[str(i)]
  12.  
  13. for i in a:
  14.     print a[i], i, m
  15.     if a[i]>m:
  16.        m=a[i]
  17.        k=i
  18.  
  19. print k
  20.  
which leaves enough room for improvement... ;)
Dec 6 '17 #3
Frinavale
9,735 Expert Mod 8TB
You could always refer to the python documentation for Python Data Structures for more information on structures like a Dictionary as numberwhun suggested ;)

That link to the documentation gives a very good example of how to use Dictionaries to accomplish what you are trying to do.
Dec 6 '17 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Massimiliano Alberti | last post by:
How can I insert an empty element in a list? The insert method has as a parameter the source object to insert... So you have to use it in this way: (where MyObject is an object with only a member,...
1
by: Rod Early | last post by:
I need to know when the select element's dropdown list is opened (as when the user clicks on the arrow or does ALT-downarrow from the keyboard). Similarly, I need to known when the dropdown list...
5
by: Adam Hartshorne | last post by:
Hi All, I want to set a pointer to the penultimate element in a std::list, what is the best way of doing this? Also would setting a pointer to the end of the list, and then adding another...
3
by: Dirk Hagemann | last post by:
Hi! I have a list of lists and in some of these lists are elements which I want to change. Here an example: lists= 'None' should be replaced by 0 or NULL or something else. But as far as I...
57
by: pinkfloydhomer | last post by:
Isn't there an easier way than lst = ... ?
3
by: Don | last post by:
My user control has a combobox with an arraylist attached to it along with custom add and remove methods. The "Add" method is working great. However I don't understand why the "Remove" method...
2
by: Ben Bush | last post by:
I have a lis: ,,,,,] I want a code to test when the difference between the first element in the list of list is equal to or larger than 6, then move the previous lists to the end of the list....
6
by: Teddy.Gammell | last post by:
Hi, I would like to write a function which does this: for each element in the list call f1() of each element. if f1() return false, break the loop else continue I try to use the...
2
by: KraftDiner | last post by:
Hi I have a list of Ojbects... I want to change one of the objects in the list for a new object.... How do I replace an existing object with a new one and maintain the list order.. This is what...
0
by: Paul | last post by:
Please help me. I have an external data source which is a list of ID's, I need a reference to n element of that list. This is a XPath expression, it works. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.