简体   繁体   中英

How can I select an item in a list?

I have:

I have two lists like this:

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

I want to (the problem):

Since ELON_MUSK and BARACK_OBAMA is true I want to retrieve them, and append to a string, however I'm pretty sure I don't know how to search the problem in correct terms since I found nothing about this, thus asking here.

I expect to happen:

People in this image: ELON_MUSK BARACK_OBAMA

I'm doing:

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)

I'm getting:

People in this image: 
# 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

I think the main issue with your current approach is that your test for appending is if 'True' in names_with_result and not if True in names_with_result ... 'True' != True ...

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

The first test, 'True' in sample_result returns False, which then won't trigger your appending logic, thus passing over that element.

Note: Your variable 'name_list' is not a list, but a string. Keep that in mind when you want to append to it.

List comprehension is perfect, but if you are learning and want to do it more explicitly, you can loop through each tuple in each list. For each list, you can check if the second argument is true, and if so, add it to the string ('name_list').

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

Try this one:

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)

You can also do this:

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)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM