[英]list in the value of a dict python
I am trying to write a function that contains a dict
in a way that the value of every key looks in the following order: for exmple if the function got the following list: 我正在尝试编写一个包含dict
的函数,使每个键的值按以下顺序显示:例如,如果该函数具有以下列表:
list = [(a,b),(a,c)(f,q)]
it would create this: 它将创建以下内容:
dict = {'a':('a',['b','c'],'f':('f',['q'])}
def create_dict(self, link_list):
"""
creating a dict
:param link_list:
:return:
"""
collector = {}
for tup in link_list:
if tup[0] not in collector:
collector[str(tup[0])] = (tup[0], [tup[1]])
else:
collector[tup[0]][1].append(tup[1])
I'm having some problems with the syntax and how to approach the list in the value of every key. 我在语法以及如何在每个键的值中接近列表方面遇到一些问题。
You've got a typo on the first line. 您在第一行有错字。
list = [(a,b),(a,c)(f,q)] # missing comma
# should be:
list = [(a,b),(a,c),(f,q)]
In your version, (a, c)
is a tuple and then without a comma, the (f, q)
after it represents calling that as a function with f
and q
as parameters. 在您的版本中, (a, c)
是一个元组,然后没有逗号,其后的(f, q)
表示将其作为函数使用f
和q
作为参数来调用。
Also, don't use built-in names for variables. 另外,请勿将内置名称用于变量。 Name it list_
instead, at least. 至少将其命名为list_
。
Edit: wrt "represents calling that as a function " == or rather, as a callable . 编辑: wrt“表示作为函数调用= =或更确切地说, 作为callable 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.