有没有更好的方法来做到这一点? [英] Is there a nicer way to do this?
问题描述
是否有更好的方法来执行以下操作?
attributes = [''foo'',''bar'']
attributeNames = {}
n = 1
属性中的属性:
attributeNames [" AttributeName %d" %n] =属性
n = n + 1
它有效,但我想知道是否有更多的pythonic方式
这样做。
S.
Is there a better way to do the following?
attributes = [''foo'', ''bar'']
attributeNames = {}
n = 1
for attribute in attributes:
attributeNames["AttributeName.%d" % n] = attribute
n = n + 1
It works, but I am wondering if there is a more pythonic way to
do this.
S.
推荐答案
你好,
Stefan Arentzaécrit:
Hello,
Stefan Arentz a écrit :
有更好的方法可以做到以下几点吗?
attributes = [''foo' ',''bar'']
attributeNames = {}
n = 1
属性中的属性:
attributeNames [" AttributeName。%d" %n] =属性
n = n + 1
它有效,但我想知道是否有更多的pythonic方式
这样做。
S.
Is there a better way to do the following?
attributes = [''foo'', ''bar'']
attributeNames = {}
n = 1
for attribute in attributes:
attributeNames["AttributeName.%d" % n] = attribute
n = n + 1
It works, but I am wondering if there is a more pythonic way to
do this.
S.
您可以使用enumerate()对项目进行编号(小心它以0):
attributes = [''foo'',''bar'']
attributeNames = {}
对于n,枚举(属性)中的属性:
attributeNames [" AttributeName。%d" %(n + 1)] =属性
然后使用生成器表达式来提供dict:
attributes = [''foo'','' bar'']
attributeNames = dict((" AttributeName。%d"%(n + 1),attribute)
代表n,属性为枚举(属性) )
希望这会有所帮助,
-
Amaury
You could use enumerate() to number the items (careful it starts with 0):
attributes = [''foo'', ''bar'']
attributeNames = {}
for n, attribute in enumerate(attributes):
attributeNames["AttributeName.%d" % (n+1)] = attribute
Then use a generator expression to feed the dict:
attributes = [''foo'', ''bar'']
attributeNames = dict(("AttributeName.%d" % (n+1), attribute)
for n, attribute in enumerate(attributes))
Hope this helps,
--
Amaury
attributes = [''foo'',''bar'']
attributes = [''foo'', ''bar'']
>
attributeNames = {}
n = 1
属性中的属性:
attributeNames [" AttributeName。%d" %n] =属性
n = n + 1
它有效,但我想知道是否有更多的pythonic方式
这样做。
>
attributeNames = {}
n = 1
for attribute in attributes:
attributeNames["AttributeName.%d" % n] = attribute
n = n + 1
It works, but I am wondering if there is a more pythonic way to
do this.
更好可能是主观的,但你可以做类似的事情
attributes = [''foo'',''bar'']
attributeNames = dict(
(" AttributeName。%d"%(i + 1),attrib)
for i,attrib
in enumerate(attributes)
)
HTH,
-tkc
"Better" may be subjective, but you could do something like
attributes = [''foo'', ''bar'']
attributeNames = dict(
("AttributeName.%d" % (i+1), attrib)
for i, attrib
in enumerate(attributes)
)
HTH,
-tkc
不确定这是否真的更好,甚至更加pythonic,但是如果你像朋友一样运行这种语言的
:
attributeNames = dict([(" AttributeName。%d"%(n + 1),属性)
n,枚举(属性)中的属性])
它的作用是创建一个列表(使用列表推导和
枚举函数)(AttributeName.x,属性)元组,然后是
用于初始化字典。
Not sure if this is really better or even more pythonic, but if you
like one-liners that exercise the language:
attributeNames = dict( [("AttributeName.%d" % (n+1), attribute) for
n,attribute in enumerate(attributes)] )
What this does is create a list (using a list comprehension and the
enumerate function) of ("AttributeName.x", attribute) tuples which is
then be used to initialize a dictionary.
这篇关于有没有更好的方法来做到这一点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!