英文:
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}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论