python - 如何将列表中的每 10 个数字除以 10?
问题描述
我有一个包含 100 万个数字的列表,例如:
[1,2,3,4,5,6,7,8,9,100,11,12,13,14,15,16,17,18,19,200,.......]
其中每 10 个数字是应有的 10 倍。我想更正列表,以便每 10 个数字除以 10。
我已经尝试过for i%10==0 then i=i/10
,但它似乎不适用于列表。
解决方案
You can achieve this with a conditional inside a list comprehension, conditioning on the position of the element (via enumerate
):
X = [1,2,3,4,5,6,7,8,9,100,11,12,13,14,15,16,17,18,19,200]
# Quotient on 9 because lists are 0-indexed
y = [x/10. if i%10==9 else x for i, x in enumerate(X)]
print(y)
>>> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10.0, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20.0]
推荐阅读
- python-3.x - 在 Python 中生成单个随机整数
- android - Android Studio 说 Instant run 被禁用,因为它没有正确的 API 级别
- image - Flutter如何在资产文件夹上共享图像?
- html - CSS:删除第一页上的@page页脚
- javascript - 为什么我的应用程序给出错误:未定义 DailyData
- javascript - 如何在 getStaticProps 中获取 slug 值以调用 api,参数为 slug
- forms - MSACESS:使用下拉有界列中的数据填充新记录
- forms - 在 Delphi 中使用 2 个活动模态形式
- mongodb - 如何找到最多共同的朋友或追随者?
- python - 使用 Mayapy 的张量流