从整数构建多维列表

时间:2018-01-03 23:27:58

标签: python multidimensional-array

我需要创建一个简单的多维列表,以便在另一个系统中触发集成。如果我有数字3,我想输出以下内容:

defer

每个网址相同,我只需要为每个号码发送一个列表。如果数字是7,我需要在嵌套列表中有7个列表。

3 个答案:

答案 0 :(得分:1)

hooks = [[i,'https://hooks.com/catch/123'] for i in range(10)]

产地:

[[0, 'https://hooks.com/catch/123'],
 [1, 'https://hooks.com/catch/123'],
 [2, 'https://hooks.com/catch/123'],
 [3, 'https://hooks.com/catch/123'],
 [4, 'https://hooks.com/catch/123'],
 [5, 'https://hooks.com/catch/123'],
 [6, 'https://hooks.com/catch/123'],
 [7, 'https://hooks.com/catch/123'],
 [8, 'https://hooks.com/catch/123'],
 [9, 'https://hooks.com/catch/123']]

答案 1 :(得分:0)

您可以使用列表理解:

def multdimensional_list(i):
    link = 'https://hooks.com/catch/123'
    return [[n,link] for n in range(1, i+1)]

multidimensional_list(3)返回:

[[1, 'https://hooks.com/catch/123'],
 [2, 'https://hooks.com/catch/123'],
 [3, 'https://hooks.com/catch/123']]

答案 2 :(得分:0)

这也有效:

url = 'https://hooks.com/catch/123'
number = 3

outlist = list(map(lambda i: [i+1,url], range(number)))
print(outlist)

输出:

[[1, 'https://hooks.com/catch/123'], [2, 'https://hooks.com/catch/123'], [3, 'https://hooks.com/catch/123']]