【发布时间】:2020-04-16 01:55:27
【问题描述】:
我需要设计一种方法来比较三个列表中的所有数字,并且如果所有三个列表中都存在一个数字 - 我必须将它添加到 matching_numbers 列表中。如果一个数字与其他任何数字都不匹配,那么我必须将它添加到 unique_numbers 列表中。我尝试使用 for 循环,但我只能完成等式的一半,而且我不确定如何将所有不匹配的数字添加到 unique_numbers 列表中。我也不希望我的 matching_numbers 或 unique_numbers 列表中有任何重复项。
list_1 = []
list_2 = []
list_3 = []
matching_numbers = []
unique_numbers = []
countone = 0
counttwo = 0
countthree = 0
import random
name = input("Hello USER. What will your name be?")
print("Hello " + name + ". Welcome to the NUMBERS program.")
amountone = int(input("How many numbers do you wish to have for your first list? Please choose from between 1 and 15."))
while countone != amountone:
x = random.randint(1, 50)
list_1 += [x,]
print(list_1)
countone += 1
amounttwo = int(input("For your second list, how many numbers do you wish to have? Please choose from between 1 and 15."))
while counttwo != amounttwo:
x = random.randint(1, 50)
list_2 += [x,]
print(list_2)
counttwo += 1
amountthree = int(input("For your third list, how many numbers do you wish to have? Please choose from between 1 and 15."))
while countthree != amountthree:
x = random.randint(1, 50)
list_3 += [x,]
print(list_3)
countthree += 1
for a in list_1:
for b in list_2:
for c in list_3:
if a == b and b == c:
matching_numbers = list(set(list_1) & set(list_2) & set(list_3))
else:
unique_numbers =
【问题讨论】:
标签: python python-3.x list concatenation