gpt4 book ai didi

python - 检查列表中是否有元素

转载 作者:太空宇宙 更新时间:2023-11-03 16:00:56 24 4
gpt4 key购买 nike

我想检查函数中通过参数给出的字符串是否在列表中。代码本身不会产生任何错误,但它的工作方式是错误的。如果我将函数“-a”作为参数,它仍然表示它不在列表中,但它肯定在列表中。

这是代码:

def generatePassword(pLength, mode):
password = str()
commands = ["-a", "-n", "-s", "-allupper", "-mixupper"]
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
"q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
specialCharacters = ["!", "?", "&", "@",
"-", "=", "#", "+", "*", "/", "%", "§"]
if mode.lower().split() not in commands:
print("Couldn't recognize commands...")
else:
for n in range(pLength):
i = random.randint(1, 2)
if "-a" in mode.lower().split():
password += alphabet[random.randint(0, 25)]
print("[+]", password)

generatePassword(30, "-a")

最佳答案

你的状态不好:

if mode.lower().split() not in commands:
print("Couldn't recognize commands...")

将其替换为(例如):

args = set(mode.lower().split())
if not set(args).issubset(set(commands)):
print("Couldn't recognize commands...")

http://python.6.x6.nabble.com/Test-if-list-contains-another-list-td1506964.html

关于python - 检查列表中是否有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40339856/

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