比较两个列表的元素和顺序 python3
Compare elements AND order of two lists python3
import random
colors = ['blue', 'yellow', 'green', 'orange', 'black']
random_colors = (random.choices(colors, k=4))
print(f'\nWelcome to the color game! your color options are: \n{colors}\n')
userinput = []
for i in range(0, 4):
print("Enter your color choice: {}: ".format(i+1))
userchoice = str(input())
userinput.append(userchoice)
def compare():
set_colors = set(random_colors)
set_userin = set(userinput)
result = set_userin - set_colors
print(result)
compare()
我想将 random_colors 集与用户输入集进行比较。如果用户在 random_colors 集中输入了错误的颜色或颜色位置,我想指定颜色是在错误的位置还是不在 random_colors 集中。
我创建的比较函数不检查顺序。
例如。最终结果:
random_colors=橙蓝蓝黑
userinput = 橙蓝黄蓝
预期 - 'yellow is a wrong color' 和 'blue is wrong position'
我还没有开始打印,因为我不确定如何比较这些集合。
set
不保留顺序,因此您无法判断用户输入的位置是否正确;您可以改用 list
。此外,您可以使用 zip
来判断两个元素是否处于同一位置。尝试以下操作:
import random
colors = ['blue', 'yellow', 'green', 'orange', 'black']
random_colors = random.choices(colors, k=4)
user_colors = [input(f"Color {i+1}: ") for i in range(4)]
for u, r in zip(user_colors, random_colors):
if u not in random_colors:
print(f"{u} is a wrong color.")
elif u != r:
print(f"{u} is in a wrong position.")
在 Python 中,set
不保留其元素的顺序。取而代之的是,您可以尝试将它们作为列表进行比较:
def compare(colors, userin):
# Iterate over the index and value of each color
# in the user input.
for idx, color in enumerate(userin):
# Check if the color is not the same as the color
# at the same position in the random colors.
if color != colors[idx]:
# If the color exists at some other position in
# random colors, then print that the color is
# in the wrong position.
if color in colors:
print(f"{color} is wrong position")
# Is the color is not present in the list of random
# colors then print that the color is a wrong color
else:
print("{color} is a wrong color")
import random
colors = ['blue', 'yellow', 'green', 'orange', 'black']
random_colors = (random.choices(colors, k=4))
print(f'\nWelcome to the color game! your color options are: \n{colors}\n')
userinput = []
for i in range(0, 4):
print("Enter your color choice: {}: ".format(i+1))
userchoice = str(input())
userinput.append(userchoice)
def compare():
set_colors = set(random_colors)
set_userin = set(userinput)
result = set_userin - set_colors
print(result)
compare()
我想将 random_colors 集与用户输入集进行比较。如果用户在 random_colors 集中输入了错误的颜色或颜色位置,我想指定颜色是在错误的位置还是不在 random_colors 集中。 我创建的比较函数不检查顺序。
例如。最终结果:
random_colors=橙蓝蓝黑
userinput = 橙蓝黄蓝
预期 - 'yellow is a wrong color' 和 'blue is wrong position'
我还没有开始打印,因为我不确定如何比较这些集合。
set
不保留顺序,因此您无法判断用户输入的位置是否正确;您可以改用 list
。此外,您可以使用 zip
来判断两个元素是否处于同一位置。尝试以下操作:
import random
colors = ['blue', 'yellow', 'green', 'orange', 'black']
random_colors = random.choices(colors, k=4)
user_colors = [input(f"Color {i+1}: ") for i in range(4)]
for u, r in zip(user_colors, random_colors):
if u not in random_colors:
print(f"{u} is a wrong color.")
elif u != r:
print(f"{u} is in a wrong position.")
在 Python 中,set
不保留其元素的顺序。取而代之的是,您可以尝试将它们作为列表进行比较:
def compare(colors, userin):
# Iterate over the index and value of each color
# in the user input.
for idx, color in enumerate(userin):
# Check if the color is not the same as the color
# at the same position in the random colors.
if color != colors[idx]:
# If the color exists at some other position in
# random colors, then print that the color is
# in the wrong position.
if color in colors:
print(f"{color} is wrong position")
# Is the color is not present in the list of random
# colors then print that the color is a wrong color
else:
print("{color} is a wrong color")