python - 如何访问for循环中的下一个值
问题描述
所以我试图在一个句子中获取单词的第一个字母(不包括第一个单词,我已经解决了这个问题)。
但它会在列表中附加空格。
如果您能提供帮助将不胜感激。
这是代码:
lst = []
for t in (input()):
if t == " ":
lst.append(t)
print(*lst, sep="")
输入1:asd dfd yjs
输出1:dy
解决方案
只是这个:
''.join([s[0] for s in input().split()[1:]])
一步步:
如果input()
返回asd dfd yjs
拆分字符串(更多):
input().split() # output: ['asd', 'dfd', 'yjs']
子列表(更多):
input().split()[1:] # output: ['dfd', 'yjs']
一行循环(更多):
[s[0] for s in ['dfd', 'yjs']] # output: ['d', 'y']
子字符串(更多):
s="dfd"
s[0] # output: d
concat 字符串列表(更多):
''.join(['d', 'y']) # output: dy
推荐阅读
- azure-devops - Azure DevOps 使用来自 AWS CodeDeploy 的部署 id 的输出变量作为管道中下一个任务的输入,而不是将变量转换为实际值
- scala - 如何创建一个 ClassTag
> 在 Java 中 - ios - 如何在 ForEach 循环中获取项目的索引?
- vb.net - Datagridview - 将图像插入特定行的单元格
- ubuntu - 将 Ubuntu 14 上的 CKAN 迁移到 Redhat Linux 7
- eigen - 如何将特征块深度复制到向量中?
- python - 当有许多可能的 POS 标签时,查找单词形式的总数
- mysql - 以总库存显示库存中的所有组件
- php - 通过 PHP 执行 shell 命令?
- c++ - 初级 5 版。来自自定义删除函数的 unique_ptr 构造函数