Python程序对元组列表中的值进行唯一键计数
给定双元组,获取元组中每个值的唯一键计数。
Input : test_list = [(3, 4), (1, 2), (2, 4), (8, 2), (7, 2), (8, 1), (9, 1), (8, 4), (10, 4)]
Output : {4: 4, 2: 3, 1: 2}
Explanation : 3, 2, 8 and 10 are keys for value 4.
Input : test_list = [(3, 4), (1, 2), (8, 1), (9, 1), (8, 4), (10, 4)]
Output : {4: 3, 2: 1, 1: 2}
Explanation : 3, 8 and 10 are keys for value 4.
方法 #1:使用循环 + defaultdict()
在此,我们迭代每个元组元素,将键作为元组值,并使用字典值列表中遇到的每个不同键递增它。接下来,通过获取映射值列表的长度,使用另一个迭代计算频率,转换为集合以获得唯一计数。
Python3
# Python3 code to demonstrate working of
# Unique keys count for Value in Tuple List
# Using loop + defaultdict()
from collections import defaultdict
# initializing list
test_list = [(3, 4), (1, 2), (2, 4), (8, 2), (7, 2), (8, 1), (9, 1), (8, 4), (10, 4)]
# printing original list
print("The original list is : " + str(test_list))
res = defaultdict(list)
for sub in test_list:
# getting all keys to values
res[sub[1]].append(sub[0])
res = dict(res)
res_dict = dict()
for key in res:
# getting unique key counts for each value
res_dict[key] = len(list(set(res[key])))
# printing result
print("Unique keys for values : " + str(res_dict))
Python3
# Python3 code to demonstrate working of
# loop + defaultdict() + Counter()
# Using loop + defaultdict() + Counter()
from collections import defaultdict, Counter
# initializing list
test_list = [(3, 4), (1, 2), (2, 4), (8, 2), (7, 2),
(8, 1), (9, 1), (8, 4), (10, 4)]
# printing original list
print("The original list is : " + str(test_list))
mem_dict = defaultdict(list)
res = []
for sub in test_list:
# if not in dict, add value
if sub[0] not in mem_dict[sub[1]]:
mem_dict[sub[1]].append(sub[0])
res.append(sub[1])
# getting frequency
res = dict(Counter(res))
# printing result
print("Unique keys for values : " + str(res))
Python3
# Python3 code to demonstrate working of
# Unique keys count for Value in Tuple List
# Using loop + defaultdict()
from collections import defaultdict
# initializing list
test_list = [(3, 4), (1, 2), (2, 4), (8, 2), (7, 2),
(8, 1), (9, 1), (8, 4), (10, 4)]
# printing original list
print("The original list is : " + str(test_list))
res = defaultdict(list)
for sub in test_list:
# getting all keys to values
res[sub[1]].append(sub[0])
res = dict(res)
res_dict = dict()
for key in res:
# getting unique key counts for each value
res_dict[key] = len(list(set(res[key])))
# printing result
print("Unique keys for values : " + str(res_dict))
输出:
The original list is : [(3, 4), (1, 2), (2, 4), (8, 2), (7, 2), (8, 1), (9, 1), (8, 4), (10, 4)]
Unique keys for values : {4: 4, 2: 3, 1: 2}
方法 #2:使用循环 + defaultdict() + Counter()
在这种情况下,为了减少计算循环,列表是动态构建的,只有唯一值。然后 Counter() 用于获取唯一值字典。
蟒蛇3
# Python3 code to demonstrate working of
# loop + defaultdict() + Counter()
# Using loop + defaultdict() + Counter()
from collections import defaultdict, Counter
# initializing list
test_list = [(3, 4), (1, 2), (2, 4), (8, 2), (7, 2),
(8, 1), (9, 1), (8, 4), (10, 4)]
# printing original list
print("The original list is : " + str(test_list))
mem_dict = defaultdict(list)
res = []
for sub in test_list:
# if not in dict, add value
if sub[0] not in mem_dict[sub[1]]:
mem_dict[sub[1]].append(sub[0])
res.append(sub[1])
# getting frequency
res = dict(Counter(res))
# printing result
print("Unique keys for values : " + str(res))
输出:
原列表为:[(3, 4), (1, 2), (2, 4), (8, 2), (7, 2), (8, 1), (9, 1), (8) , 4), (10, 4)]
值的唯一键:{4: 4, 2: 3, 1: 2}
蟒蛇3
# Python3 code to demonstrate working of
# Unique keys count for Value in Tuple List
# Using loop + defaultdict()
from collections import defaultdict
# initializing list
test_list = [(3, 4), (1, 2), (2, 4), (8, 2), (7, 2),
(8, 1), (9, 1), (8, 4), (10, 4)]
# printing original list
print("The original list is : " + str(test_list))
res = defaultdict(list)
for sub in test_list:
# getting all keys to values
res[sub[1]].append(sub[0])
res = dict(res)
res_dict = dict()
for key in res:
# getting unique key counts for each value
res_dict[key] = len(list(set(res[key])))
# printing result
print("Unique keys for values : " + str(res_dict))