dictionary-comprehension

Python list and dictionary comprehension

让人想犯罪 __ 提交于 2021-02-10 12:52:30
问题 I am trying to get use to list dictionaries comprehension. Here a small code I have not been able to transform. lst = ['C', 'A', 'B', 'A'] myd = {} for v, k in enumerate(lst): if k in myd: myd[k].append(v) else: myd[k] = [v] print(myd) >>> {'C': [0], 'A': [1, 3], 'B': [2]} I would be please to have some help. 回答1: Here is the answer, although I think the non comprehension approach is much easier to understand. And as mentioned this is not efficient in the least. I would stay with what you

Python list and dictionary comprehension

烂漫一生 提交于 2021-02-10 12:52:09
问题 I am trying to get use to list dictionaries comprehension. Here a small code I have not been able to transform. lst = ['C', 'A', 'B', 'A'] myd = {} for v, k in enumerate(lst): if k in myd: myd[k].append(v) else: myd[k] = [v] print(myd) >>> {'C': [0], 'A': [1, 3], 'B': [2]} I would be please to have some help. 回答1: Here is the answer, although I think the non comprehension approach is much easier to understand. And as mentioned this is not efficient in the least. I would stay with what you

Average of elements in a list of list grouped by first item in the list

偶尔善良 提交于 2021-01-27 14:22:17
问题 My list looks like my_list = [['A', 6, 7], ['A', 4, 8], ['B', 9, 3], ['C', 1, 1]], ['B', 10, 7]] I want to find the averages of the other two columns in each of the inner lists grouped by the first column in each of the inner list. [['A', 5, 7.5], ['B', 9.5, 5], ['C', 1, 1]] ['A', 5, 7.5] comes from ['A', (6+4)/2 ,(7+8)/2] I don't mind if I end up getting a dictionary or something, but I would prefer it remain a list. I've tried the following: my_list1 = [i[0] for i in my_list] my_list2 = [i

Dictionary comprehension with if statements using list comprehension

生来就可爱ヽ(ⅴ<●) 提交于 2020-07-21 07:29:19
问题 I am trying to filter a large dictionary based on values from another dictionary. I want to store the keys to filter on in a list. So far I have: feature_list = ['a', 'b', 'c'] match_dict = {'a': 1, 'b': 2, 'c': 3} all_dict = {'id1': {'a': 1, 'b': 2, 'c': 3}, 'id2': {'a': 1, 'b': 4, 'c': 3}, 'id3': {'a': 2, 'b': 5, 'c': 3}} filtered_dict = {k: v for k, v in all_dict.items() for feature in feature_list if v[feature] == match_dict[feature]} This returns all the ids because I think the if

Dictionary comprehension with if statements using list comprehension

三世轮回 提交于 2020-07-21 07:26:21
问题 I am trying to filter a large dictionary based on values from another dictionary. I want to store the keys to filter on in a list. So far I have: feature_list = ['a', 'b', 'c'] match_dict = {'a': 1, 'b': 2, 'c': 3} all_dict = {'id1': {'a': 1, 'b': 2, 'c': 3}, 'id2': {'a': 1, 'b': 4, 'c': 3}, 'id3': {'a': 2, 'b': 5, 'c': 3}} filtered_dict = {k: v for k, v in all_dict.items() for feature in feature_list if v[feature] == match_dict[feature]} This returns all the ids because I think the if

How to construct nested dictionary comprehension in Python with correct ordering?

旧巷老猫 提交于 2020-06-25 04:37:47
问题 I was trying to shorten the code for this problem when I encountered the problem. Basically, I was trying a nested dictionary comprehension & was unsuccessful in the attempt. Here is what I tried. dict2 = {key:value for key, value in line.split(":") for line in ["1:One", "2:Two", "4:Four"]} print dict2 When I run this, it gives me NameError: name 'line' is not defined And, when I reverse the for statements like this dict2 = {key:value for line in ["1:One", "2:Two", "4:Four"] for key, value in

How to construct nested dictionary comprehension in Python with correct ordering?

两盒软妹~` 提交于 2020-06-25 04:36:30
问题 I was trying to shorten the code for this problem when I encountered the problem. Basically, I was trying a nested dictionary comprehension & was unsuccessful in the attempt. Here is what I tried. dict2 = {key:value for key, value in line.split(":") for line in ["1:One", "2:Two", "4:Four"]} print dict2 When I run this, it gives me NameError: name 'line' is not defined And, when I reverse the for statements like this dict2 = {key:value for line in ["1:One", "2:Two", "4:Four"] for key, value in