473,513 Members | 2,559 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

count repeated elements in the list

1 New Member
I am new to Python. I am trying to find a simple way of getting a count of the number of elements repeated in a file(of first column)
e.g file contain the following list(-First column contain IP address)

10.2.1.12 2 4
192.12.23.2 3 5
10.2.1.12 1 2
192.11.23.1 3 5
10.2.1.12 4 5
192.12.23.2 1 6

Output:
IP Address count( number of repeated of 1st column)
10.2.1.12 3
192.12.23.2 2
192.11.23.1 1
Apr 23 '14 #1
2 2413
bvdet
2,851 Recognized Expert Moderator Specialist
If the order is not important, this can easily be done with a dictionary and dict method setdefault.
Expand|Select|Wrap|Line Numbers
  1. f = open("ip.txt")
  2. dd = {}
  3. for line in f:
  4.     lineList = line.split()
  5.     if lineList:
  6.         ip = lineList[0]
  7.     v = dd.setdefault(ip, 0)
  8.     dd[ip] += 1
  9.  
  10. f.close()
  11.  
  12. for key in dd:
  13.     print key, dd[key]
To display in order of count:
Expand|Select|Wrap|Line Numbers
  1. ipList = [(key, dd[key]) for key in dd]
  2. ipList.sort(key=lambda a: a[1])
  3. for item in ipList:
  4.     print item[0], item[1]
Apr 23 '14 #2
maya29988
7 New Member
You can use dictionaries for this
Expand|Select|Wrap|Line Numbers
  1. d=dict()
  2. fp=open("file.txt")
  3. for i in fp:
  4.     l=i.split()[0]
  5.     if l in d:
  6.         d[l] += 1
  7.     else:
  8.         d[l] = 1
  9.  
  10. print d.items()
  11.  
  12.  
May 2 '14 #3

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

Similar topics

1
3680
by: Hunter | last post by:
Hi group - I'm using php to extract data from postgres and print to browser. I have data that was input through checkboxes on a form - each checkbox had a different value but got pushed into the...
0
625
by: Wolfgang Lipp | last post by:
From: Lipp, Wolfgang Sent: Tuesday, 27?January?2004 13:26 <annotation> the first eleven contributions in this thread started as an off-list email discussion; i have posted them here with...
0
1504
by: Jane | last post by:
Hi, I have the following XML: <DailySchedule> <Appointment AttendedBy="Mr Judith Chalmers" ApptTime="9:15"/> <Appointment AttendedBy="Mr Johnny Vegas" ApptTime="13:15"/> <Appointment...
6
1591
by: bosgoverde | last post by:
I've tried several ways to achieve a xsd schema for the following xml example, but failed to do so. Valid: <Person> <Interest>Movies</Interest> <Interest>Computers</Interest> </Person>...
2
1118
by: Marcelo | last post by:
Hi, in my c# app, I access an collection from a COM componente of mine. The problem is that it brings elements repeated into positions 0 and 1. Does anybody know how to explain this behaviour?...
4
1552
by: juli jul | last post by:
Hello, How can I pass over all the elements that are in the first column of listview? I need to do it in a loop and to present each cell value,how? Thank you! *** Sent via Developersdex...
29
1922
by: n00m | last post by:
http://www.spoj.pl/problems/SUMFOUR/ 3 0 0 0 0 0 0 0 0 -1 -1 1 1 Answer for this input data is 33. My solution for the problem is...
7
5445
by: elainenguyen | last post by:
Hi, I am trying create a query that counts all records which have the field Hypotension=1 (which is Yes), but I only want to count the same customer once. For example: My table has 3 fields:...
2
2668
by: njsimha | last post by:
Hi, I have the following question: XML snippet: <root> <template1> <elem1>1000</elem1> <elem2>
0
7260
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
7160
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7384
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,...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5086
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3233
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.