gpt4 book ai didi

python 检查列表中是否存在值

转载 作者:行者123 更新时间:2023-11-30 23:13:28 24 4
gpt4 key购买 nike

无法得到正确的结果。我想检查列表的内容以获取一组值。我不应该陷入条件 if 因为该值不存在。

以下列表包含:['pm_pmdo', 'pm_pmco', 'shv', 'dsv', 'pmv']

检查以下内容:she、dse、pme

里面找到一个?

class newfunc(object):

def testfunction(self):

self.DSE = 'dse'
self.PME = 'pme'
self.SHE = 'she'

self.users_roles = ['pm_pmdo', 'pm_pmco', 'shv', 'dsv', 'pmv']

print 'Following list contains:'
print self.users_roles
print 'Checking for the following:'
print self.SHE
print self.DSE
print self.PME

if (self.DSE or self.PME or self.SHE for s in self.users_roles):
#if (self.DSE or self.PME or self.SHE) in self.users_roles:
print "inside found one"
else:
print "outside none found"

if __name__ == '__main__':
runtime = newfunc()
runtime.testfunction()

最佳答案

if any(role in self.users_roles for role in (self.SHE, self.PME, self.SHE)):

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

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