dictionary-comprehension

In python how does “if-else and for” in a dictionary comprehension work

寵の児 提交于 2019-12-11 13:47:23
问题 I am confused with the following line of code: data = {n.attributes['xid']: float(n.content) if n.content else np.nan for n in graph.by_tag('value') } The dictionary comprehension consists of if-else and for loop. Can anyone explain me how the code works? 回答1: Does a translation help? data = {} for n in graph.by_tag('value'): if n.content: data[n.attributes['xid']] = float(n.content) else: data[n.attributes['xid']] = np.nan 回答2: You are confused by the ... if ... else ... conditional

multiple key value pairs in dict comprehension

橙三吉。 提交于 2019-12-09 07:56:32
问题 I am trying to create multiple key : value pairs in a dict comprehension like this: {'ID': (e[0]), 'post_author': (e[1]) for e in wp_users} I am receiving "missing ','" I have also tried it this way: [{'ID': (e[0]), 'post_author': (e[1])} for e in wp_users] I then receive "list indices must be integers, not str" Which I understand, but not sure the best way in correcting this and if multiple key : value pairs is possible with dict comprehensions? 回答1: A dictionary comprehension can only ever

Creating Dictionaries from Lists inside of Dictionaries

a 夏天 提交于 2019-12-08 07:18:33
问题 I'm quite new to Python and I have been stumped by a seemingly simple task. In part of my program, I would like to create Secondary Dictionaries from the values inside of lists, of which they are values of a Primary Dictionary. I would also like to default those values to 0 For the sake of simplicity, the Primary Dictionary looks something like this: primaryDict = {'list_a':['apple', 'orange'], 'list_b':['car', 'bus']} What I would like my result to be is something like: {'list_a':[{'apple':0

Python dictionary comprehension example

 ̄綄美尐妖づ 提交于 2019-12-07 06:13:48
问题 I am trying to learn Python dictionary comprehension, and I think it is possible to do in one line what the following functions do. I wasn't able to make the n+1 as in the first or avoid using range() as in the second. Is it possible to use a counter that automatically increments during the comprehension, as in test1() ? def test1(): l = ['a', 'b', 'c', 'd'] d = {} n = 1 for i in l: d[i] = n n = n + 1 return d def test2(): l = ['a', 'b', 'c', 'd'] d = {} for n in range(len(l)): d[l[n]] = n +

How to merge a list of multiple dictionaries into a dictionary of lists?

吃可爱长大的小学妹 提交于 2019-12-05 20:07:55
I have the following list of dictionaries in Python3.x: list_of_dictionaries = [{0:3523, 1:3524, 2:3540, 4:3541, 5:3542}, {0:7245, 1:7246, 2:7247, 3:7248, 5:7249, 6:7250}, {1:20898, 2:20899, 3:20900, 4:20901, 5:20902}] In this case, it's a single list with three dictionaries. I would like to efficiently merge this into a single dictionary with lists as values; here is the correct answer: correct = {0:[3523, 7245], 1:[3524, 7246, 20898], 2:[3540, 7247, 20899], 3:[7248, 20900], 4:[3541, 20901], 5:[3542, 7249, 20902], 6:[7250]} My first thought was a list comprehension like this: dict(pair for

Dict Comprehension python from list of lists

℡╲_俬逩灬. 提交于 2019-12-05 07:08:02
问题 I have a list of lists and I am trying to make a dictionary from the lists. I know how to do it using this method. Creating a dictionary with list of lists in Python What I am trying to do is build the list using the elements in the first list as the keys and the rest of the items with the same index would be the list of values. But I can't figure out where to start. Each list is the same length but the length of the lists vary exampleList = [['first','second','third'],['A','B','C'], ['1','2'

How to use Python left outer join using FOR/LIST/DICTIONARY comprehensions (not SQL)?

≯℡__Kan透↙ 提交于 2019-12-04 23:54:33
问题 I have two tuples, details below: t1 = [ ['aa'], ['ff'], ['er'] ] t2 = [ ['aa', 11,], ['er', 99,] ] and I would like to get results like these below using python method similar to SQL's LEFT OUTER JOIN: res = [ ['aa', 11,], ['ff', 0,], ['er', 99,] ] Please help me with this. 回答1: d2 = dict(t2) res = [[k[0], d2.get(k[0], 0)] for k in t1] 来源: https://stackoverflow.com/questions/15099022/how-to-use-python-left-outer-join-using-for-list-dictionary-comprehensions-not

Why is this loop faster than a dictionary comprehension for creating a dictionary?

谁说胖子不能爱 提交于 2019-12-04 14:59:22
问题 I don't come from a software/computer science background but I love to code in Python and can generally understand why things are faster. I am really curious to know why this for loop runs faster than the dictionary comprehension. Any insights? Problem : Given a dictionary a with these keys and values, return a dictionary with the values as keys and the keys as values. (challenge: do this in one line) and the code a = {'a':'hi','b':'hey','c':'yo'} b = {} for i,j in a.items(): b[j]=i %% timeit

How to use Python left outer join using FOR/LIST/DICTIONARY comprehensions (not SQL)?

廉价感情. 提交于 2019-12-03 16:09:35
I have two tuples, details below: t1 = [ ['aa'], ['ff'], ['er'] ] t2 = [ ['aa', 11,], ['er', 99,] ] and I would like to get results like these below using python method similar to SQL's LEFT OUTER JOIN: res = [ ['aa', 11,], ['ff', 0,], ['er', 99,] ] Please help me with this. d2 = dict(t2) res = [[k[0], d2.get(k[0], 0)] for k in t1] 来源: https://stackoverflow.com/questions/15099022/how-to-use-python-left-outer-join-using-for-list-dictionary-comprehensions-not

multiple key value pairs in dict comprehension

牧云@^-^@ 提交于 2019-12-03 11:00:32
I am trying to create multiple key : value pairs in a dict comprehension like this: {'ID': (e[0]), 'post_author': (e[1]) for e in wp_users} I am receiving "missing ','" I have also tried it this way: [{'ID': (e[0]), 'post_author': (e[1])} for e in wp_users] I then receive "list indices must be integers, not str" Which I understand, but not sure the best way in correcting this and if multiple key : value pairs is possible with dict comprehensions? A dictionary comprehension can only ever produce one key-value pair per iteration. The trick then is to produce an extra loop to separate out the