如何从元组列表中添加值

How to add the value from list of tuples

我正在从日志文件中提取并使用以下代码打印

for line in data:
    g = re.findall(r'([\d.]+).*?(GET|POST|PUT|DELETE)', line)
    print (g)

[('1.1.1.1', 'PUT')]
[('2.2.2.2', 'GET')]
[('1.1.1.1', 'PUT')]
[('2.2.2.2', 'POST')]

如何添加到输出

输出

1.1.1.1: PUT = 2
2.2.2.2: GET = 1,POST=1

你可以用字典来算:

# initialize the count dict
count_dict= dict()
for line in data:
    g = re.findall(r'([\d.]+).*?(GET|POST|PUT|DELETE)', line)
    for tup in g:
        # get the counts for tuple tup if we don't have it yet
        # use 0 (second argument to .get)
        num= count_dict.get(tup, 0)
        # increase the count and write it back
        count_dict[tup]= num+1
# now iterate over the key (tuple) - value (counts)-pairs
# and print the result
for tup, count in count_dict.items():
    print(tup, count)

好吧,我不得不承认这并没有给出你想要的确切输出,但是你可以用类似的方式做:

out_dict= dict()
for (comma_string, request_type), count in count_dict.items():
    out_str= out_dict.get(comma_string, '')
    sep='' if out_str == '' else ', '
    out_str= f'{out_str}{sep}{request_type} = {count}'
    out_dict[comma_string]= out_str

for tup, out_str in out_dict.items():
    print(tup, out_str)

根据您输出的数据:

1.1.1.1 PUT = 2
2.2.2.2 GET = 1, POST = 1

我会看柜台。

from collections import Counter
results = []
for line in data:
    g = re.findall(r'([\d.]+).*?(GET|POST|PUT|DELETE)', line)
    results.append(g[0])
ip_list = set(result[0] for result in results)
for ip in ip_list:
    print(ip, Counter(result[1] for result in results if result[0] == ip ))

您可以使用collection.defaultdict

例如:

from collections import defaultdict

result = defaultdict(list)
for line in data:
    for ip, method in re.findall(r'([\d.]+).*?(GET|POST|PUT|DELETE)', line):
        result[ip].append(method)

for k, v in result.items():
    temp = ""
    for i in set(v):
        temp += " {} = {}".format(i, v.count(i))
    print("{}{}".format(k, temp))
from collections import Counter  
x = [[('1.1.1.1', 'PUT')],[('2.2.2.2', 'GET')],[('1.1.1.1', 'PUT')],[('2.2.2.2', 'POST')]]
# step 1: convert x into a dict.
m = {}
for i in x:
    a, b = i[0]
    if a not in m.keys():
        m[a] = [b] 
    else: 
        x = m[a] 
        x.append(b)
        m[a] = x    
print('new dict is {}'.format(m))

# step 2 count frequency
m_values = list(m.values())
yy = []
for i in m_values:
    x = []
    k = list(Counter(i).keys())
    v = list(Counter(i).values())
    for i in range(len(k)):       
        x.append(k[i] + '=' + str(v[i]))
    yy.append(x)

# step 3, update the value of the dict
m_keys =  list(m.keys())
n = len(m_keys)
for i in range(n):
    m[m_keys[i]] = yy[i]

print("final dict is{}".format(m))

输出为

new dict is {'1.1.1.1': ['PUT', 'PUT'], '2.2.2.2': ['GET', 'POST']}
final dict is{'1.1.1.1': ['PUT=2'], '2.2.2.2': ['GET=1', 'POST=1']}

没有依赖项,使用字典进行计数,这是一种非常基本的方式。给定 data_set:

data_set = [[('1.1.1.1', 'PUT')],
            [('2.2.2.2', 'GET')],
            [('2.2.2.2', 'POST')],
            [('1.1.1.1', 'PUT')]]

初始化变量(手动,只需几个动词)然后遍历数据:

counter = {'PUT': 0, 'GET': 0, 'POST': 0, 'DELETE': 0}
res = {}

for data in data_set:
  ip, verb = data[0]
  if not ip in res:
    res[ip] = counter
  else:
    res[ip][verb] += 1

print(res)
#=> {'1.1.1.1': {'PUT': 1, 'GET': 0, 'POST': 1, 'DELETE': 0}, '2.2.2.2': {'PUT': 1, 'GET': 0, 'POST': 1, 'DELETE': 0}}

需要格式化输出以更好地满足您的需求。