在字典的列表中查找项目出现的次数

huangapple go评论65阅读模式
英文:

Finding number of occurrences of items in a dictionary of lists

问题

以下是已翻译的代码部分:

我有两个提供的信息交易字典和交易中唯一项目的列表

    transactions = {
       "T1": ["A", "B", "C", "E"],
        "T2": ["A", "D", "E"],
        "T3": ["B", "C", "E"],
        "T4": ["B", "C", "D", "E"],
        "T5": ["B", "D", "E"]
    }
    items = ["A", "B", "C", "D", "E"]

我需要做的是找出交易中这些项目的出现次数我创建了一个字典其中的键表示唯一项目每个键的值初始化为0但我不确定如何更新这些值以表示交易中的出现次数

    occurr = dict()
    for x in items:
        occurr[x] = 0

这是我的出现次数字典它产生以下输出

    {'A': 0, 'B': 0, 'C': 0, 'D': 0, 'E': 0}

最终的字典应该如下所示

    {'A': 2, 'B': 4, 'C': 3, 'D': 3, 'E': 5}

因为'A'在交易中出现了2次'B'出现了4次等等
英文:

I have two supplied bits of information: A dictionary of transactions and a list of the unique items in the transactions.

transactions = {
   "T1": ["A", "B", "C", "E"],
    "T2": ["A", "D", "E"],
    "T3": ["B", "C", "E"],
    "T4": ["B", "C", "D", "E"],
    "T5": ["B", "D", "E"]
}
items = ["A", "B", "C", "D", "E"]

What I need to do is find the number of occurrences of these items in the transactions. I created a dictionary that has keys representing the unique items and the value for each key initialized to 0, but I am unsure of how to update these values to represent the number of occurrences in the transactions.

occurr = dict()
for x in items:
    occurr[x] = 0

This is my occurrences dictionary which yields the output:

{'A': 0, 'B': 0, 'C': 0, 'D': 0, 'E': 0}

The final dictionary should look like:

{'A': 2, 'B':4, 'C': 3, 'D': 3, 'E': 5}

as 'A' occurs 2 times in the transactions, 'B' occurs 4 times, etc.

答案1

得分: 2

你现在是在正确的方向上。你需要遍历字典的值。

occurr = dict()
for x in items:
    occurr[x] = 0

for transaction in transactions.values():
    for item in transaction:
        occurr[item] += 1

或者,你可以将所有列表连接成一个单一的列表,然后调用 collections.Counter

import collections
items = [item for transaction in transactions.values() for item in transaction]
print(collections.Counter(items))
英文:

Well, you are in the right direction. You need to iterate over the values of dictionary.

occurr = dict()
for x in items:
    occurr[x] = 0

for transaction in transactions.values():
    for item in transaction:
        occurr[item] += 1

Alternatively, you can concatenate all lists to single list and call collections.Counter:

import collections
items = [item for transaction in transactions.values() for item in transaction]
print(collections.Counter(items))

答案2

得分: 1

你可以使用Counter,例如:

from collections import Counter

transactions = {
    "T1": ["A", "B", "C", "E"],
    "T2": ["A", "D", "E"],
    "T3": ["B", "C", "E"],
    "T4": ["B", "C", "D", "E"],
    "T5": ["B", "D", "E"]
}

c = Counter()

for dd in transactions.values():
    c.update(dd)

print(c)  # 或者 c.items(), c.keys() 或 c.values()

# 结果: Counter({'E': 5, 'B': 4, 'C': 3, 'D': 3, 'A': 2})
# 请注意,结果是字典的一个子类

这将计算transactions中所有值的频率。如果需要限制到items中存在的键,则需要进行筛选。

另外,将交易列表值展开成一个列表,然后一次性计算。例如:

flatlist = [item for sublist in transactions.values() for item in sublist]
print(Counter(flatlist))
英文:

You can use Counter, for example:

from collections import Counter

transactions = {
    "T1": ["A", "B", "C", "E"],
    "T2": ["A", "D", "E"],
    "T3": ["B", "C", "E"],
    "T4": ["B", "C", "D", "E"],
    "T5": ["B", "D", "E"]
}

c = Counter()

for dd in transactions.values():
    c.update(dd)

print(c) # or c.items(), c.keys() or c.values()

# Result: Counter({'E': 5, 'B': 4, 'C': 3, 'D': 3, 'A': 2})
# Note that the result is a subclass of dict

This will count the frequency of all values in transactions. If you need to restrict to those keys present in items then filter for that.

Alternatively, flatten the transaction list values into a single list, and count that in one call. For example:

flatlist = [item for sublist in transactions.values() for item in sublist]
print(Counter(flatlist))

答案3

得分: 1

尝试

交易 = {
    "T1": ["A", "B", "C", "E"],
    "T2": ["A", "D", "E"],
    "T3": ["B", "C", "E"],
    "T4": ["B", "C", "D", "E"],
    "T5": ["B", "D", "E"],
}
物品 = ["A", "B", "C", "D", "E"]

输出 = {}
for l in 交易.values():
    for v in l:
        输出[v] = 输出.get(v, 0) + 1

输出= {k: 输出.get(k) for k in 物品}
print(输出)
英文:

Try:

transactions = {
    "T1": ["A", "B", "C", "E"],
    "T2": ["A", "D", "E"],
    "T3": ["B", "C", "E"],
    "T4": ["B", "C", "D", "E"],
    "T5": ["B", "D", "E"],
}
items = ["A", "B", "C", "D", "E"]

out = {}
for l in transactions.values():
    for v in l:
        out[v] = out.get(v, 0) + 1

out= {k: out.get(k) for k in items}
print(out)

Prints:

{'A': 2, 'B': 4, 'C': 3, 'D': 3, 'E': 5}

huangapple
  • 本文由 发表于 2023年2月16日 03:31:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464626.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定