字典的方法.viewvalues()
和.viewkeys()
允许创建在每次字典修改时链接和动态更新的列表变量,例如:
diction={'one':1,'two':2,'three':3}
dict_values=dictVar.viewvalues()
dict_keys=dictVar.viewkeys()
我想知道是否可以通过列表实现类似的功能。因此,如果有两个“源”列表变量,第三个列表是两个之和的结果:
a=[1,2,3]
b=[4,5,6]
sum=a+b
现在我想要的是一个列表变量sum
在修改列表变量a
或列表变量b
时更新。如何做到这一点?你知道吗
您必须为自定义数据结构设置权限才能执行此操作。这是正确的方向。。。你知道吗
我会定义一个函数来完成它,然后在需要列表时调用它。你知道吗
然后,在口译员中:
如果不需要一个简单的列表,你可以很容易地做你想做的事。你知道吗
也就是说,我建议不要定义名为
sum
的变量,因为它是built-in Python function。你知道吗您可以用
numpy
数组来实现。你知道吗这里,
a
和b
是数组ab
上的“视图”,修改一个视图也会修改另一个视图。你知道吗从
a
和b
开始,只需从a+b
创建一个numpy数组,并相应地重新定义a
和b
:相关问题 更多 >
编程相关推荐