简体   繁体   English

计算列表python中最流行的项目

[英]Counting most prevalent item in dictionary of lists python

I have a dictionary that looks like this:我有一本看起来像这样的字典:

dict_users = {
    "user1": ["item1", "item2", "item3", "item1", "item2", "elem3", "thing4", "thing5", "thing6"],
    "user2": ["elem5", "elem8", "elem2", "elem3", "elem8", "elem5", "thing7", "thing1", "thing9"],
    "user3": ["thing9", "thing7", "thing1", "thing4", "elem3", "elem9", "thing3", "thing5", "thing2"],
}

Now from here, I would like to build a new dictionary that couples the users to the item that is used the most in their list, so in this case the output for the example would be:现在从这里开始,我想构建一个新字典,将用户与其列表中使用最多的项目结合起来,因此在这种情况下,示例的输出将是:

dict_counted = {
'user1': 'item'
'user2': 'elem'
'user3': 'thing'
}

I now have something like this:我现在有这样的事情:

users = ['user1', 'user2', 'user3']

dictOfRatios = dict.fromkeys(users)

for key, value in dict_users.items():
    for value in dict_sers[key]:
        if value.startswith("item"):
            itemlist = list(value)
            for user in dictOfRatios:
                dictOfRatios[user] = len(itemlist)
                
print(dictOfRatios)

But the ouptut is not as desired and it even gives the wrong number... The criteria for matching in this case could be anything ranging from i, e, t to complete item, elem, thing.但是输出并不如所愿,它甚至给出了错误的数字......在这种情况下,匹配的标准可以是从 i、e、t 到完整的 item、elem、thing 的任何东西。

In your code -在您的代码中 -

itemlist = list(value)

This will set the same list again to item list.这将再次将相同的列表设置为项目列表。 When you do len on it, you will get the length of the full list.当你对它做 len 时,你会得到完整列表的长度。

This will solve your problem这将解决您的问题

dict_users = {'user1': ['item1', 'item2', 'item3', 'item1', 'item2', 'elem3', 'thing4', 'thing5', 'thing6'],'user2': ['elem5', 'elem8', 'elem2', 'elem3', 'elem8', 'elem5', 'thing7', 'thing1', 'thing9'],'user3': ['thing9', 'thing7', 'thing1', 'thing4', 'elem3', 'elem9', 'thing3', 'thing5', 'thing2']}

new_dict = {}
for user, value in dict_users.items():
    item_count = sum([1 for each in value if each.startswith('item')])
    elem_count = sum([1 for each in value if each.startswith('elem')])
    thing_count = sum([1 for each in value if each.startswith('user')])
    max_count = item_count
    new_value = 'item'
    if elem_count > max_count:
        max_count = elem_count
        new_value = 'elem'
    if  thing_count > max_count:
        max_count = thing_count
        new_value = 'thing'
    new_dict[user]  = new_value
     

Edit:编辑:

Just saw that the list values may have single characters to denote item , elem & thing .刚刚看到列表值可能有单个字符来表示itemelemthing

Look into regex and how to match with it.查看正则表达式以及如何与之匹配。 The same code but instead of using startswith , use regex to match.相同的代码,但不是使用startswith ,而是使用正则表达式进行匹配。

python counter is what you need python 计数器是你所需要的

from collections import Counter
import re

dict_users = {
'user1': ['item1', 'item2', 'item3', 'item1', 'item2', 'elem3', 'thing4', 'thing5', 'thing6'],
'user2': ['elem5', 'elem8', 'elem2', 'elem3', 'elem8', 'elem5', 'thing7', 'thing1', 'thing9'],
'user3': ['thing9', 'thing7', 'thing1', 'thing4', 'elem3', 'elem9', 'thing3', 'thing5', 'thing2']
}

users = {user: Counter() for user in dict_users.keys()}

for us, lst in dict_users.items():
    user_counter = users[us]
    for el in lst:
        item_name = re.split("\d",el)[0]
        user_counter[item_name] += 1

dict_counted = {user: counter.most_common(1)[0][0] for user, counter in users.items()}
print(dict_counted)

Outputs:输出:

{
 'user1': 'item',
 'user2': 'elem',
 'user3': 'thing'
}

Try this:尝试这个:

dict_users = {
    'user1': ['a', 'a', 'a', 'b', 'b', 'c'],
    'user2': ['1', '1', '1', '2', '2', '2', '2'],
    'user3': ['!', '!', '!', '!', '@', '@', '@', '@', '@']
}

unique_values = {}
final_dict = {}

for key, value in dict_users.items():
    unique_values[key] = set(value)

count = 0
for key, value in unique_values.items():
    for el in value:
        if dict_users[key].count(el) > count:
            count = dict_users[key].count(el)
        final_dict[key] = el

print(final_dict)

This gives takes the dictionary,这给出了字典,

dict_users = {
    'user1': ['a', 'a', 'a', 'b', 'b', 'c'],
    'user2': ['1', '1', '1', '2', '2', '2', '2'],
    'user3': ['!', '!', '!', '!', '@', '@', '@', '@', '@']
}

and gives you,并给你,

{'user1': 'a', 'user2': '2', 'user3': '@'}

I hope this is what you have wanted to achieve.我希望这就是你想要实现的目标。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM