Python:对嵌套列表的子集元素进行数学运算

时间:2017-04-05 13:17:30

标签: python python-2.7

我是编码的初学者,我对在嵌套列表的元素上进行数学运算有问题。我有这个嵌套列表q:

import random

point = list (range(1000))
f = ("f1", "f2", "f3", "f4", "f5", "f6")
q = []
for i, x1 in enumerate(point):
    q.append([])
    for x2 in f:
        q[i].append(random.randint(100, 1500))

我也有:

t = 10
capacity = 1000

假设将x调用为列表子集的单个元素;而不是x我希望abs(int(-t/(capacity//x)))

如何创建这个新的嵌套列表? 提前谢谢!

2 个答案:

答案 0 :(得分:0)

这就是你要找的东西:

from __future__ import division
import random

point = range(1000)
f = ("f1", "f2", "f3", "f4", "f5", "f6")

t = 10
capacity = 1000

q = [[abs(int(-t/(capacity/random.randint(100, 1500)))) for _ in range(len(f))] for _ in point]

答案 1 :(得分:0)

递归解决方案更通用,因为它适用于任意嵌套列表:

Advertisement.questions