获取列表和元组列表的交叉点

mon*_*top 2 python tuples intersection list

两个清单

ListOne = ['steve','rob','jym','rich','bell','mick']
ListTwo = [('steve',22 ,178), ('rob', 23 ,189), ('jym', 25,165), ('steven',18 ,187), ('Manro',16 ,200), ('bell',21 ,167), ('mick', 24 ,180)]
Run Code Online (Sandbox Code Playgroud)

我怎样才能从ListOne中获得ListTne的数据,就像是ListOne上的学生一样two list intersections.

输出如:

ListTwo = [('steve',22 ,178), ('rob', 23 ,189), ('jym', 25,165), ('bell',21 ,167), ('mick', 24 ,180)]
Run Code Online (Sandbox Code Playgroud)

我尝试过这个,但我正在寻找一些更负责任的东西:

for row in ListTwo:   
    if row[0] in ListOne :
        print 'this student exist' + `row[0]`

    else :
        for i,e in enumerate(ListTwo):
        #Check the student with the above results, will be removed
            if row[0] == e: 
                temp=list(ListTwo[i])
                pprint.pprint('I will remove this student : ' + `e`)
                #Remove the Student
                for f, tmp in enumerate(temp):
                     temp[f]= []
                #pprint.pprint(temp)
                ListTwo[i]=tuple(temp)
Run Code Online (Sandbox Code Playgroud)

shx*_*hx2 6

使用列表理解:

[rec for rec in ListTwo if rec[0] in ListOne]
Run Code Online (Sandbox Code Playgroud)

为了使其更快,你可以先在列表转换为替代列表,查找与设定查找,设定:

ListOne = set(ListOne)
Run Code Online (Sandbox Code Playgroud)