[英]Python: how to copy between lists using lists, e.g. list1[index1] = list2[index2]
Say we have two lists of values, list1
and list2
, and tow lists of indices, index1
and index2
.假设我们有两个值列表list1
和list2
,以及两个索引列表index1
和index2
。 How do we copy elements with position index2
in list2
to elements with position index1
in list1
.我们如何将list2
位置为index2
元素复制到list1
位置为index1
元素。 In Matlab, for example, we can simply write list1[index1] = list2[index2]
, but strangely this does not seem to work in Python.例如,在 Matlab 中,我们可以简单地编写list1[index1] = list2[index2]
,但奇怪的是,这在 Python 中似乎不起作用。 Also, how do we do this if index1
and/or index2
is a list of booleans?另外,如果index1
和/或index2
是布尔值列表,我们该怎么做?
For example, say list1 = ['a','b','c','d','e']
and list2 = ['A','B','C','D','E']
and index1 = [0,1,3]
and index2 = [1,2,4]
with the result of list1[index1] = list2[index2]
being list1: ['B','C','c','E','e']
.例如,说list1 = ['a','b','c','d','e']
和list2 = ['A','B','C','D','E']
和index1 = [0,1,3]
和index2 = [1,2,4]
与list1[index1] = list2[index2]
是list1: ['B','C','c','E','e']
。
In the case of boolean, the same output should be achived with index1 = [True,True,False,True,False]
and index2 = [False,True,True,False,True]
.在布尔值的情况下,应该使用index1 = [True,True,False,True,False]
和index2 = [False,True,True,False,True]
。
PS: It is clear how to do this with a for
loop. PS:很清楚如何使用for
循环来做到这一点。 I am looking for a more "pythonic" way, eg list comprehension.我正在寻找一种更“pythonic”的方式,例如列表理解。 Thank you谢谢
PPS: After some experimentation, I defined a class MyList
which inhereted from class Python's list
and modified __getitem__
and __setitem__
to accept lists of integers or booleans. PPS:一些实验后,我定义的类MyList
从类Python的inhereted list
和修改__getitem__
和__setitem__
接受整数或布尔的列表。 The class definition is given in my post here How to extend list class to accept lists for indecies in Python, eg to use list1[list2] = list3[list4]类定义在我的帖子中给出了如何扩展列表类以接受 Python 中的 indecies 列表,例如使用 list1[list2] = list3[list4]
You can use a simple for-loop with enumerate
:您可以在enumerate
使用简单的 for 循环:
list1 = ['a','b','c','d','e']
list2 = ['A','B','C','D','E']
index1 = [0,1,3]
index2 = [1,2,4]
for i, val in enumerate(index1):
list1[val] = list2[index2[i]]
print(list1)
Output:输出:
['B', 'C', 'c', 'E', 'e']
Or alternatively with zip
:或者使用zip
:
list1 = ['a','b','c','d','e']
list2 = ['A','B','C','D','E']
index1 = [0,1,3]
index2 = [1,2,4]
for a, b in zip(index1, index2):
list1[a] = list2[b]
print(list1)
Output:输出:
['B', 'C', 'c', 'E', 'e']
As far as I can see, the boolean case is a completely different one, and should be asked in a separate question.据我所知,布尔情况是一个完全不同的情况,应该在一个单独的问题中提出。
Use zip
and combine the elements of the two index lists.使用zip
并组合两个索引列表的元素。
list1 = ['a', 'b', 'c', 'd', 'e']
list2 = ['A', 'B', 'C', 'D', 'E']
index1 = [0, 1, 3]
index2 = [1, 2, 4]
for i1, i2 in zip(index_1, index_2):
list1[i1] = list2[i2]
print(list1)
list1 = ['a','b','c','d','e']
list2 = ['A','B','C','D','E']
index1 = [0,1,3]
index2 = [1,2,4]
x=0
for i in index1:
list1[i] = list2[index2[x]]
x+=1
print(list1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.