首页 > 解决方案 > 获取列表中单词中字母的索引 - python

问题描述

我有一个很长的列表,里面有很多单词,我想创建一个只包含以“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)

标签: python

解决方案


您可以使用startswith

list_1 = [x for x in words if x.startswith('a')]

推荐阅读