我有这样的名单,
a=['name test', 'maximum-net-data-rate 4294967295', 'minimum-expected-throughput 0', 'maximum-gamma-data-rate 4294967295', 'minimum-gamma-data-rate 0']
b=['name test', 'maximum-net-data-rate 4294967295', 'minimum-expected-throughput 0', 'maximum-gamma-data-rate 4294967295', 'minimum-gamma-data-rate 0']
c=['name test', 'maximum-net-data-rate 4294967296', 'minimum-expected-throughput 1', 'maximum-gamma-data-rate 4294967296', 'minimum-gamma-data-rate 1']
这里我试图替换列表中的每个元素' a'一个接一个地列出了列表元素' c'。 我试过这段代码:
for i in range(len(a)):
a[i]=c[i]
print a
a=['name test', 'maximum-net-data-rate 4294967295', 'minimum-expected-throughput 0', 'maximum-gamma-data-rate 4294967295', 'minimum-gamma-data-rate 0']
使用此代码我得到正确的输出:
['name test', 'maximum-net-data-rate 4294967295', 'minimum-expected-throughput 0', 'maximum-gamma-data-rate 4294967295', 'minimum-gamma-data-rate 0']
['name test', 'maximum-net-data-rate 4294967296', 'minimum-expected-throughput 0', 'maximum-gamma-data-rate 4294967295', 'minimum-gamma-data-rate 0']
['name test', 'maximum-net-data-rate 4294967295', 'minimum-expected-throughput 1', 'maximum-gamma-data-rate 4294967295', 'minimum-gamma-data-rate 0']
['name test', 'maximum-net-data-rate 4294967295', 'minimum-expected-throughput 0', 'maximum-gamma-data-rate 4294967296', 'minimum-gamma-data-rate 0']
['name test', 'maximum-net-data-rate 4294967295', 'minimum-expected-throughput 0', 'maximum-gamma-data-rate 4294967295', 'minimum-gamma-data-rate 1']
但是当我将最后一个语句(赋值)替换为
时for i in range(len(a)):
a[i]=c[i]
print a
a=b
列表' b'也在更新。如何通过直接分配整个列表来实现这一目标。
答案 0 :(得分:1)
只需将最后一行替换为:
a=b[:]
答案 1 :(得分:1)
您可以在python中尝试copy moduel
from copy import deepcopy
a = deepcopy(b)
答案 2 :(得分:0)
真的不确定你问的是什么,但在我看来,你正在努力实现以下目标:
for i in range(len(a)):
a[i]=c[i] # change only the specified itemnr of list a with the same itemnr of list c.
print a # prints (as you've seen and mentioned to be correct: list a with one of its items replaced by an item of list c)
a[i]=b[i] # Since list a[i] has been set to c[i] earlier in the loop doesn't mean it can't be changed again in the same loop.
print a
输出:
['name test', 'maximum-net-data-rate 4294967295', 'minimum-expected-throughput 0', 'maximum-gamma-data-rate 4294967295', 'minimum-gamma-data-rate 0']
['name test', 'maximum-net-data-rate 4294967295', 'minimum-expected-throughput 0', 'maximum-gamma-data-rate 4294967295', 'minimum-gamma-data-rate 0']
['name test', 'maximum-net-data-rate 4294967296', 'minimum-expected-throughput 0', 'maximum-gamma-data-rate 4294967295', 'minimum-gamma-data-rate 0']
['name test', 'maximum-net-data-rate 4294967295', 'minimum-expected-throughput 0', 'maximum-gamma-data-rate 4294967295', 'minimum-gamma-data-rate 0']
['name test', 'maximum-net-data-rate 4294967295', 'minimum-expected-throughput 1', 'maximum-gamma-data-rate 4294967295', 'minimum-gamma-data-rate 0']
['name test', 'maximum-net-data-rate 4294967295', 'minimum-expected-throughput 0', 'maximum-gamma-data-rate 4294967295', 'minimum-gamma-data-rate 0']
['name test', 'maximum-net-data-rate 4294967295', 'minimum-expected-throughput 0', 'maximum-gamma-data-rate 4294967296', 'minimum-gamma-data-rate 0']
['name test', 'maximum-net-data-rate 4294967295', 'minimum-expected-throughput 0', 'maximum-gamma-data-rate 4294967295', 'minimum-gamma-data-rate 0']
['name test', 'maximum-net-data-rate 4294967295', 'minimum-expected-throughput 0', 'maximum-gamma-data-rate 4294967295', 'minimum-gamma-data-rate 1']
['name test', 'maximum-net-data-rate 4294967295', 'minimum-expected-throughput 0', 'maximum-gamma-data-rate 4294967295', 'minimum-gamma-data-rate 0']
或者,如果您要处理类似于b
的列表a
,您也可以这样做(因为列表b的长度与a完全相同):
for i in range(len(a)):
a[i]=c[i]
print a
b[i]=c[i]
print b
输出:
['name test', 'maximum-net-data-rate 4294967295', 'minimum-expected-throughput 0', 'maximum-gamma-data-rate 4294967295', 'minimum-gamma-data-rate 0']
['name test', 'maximum-net-data-rate 4294967295', 'minimum-expected-throughput 0', 'maximum-gamma-data-rate 4294967295', 'minimum-gamma-data-rate 0']
['name test', 'maximum-net-data-rate 4294967296', 'minimum-expected-throughput 0', 'maximum-gamma-data-rate 4294967295', 'minimum-gamma-data-rate 0']
['name test', 'maximum-net-data-rate 4294967296', 'minimum-expected-throughput 0', 'maximum-gamma-data-rate 4294967295', 'minimum-gamma-data-rate 0']
['name test', 'maximum-net-data-rate 4294967296', 'minimum-expected-throughput 1', 'maximum-gamma-data-rate 4294967295', 'minimum-gamma-data-rate 0']
['name test', 'maximum-net-data-rate 4294967296', 'minimum-expected-throughput 1', 'maximum-gamma-data-rate 4294967295', 'minimum-gamma-data-rate 0']
['name test', 'maximum-net-data-rate 4294967296', 'minimum-expected-throughput 1', 'maximum-gamma-data-rate 4294967296', 'minimum-gamma-data-rate 0']
['name test', 'maximum-net-data-rate 4294967296', 'minimum-expected-throughput 1', 'maximum-gamma-data-rate 4294967296', 'minimum-gamma-data-rate 0']
['name test', 'maximum-net-data-rate 4294967296', 'minimum-expected-throughput 1', 'maximum-gamma-data-rate 4294967296', 'minimum-gamma-data-rate 1']
['name test', 'maximum-net-data-rate 4294967296', 'minimum-expected-throughput 1', 'maximum-gamma-data-rate 4294967296', 'minimum-gamma-data-rate 1']
或者你可以这样做:
for x in [a,b]:
for i in range(len(x)):
x[i]=c[i]
print(x)
输出:
['name test', 'maximum-net-data-rate 4294967295', 'minimum-expected-throughput 0', 'maximum-gamma-data-rate 4294967295', 'minimum-gamma-data-rate 0']
['name test', 'maximum-net-data-rate 4294967296', 'minimum-expected-throughput 0', 'maximum-gamma-data-rate 4294967295', 'minimum-gamma-data-rate 0']
['name test', 'maximum-net-data-rate 4294967296', 'minimum-expected-throughput 1', 'maximum-gamma-data-rate 4294967295', 'minimum-gamma-data-rate 0']
['name test', 'maximum-net-data-rate 4294967296', 'minimum-expected-throughput 1', 'maximum-gamma-data-rate 4294967296', 'minimum-gamma-data-rate 0']
['name test', 'maximum-net-data-rate 4294967296', 'minimum-expected-throughput 1', 'maximum-gamma-data-rate 4294967296', 'minimum-gamma-data-rate 1']
['name test', 'maximum-net-data-rate 4294967295', 'minimum-expected-throughput 0', 'maximum-gamma-data-rate 4294967295', 'minimum-gamma-data-rate 0']
['name test', 'maximum-net-data-rate 4294967296', 'minimum-expected-throughput 0', 'maximum-gamma-data-rate 4294967295', 'minimum-gamma-data-rate 0']
['name test', 'maximum-net-data-rate 4294967296', 'minimum-expected-throughput 1', 'maximum-gamma-data-rate 4294967295', 'minimum-gamma-data-rate 0']
['name test', 'maximum-net-data-rate 4294967296', 'minimum-expected-throughput 1', 'maximum-gamma-data-rate 4294967296', 'minimum-gamma-data-rate 0']
['name test', 'maximum-net-data-rate 4294967296', 'minimum-expected-throughput 1', 'maximum-gamma-data-rate 4294967296', 'minimum-gamma-data-rate 1']