I'm a python beginner.
dict1 = {"group1":["a", "a", "b", "a"], "group2":["a", "a", "a", "a", "a"]}
dict2={"group1":["1", "2", "1", "1", "1", "group2":["1", "1", "1", "2", "3", "4"]}
This is the same group from the left of the values.For example, in group 1, one of a in group 1 is one group, and
In addition, there are four groups: 2 in a in group 1, 1 in b in group 1, 1 in a in group 1, and 4 in a in group 1.
The following is what you would like to do with the dictionary dict1, dict2.
それぞれ Match the values in each group
②Determining how many different types exist (in group 1, there are three types because there are two types of 1 in a, two in a, and one in b).
③Extract as a dictionary
I would like a dictionary similar to the one below.
result={"group1":3,"group2":4}
I thought about the program proposal despite my lack of knowledge.
dict3 = {}
import collections
forkindict2.keys():
c=collections.Counter(dict2[k])
iflen(c)>=2:
dict3[k]=len(c)
print(dict3)
# Output
{"group1":2, "group2":4}
Of course, it can only be classified by the value of dict2.
Please let me know.
dict1={"group1":["a", "a", "b", "a"], "group2":["a", "a", "a", "a", "a", "a"]}
dict2={"group1":["1", "2", "1", "1", "1", "group2":["1", "1", "2", "3", "4"]}
result = {k:len(set(zip(dict1[k], dict2[k]))))) for direct1.keys()}
print(result)
# {'group1':3,'group2':4}
Create a set of elements, each of the values in the dict1
and dict2
lists, summarized by tuple
, such as ('a', '1')
.
I think the number of elements in that set will be the number you want to find.
dict1={"group1":["a", "a", "b", "a"], "group2":["a", "a", "a", "a", "a", "a"]}
dict2={"group1":["1", "2", "1", "1", "1", "group2":["1", "1", "2", "3", "4"]}
ret = {}
for key, lst1indict1.items():
lst2 = dict2 [ key ]
s = set ([(v1, v2) for v1, v2 in zip(lst1, lst2)])
print(s)#{('b', '1'), ('a', '1'), ('a', '2')} etc.
ret [key] = len(s)
print(ret)# {'group1':3, 'group2':4}
© 2024 OneMinuteCode. All rights reserved.