如何从列表中删除并排出现在另一个列表中的项目?

时间:2019-10-13 12:13:29

标签: python list for-loop

grt=["thousand","million","billion","trillion","quadrillion","quintillion","sextillion","septillion","octillion","nonillion","decillion","undecillion","duodecillion","tredecillion","quattuor-decillion","quindecillion","sexdecillion","septen-decillion","octodecillion","novemdecillion","vigintillion"]


e=["sixty","million","billion","trillion","quadrillion","quintillion","sextillion","septillion","octillion"]

def sep():
    global e
    global grt

    for i in range(1,len(e)):
        if (e[i] in grt and e[i-1] in grt)==True:
            e.remove(e[i])

while all(((i in grt) and (e[e.index(i)-1] in grt)) for i in e[1:len(e)])!=False:
    sep()
print(e)

我希望最后一条语句print(e)会打印[“ sixty”],但不会打印出来。

1 个答案:

答案 0 :(得分:0)

过滤功能非常适合此类应用。


grt=["thousand","million","billion","trillion","quadrillion","quintillion","sextillion","septillion","octillion","nonillion","decillion","undecillion","duodecillion","tredecillion","quattuor-decillion","quindecillion","sexdecillion","septen-decillion","octodecillion","novemdecillion","vigintillion"]


e=["sixty","million","billion","trillion","quadrillion","quintillion","sextillion","septillion","octillion"]

results = list(filter(lambda x:  x not in grt, e))
print(results)