473,549 Members | 2,935 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 2415
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
3683
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 same column. So - my values look like this: surveys=# select distinct(additionaldeliverablearray) from csssurvey; additionaldeliverablearray...
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 the consent of their authors. </annotation>
0
1506
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 AttendedBy="Mr Johnny Vegas" ApptTime="15:15"/> <Appointment AttendedBy="Mrs Albert Einstein" ApptTime="15:15"/> <Appointment AttendedBy="Mrs Albert...
6
1594
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> <Person> <Interest>Movies</Interest>
2
1119
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? Is it something with c# or c++?? tia marcelo
4
1559
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 http://www.developersdex.com ***
29
1926
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
5449
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: RecID (primary key), MRN, and Hypotension RecID Cus# Hypotension 1 222 1 2 222 ...
2
2670
by: njsimha | last post by:
Hi, I have the following question: XML snippet: <root> <template1> <elem1>1000</elem1> <elem2>
0
7518
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7715
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7956
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7469
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7808
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5368
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5087
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3498
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1057
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.