判断列表中相同的元素

  1. 列表中的值为字符串:
list_a = ['False', 'False', 'False', 'True', 'False', 'False', 'True']
list_b = ['True', 'False',  'False', 'False', 'True', 'False', 'True']

list_a1 = []
for i in list_a:
    if i == 'False':
        list_a1.append('0')
    else:
        list_a1.append('1')
print('list_a1:', list_a1)

list_b1 = []
for i in list_b:
    if i == 'False':
        list_b1.append('0')
    else:
        list_b1.append('1')
print('list_b1', list_b1)

list_same = []
for j in range(len(list_a1)):
    if list_a1[j] == list_b1[j]:
        list_same.append('0')
    else:
        list_same.append('1')
print('list_same', list_same)

'''
print:
list_a1: ['0', '0', '0', '1', '0', '0', '1']
list_b1 ['1', '0', '0', '0', '1', '0', '1']
list_same ['1', '0', '0', '1', '1', '0', '0']
'''
  1. 列表中的值为布尔:
    list_image_2 = []
    for i in range(len(list_image)):
        if list_image[i] == False:
            list_image_2.append('1')
        else:
            list_image_2.append('0')

猜你喜欢

转载自blog.csdn.net/everyxing1007/article/details/126570312
今日推荐