python 删除任意值
发布网友
发布时间:2022-05-21 09:56
我来回答
共1个回答
热心网友
时间:2023-10-22 20:46
import random
test=['zhangsan','lisi','wangwu','zhaoliu','guoer','heihei','gaga']
#---------------------------
def removeItem(source, count):
sample = random.sample(source,count)
for item in sample:
source.remove(item)
print('Remove: ', sample)
return source
#---------------------------
count = len(test)
if count%2 ==1:
test = removeItem(test,1)
while len(test)!=2:
test = removeItem(test,2)
print('Remain: ', test)
输出结果:
Remove: ['gaga']
Remove: ['zhangsan', 'wangwu']
Remove: ['lisi', 'guoer']
Remain: ['zhaoliu', 'heihei']