python - 获取列表中单词中字母的索引 - 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)
解决方案
您可以使用startswith
list_1 = [x for x in words if x.startswith('a')]
推荐阅读
- arrays - Storing certain array content in a variable
- e2e-testing - 发生 500 xhr 请求时,cypress 测试失败
- javascript - 如何在 react-bootstrap 选择选项中使用翻译
- module - 模块的 NestJs 版本
- api - 为什么我无法显示来自 YouTube 直播的嵌入视频?
- angular - 删除本地文件 Ionic 4
- uml - 如何为无需登录的用户创建一个类?
- c# - 如何在 WebApi 中将 Get 请求转换为 Post 请求?
- java - 在 Intellij 2019.1 中使用 Open JDK 11.0.2 构建 SpringBoot 项目失败
- scala - 在惰性流中保留共享意味着什么?