0

我有

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

如何自动分配a[0]equal tob[0]a[1]to b[1]

4

3 回答 3

2

我认为您正在尝试做这样的事情

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

或全部在一条线上

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])]
于 2012-11-01T01:53:00.673 回答
0

要将字符串与 in的值相关联吗?IE b 中第一个元素的名称?如果是这样,你想要一本字典:abprice1

 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 中的内容吗?它很简单

 a[0]=b[0]

或者

 a = b
于 2012-11-01T01:36:46.357 回答
0

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

>>> 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()只有在没有键控的情况下才能做到这一点。

>>> zip(a,b)
[(10000, [108455, 106406, 103666, 101408, 98830]), (22222, [3926, 4095, 426])]
于 2012-11-01T01:39:55.760 回答