python 获取只包含以“a”开头的单词列表

bvhaajcl  于 2021-09-13  发布在  Python
关注(0)|答案(5)|浏览(875)

我有一个很长的列表,其中有很多单词,我想创建一个新的列表,只包含以“a”开头的单词。

list_1 = []

for i in range(len(words) - 1):
    if words[0: len(words) - 1][0] == "a":
        list_1.append(words)

print(list_1)
xxls0lw8

xxls0lw81#

你可以用 startswith ```
list_1 = [x for x in words if x.startswith('a')]

7rfyedvj

7rfyedvj2#

尝试:

list_1= [word for word in words if word[0] =='a']
nlejzf6q

nlejzf6q3#

方法1-

list_1 = []

for i in range(len(words) - 1):
    if words[i][0] == "a":
        list_1.append(words)

print(list_1)

方法2

list_1= [word for i in words if word[0] =='a']
9jyewag0

9jyewag04#

old_list = ['hello', 'world', 'alike', 'algorithm']
new_list = [i for i in old_list if i.startswith('a')]

现在打印新的_列表将给出['like','algorithm']

aelbi1ox

aelbi1ox5#

看看这个

for i in range(len(words) - 1):
    if words[0] == "a":
        list_1.append(words)

print(list_1)

相关问题