简体   繁体   中英

How to count the string occurrences in python

I have this file

10, 44.66.44.55 , AD, AC , 112
10, 44.66.54.55 , AD, AC , 112
10, 44.66.44.55 , AD, AC , 112
50, 44.66.64.55 , AD, AC , 112
10, 44.66.54.55 , AD, AC , 112
10, 44.66.44.55 , AD, AC , 112

I want to add the column1 with same ip address. i want the output like

30, 44.66.44.55 , AD, AC , 112
20, 44.66.54.55 , AD, AC , 112
50, 44.66.64.55 , AD, AC , 112

I want to do in python

I tried

import re
import collections

a = collections.Counter()
with open("temp.txt", "r") as f:
   for line in f.readlines():
         list = line.split()
     a[list[1]] += int(list[0])
         print list[1]

You may use the itertools.groupby solution, which is ideal in this case

>>> with open("test.csv") as fin:
    grouped_lines = groupby(sorted((e.split(',') for e in fin), key = itemgetter(1)), key = itemgetter(1))


>>> for k, v in grouped_lines:
    lines = list(v)
    lines[0][0] = sum(int(e[0]) for e in lines)
    print lines[0]


[30, ' 44.66.44.55 ', ' AD', ' AC ', ' 112\n']
[20, ' 44.66.54.55 ', ' AD', ' AC ', ' 112\n']
[50, ' 44.66.64.55 ', ' AD', ' AC ', ' 112\n']

You need to split on , not on white spaces

try this

list = line.split(',')

There is a collections.Counter module. It returns a dictionary of of {'word': numberTimes} http://docs.python.org/2/library/collections.html

Although @Abhijit's answer is shorter,
Try this, it also works.

After processing the file data, I store the data in a dictionary. This data is then manipulated when needed.
Your data is the values in the dict.

all_ips = {}

f = open('Test2.txt')
lines = f.readlines()
f.close()

for line in lines:
    ip = line.split(',')[1]

    props = line.split(',')
    props[0] = int(props[0])

    if ip not in all_ips:
        all_ips[ip] = props
    else:
        all_ips[ip][0] += props[0]

for ip in all_ips:
    print all_ips[ip]

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