首页 > 解决方案 > 我正在编写一个基本的 python 程序来检查字符串是否出现在字符串列表中

问题描述

如何检查python列表中是否存在字符串?

with open('pwds.csv', newline='\n') as csvfile:
global passwordlist
passwords = csv.reader(csvfile,delimiter=',')
passwordlist = []
for row in passwords:
    global passwordlist
    passwordlist = list(passwords)
    map(lambda passwordlist: str.replace ("['", ""), passwordlist)
    print (passwordlist)

我正在尝试将数据存储在 CSV 文件中,然后在 python 中读取它以查看它是否包含用户输入中给出的字符串。目前,即使我让程序打印列表以确保字符串确实存在,程序也总是无法在列表中找到输入的字符串。我只是迷路了。这是一项针对扩展工作的 GCSE 计算挑战任务。

标签: pythonpython-3.x

解决方案


mylist = ["a", "b", "c"]
if a in mylist:
    #do something if "a" is in the list

如果列表真的很大:

mylist = ["a", "b", "c"]
myset = set(mylist)
if a in mylist:
    #do something if "a" is in the list

推荐阅读