python - 每十个元素从列表中获取两个连续元素

标签 python list list-comprehension

我有一个列表A,我想获取元素 1、2、11、12、21、22 等作为列表。

例如,如果 A = range(100),那么我想要 [0, 1, 10, 11, 20, 21, ..., 90, 91]

现在我有 [num for elem in [[A[i], A[i+1]] for i in range(0, len(A)-1, 10)] for num in elem ]

是否有更简洁或更有效的方法来实现这一点?

最佳答案

[elem for i, elem in enumerate(A) if i %10 in (0, 1)]

关于python - 每十个元素从列表中获取两个连续元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31507813/

相关文章:

python - 在 python 2.6 中创建字典的字典

python - 在 Python 中,SciPY ode.int 的过度工作完成问题

python - 引发 AttributeError : Response content isn't text Scarpy proxy pool. 如何解决?

python - 如何将文本文件中的行附加到 python 3x 中的多个列表

python - 为什么我得到 None 值,并且为什么该数字仍在 Python 中打印?

来自两个列表或 zip 的 python 理解

python - 我真的需要 Visual Studio 2010 for PIP 才能工作吗?或者我可以安装 2013/2015 并且它可以工作吗?

python - Raspberry Pi 照相亭打印

python - 如何从列表中获取最常用的 "n"单词?

Erlang 列表理解、排列