python - 确定python中两个数据集是否相等的最佳方法?

标签 python python-3.x if-statement conditional-statements simplify

您知道实现与此相同结果的更简单方法吗? 我有这段代码:

color1 = input("Color 1: ")
color2 = input("Color 2: ")

if ((color1=="blue" and color2=="yellow") or (color1=="yellow" and color2=="blue")):
            print("{0} + {1} = Green".format(color1, color2))

我也试过这个:

if (color1 + color2 =="blueyellow" or color1 + color2 =="yellowblue")

最佳答案

您可以使用 set 进行比较。

Two sets are equal if and only if every element of each set is contained in the other

In [35]: color1 = "blue"

In [36]: color2 = "yellow"

In [37]: {color1, color2} == {"blue", "yellow"}
Out[37]: True

In [38]: {color2, color1} == {"blue", "yellow"}
Out[38]: True

关于python - 确定python中两个数据集是否相等的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39781887/

相关文章:

python-3.x - 如何使用 .join() 将字母转换为字符串

python - 在 if 语句中使用 input() 是一种不好的做法吗?

C# 单行如果有 void 返回

python - 这可以写成 python reduce 函数吗?

javascript - 如何根据具有不同前缀的其他字符串重命名字符串?

python - 将列表中的任何列表分配给字符串脚本。

java - 根据输入询问变量数量(for/while 语句?)

python - 如何在Python中将json文件解析为DataFrame

python - 使用 Pygame 在 Python 游戏中随机显示图像

python - 在 Django 中过滤外键