相关疑难解决方法(0)

如何针对值测试多个变量?

我正在尝试创建一个函数,将多个变量与一个整数进行比较,并输出一个由三个字母组成的字符串.我想知道是否有办法将其翻译成Python.所以说:

x = 0
y = 1
z = 3
mylist = []

if x or y or z == 0 :
    mylist.append("c")
if x or y or z == 1 :
    mylist.append("d")
if x or y or z == 2 :
    mylist.append("e")
if x or y or z == 3 : 
    mylist.append("f")
Run Code Online (Sandbox Code Playgroud)

这将返回一个列表

["c", "d", "f"]
Run Code Online (Sandbox Code Playgroud)

这样的事情可能吗?

python comparison boolean-logic if-statement match

594
推荐指数
19
解决办法
19万
查看次数

str.startswith,带有要测试的字符串列表

我试图避免使用如此多的if语句和比较,只是使用一个列表,但不知道如何使用它str.startswith:

if link.lower().startswith("js/") or link.lower().startswith("catalog/") or link.lower().startswith("script/") or link.lower().startswith("scripts/") or link.lower().startswith("katalog/"):
    # then "do something"
Run Code Online (Sandbox Code Playgroud)

我希望它是:

if link.lower().startswith() in ["js","catalog","script","scripts","katalog"]:
    # then "do something"
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.

python string list

154
推荐指数
2
解决办法
11万
查看次数

什么是蟒蛇中的Truthy和Falsy?它与真与假有什么不同?

我刚刚才知道有TruthyFalsy在Python这是与正常不同的价值观TrueFalse

是否有人可以深入解释什么truthyfalsy值?

我应该在哪里使用它们?

truthyTrue值以及falsyFalse值之间有什么区别?

python

92
推荐指数
4
解决办法
3万
查看次数

标签 统计

python ×3

boolean-logic ×1

comparison ×1

if-statement ×1

list ×1

match ×1

string ×1