我有一个很长的列表,其中有很多单词,我想创建一个新的列表,只包含以“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)
xxls0lw81#
你可以用 startswith ```list_1 = [x for x in words if x.startswith('a')]
startswith
7rfyedvj2#
尝试:
list_1= [word for word in words if word[0] =='a']
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']
9jyewag04#
old_list = ['hello', 'world', 'alike', 'algorithm'] new_list = [i for i in old_list if i.startswith('a')]
现在打印新的_列表将给出['like','algorithm']
aelbi1ox5#
看看这个
for i in range(len(words) - 1): if words[0] == "a": list_1.append(words) print(list_1)
5条答案
按热度按时间xxls0lw81#
你可以用
startswith
```list_1 = [x for x in words if x.startswith('a')]
7rfyedvj2#
尝试:
nlejzf6q3#
方法1-
方法2
9jyewag04#
现在打印新的_列表将给出['like','algorithm']
aelbi1ox5#
看看这个