如何选择列表中的项目?

图伯克·卡恩·杜曼

我有:

我有两个这样的列表:

[('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: 
黑网站
# 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,然后不会触发您的附加逻辑,从而传递该元素。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章