检查在二维列表中输入的值是否与所有行的同一列中的任何其他值匹配



python帮助,我试图让用户输入一个int值,并将其与2d列表中的所有其他值进行比较。如果新的输入值已经在同一列但在不同的行,我想让用户租用一个值,直到他们输入一个唯一的值。


num_list = [
[1, 2, 3, 4, 5, 6, 7],
[11, 12, 13, 14, 15, 16, 17],
[21, 22, 23, 24, 25, 26, 27]]
num_list.extend([[0] * 7])
rows = (len(num_list))-1
num_list[rows][0] = input("Enter Numbern")
# check if their input is already in the list at the same col index 

我想我解决了它,

for col in range(1):
# check the "above" col values; `:-1` means all rows except last
other_col_values = [row[col] for row in num_list[:-1]]
while True:
new_num = int(input("enter: "))
# if `new_num` is distinct from *all* others, break the while, stop asking
if all(new_num != ocv for ocv in other_col_values):
break
# if we're here, `new_num` can be safely put; so we do that
num_list[rows][col] = new_num

相关内容