python - 查找列表中的每个第 n 个元素

标签 python

如何找到列表中的每第 n 个元素?

对于列表[1,2,3,4,5,6]returnNth(l,2) 应该返回[1,3, 5] 对于列表 ["dog", "cat", 3, "hamster", True]returnNth(u,2) 应该返回[‘狗’,3,真]。我该怎么做?

最佳答案

你只需要 lst[::n]

例子:

>>> lst=[1,2,3,4,5,6,7,8,9,10]
>>> lst[::3]
[1, 4, 7, 10]
>>> 

关于python - 查找列表中的每个第 n 个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14680273/

相关文章:

python - 如何让程序根据答案列表要求输入一个或两个答案

python - 如何在 python/pandas 中进行左内连接?

python - 四倍精度特征值、特征向量和矩阵对数

python - 使用 SQLAlchemy ORM 批量插入

python - 转换数据帧(转置)

python - 用于查找首字母缩略词并避免由该模式组成的单词的正则表达式

python - 检查 Makefile 中是否存在 conda 环境

python - Django 重置密码电子邮件未发送

python - 如何创建具有多个线程的全局列表

python - 具有高级字符的非贪婪模式