在搜索字符串时,如何从列表列表中返回列表,并强制将比较与小写列表?
我有这样的列表:
allTeams = [ [57, 'Arsenal FC', 'Arsenal', 'ARS'], [58, 'Aston Villa FC', 'Aston Villa', 'AVL'], [61, 'Chelsea FC', 'Chelsea', 'CHE'], ...]
userIsLookingFor = "chelsea"
for team in allTeams:
if userIsLookingFor.lower() in any_element_of_team.lower():
print(team)
> [61, 'Chelsea FC', 'Chelsea', 'CHE']
我基本上会在列表列表中查找用户的请求单词,如果有匹配项,我会打印该列表。在上面的情况下,用户搜索“切尔西”,在其中一个列表中,“切尔西”(切尔西FC或切尔西都没有关系)。因此,我将返回该特定列表。
我尝试使用“任何”,但它似乎只返回布尔值,而我实际上无法从中打印任何列表。
I have a list of lists as such:
allTeams = [ [57, 'Arsenal FC', 'Arsenal', 'ARS'], [58, 'Aston Villa FC', 'Aston Villa', 'AVL'], [61, 'Chelsea FC', 'Chelsea', 'CHE'], ...]
userIsLookingFor = "chelsea"
for team in allTeams:
if userIsLookingFor.lower() in any_element_of_team.lower():
print(team)
> [61, 'Chelsea FC', 'Chelsea', 'CHE']
I would basically look for the user's requested word in a list of lists, and if there's a match, I print that list. In the case above, the user searches for "chelsea" and in one of the lists, there's a match for "chelsea" (either Chelsea FC or Chelsea, doesn't matter). So I would return that specific list.
I tried using "any" but it seems to only return a boolean and I can't actually print any list from it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用列表理解:
输出:
You can use a list comprehension:
output: