gpt4 book ai didi

python - 与 python 中每个确切位置的列表进行比较

转载 作者:太空宇宙 更新时间:2023-11-03 18:43:46 25 4
gpt4 key购买 nike

我有 2 个列表:

Gigits_ListGuesses_list

我需要比较它们并找到哪里有公牛和奶牛(就像真正的游戏一样)

例如:如果一个列表是 ['1', '3', '4', '6'] 而第二个列表是 ['2', '3' ,'6','4']。所以是“2C 1B”2头牛和1头牛

    #setting the secret length
Secret_Length = int(raw_input("the secret length"))

#setting the secret base
Secret_Base = int(raw_input("secret base_between 6-10"))

#getting the secret from the user
Secret = str(raw_input("enter the secret"))

#checking if the secret in the right length
if (int(len(Secret)) != Secret_Length):
print "ERROR"
sys.exit()

Gigits_List = []
#checking if the number in the right base
for Each_Digigt in Secret:
Gigits_List.append(Each_Digigt)
if (int(Each_Digigt)>Secret_Base-1):
print "ERROR"
sys.exit

#getting a guess from the user
Guess = str(raw_input("enter the guess"))

Guesses_list = []
for Each_Guess in Guess:
Guesses_list.append(Each_Guess)

最佳答案

list1 = ['1', '2', '3', '3']
list2 = ['1', '3', '3', '3']

cow, bull, removed = 0, 0, 0
for i in range(len(list1)):
if list1[i - removed] == list2[i - removed]:
bull += 1
list1 = list1[:i - removed] + list1[i - removed + 1:]
list2 = list2[:i - removed] + list2[i - removed + 1:]
removed += 1
for i in range(len(list2)):
if list2[i] in list1:
cow += 1
print cow, bull

输出

0 3

关于python - 与 python 中每个确切位置的列表进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20002383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com