Python 如何计算出现次数并更新字典值?

Python 如何计算出现次数并更新字典值?,python,dictionary,Python,Dictionary,我正在遍历一个数据库列 我需要创建一个字典,如果满足条件,它会更新某些键的某些值 比如说 The first iteration is: 'apples' The dictionary should be {'apples': 1} The second iteration is: 'peers' The dictionary should be {'apples': 1, 'peers': 1} The third iteration is: 'apples' The dictionary

我正在遍历一个数据库列

我需要创建一个字典,如果满足条件,它会更新某些键的某些值

比如说

The first iteration is: 'apples'
The dictionary should be {'apples': 1}

The second iteration is: 'peers'
The dictionary should be {'apples': 1, 'peers': 1}

The third iteration is: 'apples'
The dictionary should be {'apples': 2, 'peers': 1}
我为基本的解释道歉。这是我认为传达我想要的最好的方式,因为我不知道如何编写代码

我需要将其放在字典中,因为此操作深入到嵌套for循环结构中

目标是:

就是得到最容易出现的迭代

预期结果:

mostListed = 'apples'
我是python新手,如果我遗漏了一些明显的东西,我非常愿意学习使用集合中的计数器:

例如,使其适用于您的案例:

from collections import Counter

list_ = []

for item in ["first", "second", "third"]:
    input_value = input(f"{item} iteration: ")
    list_.append(input_value)
count = Counter(list_)
print(count) # output: Counter({'apples': 2, 'pears': 1})
print(count.most_common(1)) # output: [('apples', 2)]
使用集合中的计数器:

例如,使其适用于您的案例:

from collections import Counter

list_ = []

for item in ["first", "second", "third"]:
    input_value = input(f"{item} iteration: ")
    list_.append(input_value)
count = Counter(list_)
print(count) # output: Counter({'apples': 2, 'pears': 1})
print(count.most_common(1)) # output: [('apples', 2)]

您可以通过执行以下操作来更新字典中键的值

if 'apples' in dict:
    dict['apples'] += 1
else:
    dict['apples'] = 1
您可以通过以下方式找到具有最大值的键:

most_listed = max(dict, key=lambda k: dict[k])

您可以通过执行以下操作来更新字典中键的值

if 'apples' in dict:
    dict['apples'] += 1
else:
    dict['apples'] = 1
您可以通过以下方式找到具有最大值的键:

most_listed = max(dict, key=lambda k: dict[k])
以下是一个例子:

my_list = ['apples', 'apples', 'peers', 'apples', 'peers']
new_dict = {}

for i in my_list:
    if i in new_dict:
        new_dict[i] += 1
    else:
        new_dict[i] = 1

print(new_dict)
以下是一个例子:

my_list = ['apples', 'apples', 'peers', 'apples', 'peers']
new_dict = {}

for i in my_list:
    if i in new_dict:
        new_dict[i] += 1
    else:
        new_dict[i] = 1

print(new_dict)
无违约地 您可以使用以下代码:

d = {}
for iteration in ['first', 'second', 'third']:
    value = input(f'The {iteration} iteration is:')
    if value in d:
        d[value] += 1
    else:
        d[value] = 1

print(d)
输出:

使用defaultdict 您可以创建默认值为0的defaultdict,如下所示:

from _collections import defaultdict
d = defaultdict(lambda: 0)
for iteration in ['first', 'second', 'third']:
    value = input(f'The {iteration} iteration is:')
    d[value] += 1

print(dict(d))
输出

无违约地 您可以使用以下代码:

d = {}
for iteration in ['first', 'second', 'third']:
    value = input(f'The {iteration} iteration is:')
    if value in d:
        d[value] += 1
    else:
        d[value] = 1

print(d)
输出:

使用defaultdict 您可以创建默认值为0的defaultdict,如下所示:

from _collections import defaultdict
d = defaultdict(lambda: 0)
for iteration in ['first', 'second', 'third']:
    value = input(f'The {iteration} iteration is:')
    d[value] += 1

print(dict(d))
输出


为了清晰起见,在已有众多答案的基础上增加了这一点

from collections import Counter

values = ['apples', 'peers', 'apples']

Counter(values).most_common(1)

>>> [('apples', 2)]

为了清晰起见,在已有众多答案的基础上增加了这一点

from collections import Counter

values = ['apples', 'peers', 'apples']

Counter(values).most_common(1)

>>> [('apples', 2)]

谢谢,这正是我想要的!拯救了一天谢谢你这正是我要找的!拯救了这一天