Python 将一个列表的值指定给另一个列表的值

Python 将一个列表的值指定给另一个列表的值,python,Python,我有 如何自动将a[0]赋值为b[0],并将a[1]赋值为b[1]?是否将a中的字符串与b中的值相关联?即b中第一个元素的名称price1?如果你想要一本字典: a = [price1, price2] b = [[108455, 106406, 103666, 101408, 98830], [3926, 4095, 426]] 您是否只想用a覆盖b中的内容?简单到 d = {} # i is the current position in a # key is the value a

我有


如何自动将
a[0]
赋值为
b[0]
,并将
a[1]
赋值为
b[1]

是否将
a
中的字符串与
b
中的值相关联?即b中第一个元素的名称
price1
?如果你想要一本字典:

a = [price1, price2]
b = [[108455, 106406, 103666, 101408, 98830], [3926, 4095, 426]]
您是否只想用a覆盖b中的内容?简单到

 d = {}
 # i is the current position in a
 # key is the value at that position
 for i, key in enumerate(a):
     d[key] = b[i]


不太清楚你在找什么,这个怎么样:

 a = b
实际上,
zip()

>>> price1 = 10000
>>> price2 = 22222
>>> a = [price1, price2]
>>> b = [[108455, 106406, 103666, 101408, 98830], [3926, 4095, 426]]
>>>
>>> merged_ab = {a_item: b_item for a_item, b_item in zip(a,b)}
>>> merged_ab
{10000: [108455, 106406, 103666, 101408, 98830], 22222: [3926, 4095, 426]}
>>>

我想你是想做这样的事

>>> zip(a,b)
[(10000, [108455, 106406, 103666, 101408, 98830]), (22222, [3926, 4095, 426])]
还是全部在一条线上

b = [[108455, 106406, 103666, 101408, 98830], [3926, 4095, 426]]
price1, price2 = b
另一种可能是创建一个具有属性的可变对象来保存价格

price1, price2 = [[108455, 106406, 103666, 101408, 98830], [3926, 4095, 426]]

你想做的与
a=b
有什么不同?你所说的
自动化是什么意思?您的问题似乎不够清楚。如果
price1
price2
是您希望更改其值的变量,那么您从一个奇怪的方向解决了问题。为什么不用
b[0]
而不用
price1
呢?如果这不是你想做的,那恐怕我不明白。这只是为了澄清我在做什么。我希望能够做到
price1=[10845510640610366610140898830]
而不必手动说这些。
dict(zip(a,b))
会更像python。假设这是他真正想要的。不需要字典理解(即Python3+)
dict(zip(a,b))
是所有需要的。卢卡斯,是的,dict(zip(a,b))做同样的事情,但是,字典理解在2.7中也可用。你对dict理解和2.7的看法是对的-这有多好?:)在您的“一行通”中,您甚至不需要外部的
[]
。(即,您可以解压元组而不是列表)。
price1, price2 = [[108455, 106406, 103666, 101408, 98830], [3926, 4095, 426]]
>>> class Price(object):
...     def __init__(self, value=None):
...         self.value = value
...     def __repr__(self):
...         return "Price({})".format(self.value)
... 
>>> price1 = Price()
>>> price2 = Price()
>>> a = [price1, price2]
>>> b = [[108455, 106406, 103666, 101408, 98830], [3926, 4095, 426]]
>>> for i,j in zip(a, b):
...     i.value = j
... 
>>> a
[Price([108455, 106406, 103666, 101408, 98830]), Price([3926, 4095, 426])]