如果有人可以帮助我使用Python代码: 如果我输入一个字母,如何打印所有以该字开头的单词?
答案 0 :(得分:2)
print [word for word in words if word.startswith(letter)]
答案 1 :(得分:1)
有很多方法可以做到这一点,例如:
words = ["zwei", "peanuts", "were", "walking", "down", "the", "strasse"]
letter = "w"
output = [x for x in words if x[0] == letter]
output
的内容将是:
['were', 'walking']
一些注意事项: