473,385 Members | 1,912 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,385 software developers and data experts.

count repeated elements in the list

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 2408
bvdet
2,851 Expert Mod 2GB
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
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
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
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
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
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
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
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
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
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
by: njsimha | last post by:
Hi, I have the following question: XML snippet: <root> <template1> <elem1>1000</elem1> <elem2>
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.