如何选择列表中的项目?

时间:2018-12-01 16:23:13

标签: python list select

我有

我有两个这样的列表:

[('ELON_MUSK', True), ('BARACK_OBAMA', False), ('DONALD_TRUMP', False)]
[('ELON_MUSK', False), ('BARACK_OBAMA', True), ('DONALD_TRUMP', False)]

我要(问题所在):

由于ELON_MUSKBARACK_OBAMAtrue,因此我想检索它们并附加到字符串中,但是我很确定我不知道如何在其中搜索问题正确的用语,因为我对此一无所获,因此在这里询问。

我希望发生:

People in this image: ELON_MUSK BARACK_OBAMA

我正在做

for imagePath in imageArray:
        # Try comparing an unknown image
        unknown_image = face_recognition.load_image_file(imagePath)
        unknown_face = face_recognition.face_encodings(unknown_image)
        face_count = len(unknown_face)
        name_list = ""
        print("Checking: " + imagePath)
        print("----------------------------")
        for i in range(face_count):
                result = face_recognition.compare_faces(face_encodings, unknown_face[i])
                # Print the result as a list of names with True/False
                names_with_result = list(zip(face_names, result))
                print(names_with_result, end = '')
                print(" -- Checking face #" + str(i+1))
                # vvv I HAVE NO IDEA ABOUT THIS PART vvv
                if "True" in names_with_result:
                        #name_list = name_list + " name of the TRUE person";
        print("People in this image: " + name_list)

我得到:

People in this image: 

4 个答案:

答案 0 :(得分:2)

# Separate lists of (name, is_in_image) tuples
>>> a = [('ELON_MUSK', True), ('BARACK_OBAMA', False), ('DONALD_TRUMP', False)]
>>> b = [('ELON_MUSK', False), ('BARACK_OBAMA', True), ('DONALD_TRUMP', False)]
# Combine the lists
>>> together = a + b
# Create a list containing all names if the second element (is_in_image) is True
>>> [name for name, is_in_image in together if is_in_image]
['ELON_MUSK', 'BARACK_OBAMA']
>>> print('People in this image: {}'.format(', '.join([name for name, is_in_image in together if is_in_image])))
People in this image: ELON_MUSK, BARACK_OBAMA

我认为您当前使用的方法的主要问题是您添加的测试是if 'True' in names_with_result而不是if True in names_with_result ... 'True' != True ...

>>> sample_result = ('ELON_MUSK', True)
>>> 'True' in sample_result
False
>>> True in sample_result
True

第一个测试'True' in sample_result返回False,然后它不会触发附加逻辑,从而跳过了该元素。

答案 1 :(得分:0)

注意:您的变量“名称列表”不是列表,而是字符串。当您想附加到它时,请记住这一点。

列表理解是完美的,但是如果您正在学习并且想更明确地做到这一点, 您可以遍历每个列表中的每个元组。对于每个列表,您可以检查第二个参数是否为true,如果是,则将其添加到字符串('name_list')。

for tup in l1:
    if tup[1]:
        name_list += tup[0]

答案 2 :(得分:0)

尝试这个:

A= [('ELON_MUSK', True), ('BARACK_OBAMA', False), ('DONALD_TRUMP', False)]
B= [('ELON_MUSK', False), ('BARACK_OBAMA', True), ('DONALD_TRUMP', False)]


name_list  = ''.join([a[0]+' , '+b[0] for a in A for b in B if a[1]==True and b[1]== True])
print("People in this image: "+ name_list)

答案 3 :(得分:0)

您也可以这样做:

l1 = [('ELON_MUSK', True), ('BARACK_OBAMA', False), ('DONALD_TRUMP', False)]
l2 = [('ELON_MUSK', False), ('BARACK_OBAMA', True), ('DONALD_TRUMP', False)]

# join the two list
l1.extend(l2)

# create a simple function that return a list of true

f = lambda x: [i for i,j in x if j]

print('{} is not {}'.format(*f(l1)))