如何返回不区分大小写的字符串fromlist,如果包含不区分大小写的子字符串? 返回的字符串应该是不区分大小写
我能够从列表返回string,但是我使用小写方法。我想在不修改
的情况下将其返回到原始状态entriesList = ['CSS', 'Django', 'Git', 'HTML', 'Python']
substring = "g"
def substringSearch(substring, entriesList):
return [string for string in (string.casefold() for string in entriesList) if substring in string]
print(substringSearch(substring, entriesList))
结果:
['django', 'git']
我想要什么:
['Django', 'Git']
你可以在理解题中只使用lower()
或casefold()
。
[string for string in entriesList if substring in string.casefold()]
您不需要为此创建一个新的生成器。这将给你:
entriesList = ['CSS', 'Django', 'Git', 'HTML', 'Python']
substring = "g"
def substringSearch(substring, entriesList):
return [string for string in entriesList if substring in string.casefold()]
print(substringSearch(substring, entriesList))
# ['Django', 'Git']