In python how does “if-else and for” in a dictionary comprehension work
问题 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