[英]How can i move more than one item from one list to another in Python
I would like to move more than one item from one list to another. 我想将一个以上的项目从一个列表移到另一个。
list1 = ['2D',' ',' ',' ',' ',' ',' ',' ',' ']
list2 = ['XX','XX','5D','4S','3D',' ',' ',' ',' ']
list3 = ['XX','XX','XX','8C','7H','6C',' ',' ',' ']
In the code above ' '
is a double space 在上面的代码中, ' '
是一个双空格
I would like to be able to move '5D','4S','3D'
from list2
onto '8C','7H','6C'
in list3
. 我希望能够将'5D','4S','3D'
从list2
移到list3
'8C','7H','6C'
。
I've tried the code below but it doesn't work. 我已经尝试了下面的代码,但是它不起作用。
list1 = ['2D',' ',' ',' ',' ',' ',' ',' ',' ']
list2 = ['XX','XX','5D','4S','3D',' ',' ',' ',' ']
list3 = ['XX','XX','XX','8C','7H','6C',' ',' ',' ']
items_to_be_moved = list2[list2.index('XX')+2 : list2.index(' ')]
list3[list3.index(' ')] = items_to_be_moved
del list2[list2.index('XX')+2 : list2.index(' ')]
print('list2',list2)
print('list3',list3)
and this returns 然后返回
list2 ['XX', 'XX', ' ', ' ', ' ', ' ']
list3 ['XX', 'XX', 'XX', '8C', '7H', '6C', ['5D', '4S', '3D'], ' ', ' ']
However, i dont want to use list2.index('XX')+2
, i would like to use a code that gives the last index of 'XX'
just like list2.index(' ')
gives the first index of ' '
. 但是,我不想使用list2.index('XX')+2
,我想使用给出list2.index(' ')
的最后一个索引为'XX'
的代码,就像list2.index(' ')
给出的第一个索引为' '
。
Also, i dont want the moved items to be in a separate list of their own within another list. 另外,我不希望移动的项目在另一个列表中位于它们自己的单独列表中。 For example: instead of returning 例如:而不是返回
"list3 ['XX', 'XX', 'XX', '8C', '7H', '6C', ['5D', '4S', '3D'], ' ', ' ']"
the list 名单
"list3 ['XX', 'XX', 'XX', '8C', '7H', '6C','5D', '4S', '3D', ' ', ' ']"
should be returned. 应该退货。
To replace the correct items, use slice assignment. 要替换正确的项目,请使用切片分配。
list1 = ['2D',' ',' ',' ',' ',' ',' ',' ',' ']
list2 = ['XX','XX','5D','4S','3D',' ',' ',' ',' ']
list3 = ['XX','XX','XX','8C','7H','6C',' ',' ',' ']
list3[3:6] = list2[2:5]
print list3
# ['XX', 'XX', 'XX', '5D', '4S', '3D', ' ', ' ', ' ']
If you want to start from after the final consecutive 'XX'
, you can use: 如果要从最后一个连续的'XX'
之后开始,则可以使用:
from itertools import takewhile
i = sum(1 for _ in takewhile(lambda elem: elem == 'XX', list3))
list3[i:i+3] = list2[i-1:i+2]
to find the index of the last one. 查找最后一个的索引。
well if all of the 'xx' are always before the needed there is another way (beside the one suggested by @turek) using 'list.count('xx')' and the slicing : 好吧,如果所有的“ xx”总是在所需的前面,那么还有另一种方法(@turek建议的方法除外)使用“ list.count('xx')”和切片:
list[count-1:count+2]
you can use the slice operator to do the insert also I would probably iterate like suggested by @intra-c 您可以使用slice运算符进行插入,我也可能会像@ intra-c所建议的那样进行迭代
First, here's how to find the last index . 首先,这是找到最后一个索引的方法 。 But beware, if you get the index number of the last copy of XX
, what if the list reads, eg, ['XX', '1D', 'XX', '4S']
so that there's a valid item in the middle? 但是请注意,如果您获得了XX
的最后一个副本的索引号,那么如果列表显示为['XX', '1D', 'XX', '4S']
那么中间会有一个有效项?
Anyway, once you have a span that you want to move from one list to another, you need only use the slice operator to do the insert and delete: 无论如何,一旦您有一个跨度要从一个列表移到另一个列表,则只需要使用slice运算符进行插入和删除:
>>> list2[2:5]
['5D', '4S', '3D']
>>> list3[6:6] = list2[2:5]
>>> list3
['XX', 'XX', 'XX', '8C', '7H', '6C', '5D', '4S', '3D', ' ', ' ', ' ']
You can use del
to remove from list2, or you can just assign to the slice: 您可以使用del
从list2中删除,也可以只分配给切片:
>>> list2[2:5] = []
>>> list2
['XX', 'XX', ' ', ' ', ' ', ' ']
Try looping through with 尝试遍历
for item in items_to_be_moved:
# Add items one at a time
On the second topic: 关于第二个主题:
Don't think there's a function for finding last occurance in lists, but you could either do a loop 不要以为在列表中没有找到最后出现的函数,但是您可以执行循环
pos = 0
while 1:
try:
pos = list3.index('XX',pos+1)
except ValueError:
break
if pos == 0 and list3[0] != 'XX':
#Do something to pos if there was no XX in list...
or reverse the list in-place do the index-thing reverse it back and recalc the position. 或就地反转列表,将index-thing反向反转并重新计算位置。
list3.reverse()
pos = list3.index('XX')
list3.reverse()
pos = len(list3) - pos - 1
Don't forget about negative indices for slicing from the end: 不要忘了从最后开始切片的负索引:
i2_begin = -list2[::-1].index('XX')
i2_end = list2.index(' ')
i3 = list3.index(' ')
list3[i3:i3] = list2[i2_begin:i2_end]
del list2[i2_begin:i2_end]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.