讓我們假設,我有一個包含以下串列的陣列:
data = [['a', 'b', 'c'],['a', 'b'],['c']]
通過它們所在的串列數量來計算每對出現的最佳解決方案是什么?
例如結果應該是:
member_one_is member_two_is COUNT
a b 2
a c 1
b c 1
uj5u.com熱心網友回復:
使用collections.Counterand 的一種方法itertools.combinations:
from collections import Counter
from itertools import combinations
import pandas as pd
data = [['a', 'b', 'c'], ['a', 'b'], ['c']]
# get the counts using collections Counter and the combinations using combinations
# make sure each sub-list is sorted with sorted
counts = Counter(combination for lst in map(sorted, data) for combination in combinations(lst, 2))
# create the DataFrame
df = pd.DataFrame(data=[[*k, v] for k, v in counts.items()], columns=["member_one_is", "member_two_is", "COUNT"])
print(df)
輸出
member_one_is member_two_is COUNT
0 a b 2
1 a c 1
2 b c 1
請注意,如果串列已排序,您可以跳過map(sorted, data)并直接迭代data。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/343407.html
