[英]positions in a list. python
Lets say I have this list: 可以说我有这个清单:
lst = [[0], [0, 0], [0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0, 0]...]
how can I add a certain number to each cell according to its position in the lists for exapmle: I want to add a formula to each cell by multipying 3 with the position on the list*position in the nested list so lets say the second cell on the third list will be 我如何根据示例在列表中的位置为每个单元格添加一定的数字:我想通过将3与嵌套列表中list * position的位置乘以3,向每个单元格添加公式在第三个列表上将是
3*3*2
(random number)*(the third nested list)*(second spot on that list)
so eventually the list will look like that (only for the number 3) 所以最终列表看起来像这样(仅适用于数字3)
lst = [[3], [6, 12], [9, 18, 27], [12, 24, 36, 48], [15, 30, 45, 60, 75]...]
anyway this is just an example and Im asking generally about how to apply a certain formula considaring the position of nested list and inner cells in a list. 无论如何,这只是一个例子,我大体上在询问如何应用某个公式来考虑嵌套列表和列表中内部单元的位置。 Its kind of difficult to explain so I hope it came out clear enough.
它很难解释,所以我希望它足够清楚。 thank you.
谢谢。
Looks like a job for enumerate! 看起来像是一份工作!
for i, sublist in enumerate(lst):
for j, elem in enumerate(sublist):
sublist[j] = 3*(i+1)*(j+1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.