从整数构建多维列表

问题描述 投票:-2回答:3

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

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

每个的URL都相同,我只需要为每个号码发送一个列表。如果数字是7,我需要嵌套列表中的7个列表。

python multidimensional-array
3个回答
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']]

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']]

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']]
© www.soinside.com 2019 - 2024. All rights reserved.