4 回答
TA贡献1821条经验 获得超6个赞
使用列表理解 startswith
[i for i in mylist if i.startswith(letter)]
它与您正在做的类似,但您没有检查如下条件
mylist = ["hello","world","how","you","doing"]
for word in mylist:
if word[0]==letter:
print(word)
对.lower()列表中存在的字符串进行安全使用
mylist = ["Hello","world","how","you","doing"]
letter = 'h'
[i for i in mylist if i.lower().startswith(letter)] #["Hello","how"]
TA贡献1810条经验 获得超4个赞
mylist = ["hello","world","how","you","doing"]
letter = input("input a letter you would like to search ").lower()
input a letter you would like to search 'h'
list_starting_with_letter = [i for i in mylist if i[0].lower()==letter.lower()]
list_starting_with_letter
['hello', 'how']
print('Number of such words: '+ str(len(list_starting_with_letter)))
Number of such words: 2
TA贡献1859条经验 获得超6个赞
for word in list:
if input() in word:
print(word)
在那条线上的东西。它从列表中获取单词,然后针对该单词测试用户输入。
添加回答
举报