简体   繁体   English

如何使用现有的 arrays 创建新阵列?

[英]How to use existing arrays to create a new array?

If I have some arrays:如果我有一些 arrays:

a1,
a2,
a3,
a4

How can I use them to build a new array like this:我如何使用它们来构建这样的新数组:

result = [
    [[28604,77,17096869,'Australia',2015],[31163,77.4,27662440,'Australia',1990]],
    [[44056,81.8,23968973,'Canada',1990],[43294,81.7,35939927,'Canada',2015]]
];

If I print the 0th row of result, it will output [[28604,77,17096869,'Australia',2015],[31163,77.4,27662440,'Australia',1990]] as an array.如果我打印结果的第 0 行,它将 output [[28604,77,17096869,'Australia',2015],[31163,77.4,27662440,'Australia',1990]]作为数组。

print(result[0])
output:  [[28604,77,17096869,'Australia',2015],[31163,77.4,27662440,'Australia',1990]]

Here you go, creating a list of list which every list contains two lists.在这里你 go,创建一个列表列表,每个列表包含两个列表。

a1 = [28604,77,17096869,'Australia',2015]
a2 = [31163,77.4,27662440,'Australia',1990]
a3 = [44056,81.8,23968973,'Canada',1990]
a4 = [43294,81.7,35939927,'Canada',2015]

result = [
    [a1, a2],
    [a3, a4]
]
print(result[0])

The question isn't clear on how exactly you are trying to do that.这个问题不清楚你究竟是如何做到这一点的。 In the simplest way, You can directly put them into results while initializing the list.最简单的方式,你可以在初始化列表时直接将它们放入结果中。 You can do it like -你可以这样做 -

a1=[28604,77,17096869,'Australia',2015]
a2=[31163,77.4,27662440,'Australia',1990]
a3=[44056,81.8,23968973,'Canada',1990]
a4=[43294,81.7,35939927,'Canada',2015]

result=[[a1,a2],[a3,a4]]
print(result)
print(result[0])

OUTPUT: OUTPUT:

[[[28604, 77, 17096869, 'Australia', 2015], [31163, 77.4, 27662440, 'Australia', 1990]], [[44056, 81.8, 23968973, 'Canada', 1990], [43294, 81.7, 35939927, 'Canada', 2015]]]
[[28604, 77, 17096869, 'Australia', 2015], [31163, 77.4, 27662440, 'Australia', 1990]]

The desired behaviour of your question is a little unclear, but I am sure you can achieve what you're trying to do using either您的问题的预期行为有点不清楚,但我相信您可以使用任何一种方法来实现您想要做的事情

new_array.append(a1)

or或者

new_array += a1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM