【发布时间】:2020-11-19 06:47:59
【问题描述】:
这是我最近制作的一个程序。这段代码的目标是一对对应的列表。所以randomStringpt1[0] 对应于randomStringpt2[0]。我想将randomStringpt1[0] 和randomString2[0] 与用户在随机字符串中提供的其余对进行比较。但是在使用这段代码之后,看起来我已经多次复制了每一对,这与我所寻找的相反。我正在考虑使用字典,但后来意识到字典键只能有一个值,如果用户两次使用数字,这对我的情况没有帮助。有谁知道我可以如何减少重复?
(我一直在运行的测试编号为randomStringpt1 = [1,3,1,1,3] 和randomStringpy2 = [2,4,2,3,4]
)
randomStringpt1 = [1, 2, 3, 4, 5] #Pair of strings that correspond to each other("1,2,3,4,5" doesn't actually matter)
randomStringpt2 = [1, 2, 3, 4, 5]
for i in range(len(randomStringpt1)):
randomStringpt1[i] = input("Values for the first string: ")
randomStringpt2[i] = input("Corresponding value for the second string: ")
print(randomStringpt1) #numbers that the user chose for the first number of the pair
print(randomStringpt2) #numbers that the user chose for the second number of the pair
newStart = []
newEnd = []
for num1 in range(len(randomStringpt1)):
for num2 in range(len(randomStringpt1)):
if (int(randomStringpt1[num1]) != int(randomStringpt1[num2]) and int(randomStringpt2[num1]) != int(randomStringpt2[num2])):
newStart.append(randomStringpt1[num1]) # Adding the pairs that aren't equal to each other to a new list
newEnd.append(randomStringpt2[num1])
newStart.append(randomStringpt1[num2])
newEnd.append(randomStringpt2[num2])
# else:
# print("The set of numbers from the randomStrings of num1 are not equal to the ones in num2")
print(newStart)
print(newEnd)
【问题讨论】:
标签: python-3.x list duplicates