Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从现有列表或列表的相反顺序创建新数组_Python_Arrays_Switch Statement - Fatal编程技术网

Python 如何从现有列表或列表的相反顺序创建新数组

Python 如何从现有列表或列表的相反顺序创建新数组,python,arrays,switch-statement,Python,Arrays,Switch Statement,我在一个数组中有两个列表。我想切换列表顺序。列表2变为列表1,列表1变为列表2。关于如何有效地切换这些,我想请您提供一些帮助。谢谢 我有以下python代码 RestoredData_Array = np.dot(featuresT.reshape(2,2), FinalData1_Matrix.reshape(2,20)) RestoredData_Array 实际结果: array([[ 2.3065 , 9.21202097, 2.03334271, 8.121047

我在一个数组中有两个列表。我想切换列表顺序。列表2变为列表1,列表1变为列表2。关于如何有效地切换这些,我想请您提供一些帮助。谢谢

我有以下python代码

RestoredData_Array = np.dot(featuresT.reshape(2,2), FinalData1_Matrix.reshape(2,20))
RestoredData_Array
实际结果:

array([[  2.3065    ,   9.21202097,   2.03334271,   8.12104732,
          1.02492108,   4.09347257,  -0.54700703,  -2.18471288,
          0.15896622,   0.63490144,  -0.51904295,  -2.07302602,
         -2.11190708,  -8.43482867,  -3.33826623, -13.33283264,
         -3.24268925, -12.95110399,  -3.39616989, -13.56409637],

       [ -9.93383348, -39.6751278 , -11.51169937, -45.9770284 ,
        -13.27291919, -53.01123343, -12.73236969, -50.85231155,
        -13.35424863, -53.33605825, -14.86736232, -59.37934246,
        -17.41605181, -69.55865355, -17.84717309, -71.28052577,
        -19.07951685, -76.20243193, -20.72810021, -82.78677378]])
预期结果:

array([[ -9.93383348, -39.6751278 , -11.51169937, -45.9770284 ,
        -13.27291919, -53.01123343, -12.73236969, -50.85231155,
        -13.35424863, -53.33605825, -14.86736232, -59.37934246,
        -17.41605181, -69.55865355, -17.84717309, -71.28052577,
        -19.07951685, -76.20243193, -20.72810021, -82.78677378]

        [  2.3065    ,   9.21202097,   2.03334271,   8.12104732,
          1.02492108,   4.09347257,  -0.54700703,  -2.18471288,
          0.15896622,   0.63490144,  -0.51904295,  -2.07302602,
         -2.11190708,  -8.43482867,  -3.33826623, -13.33283264,
         -3.24268925, -12.95110399,  -3.39616989, -13.56409637]])

交换两个数组的值:

a=[1]
b=[2]
a,b = b,a
print(a)
print(b)

交换嵌套数组的值:

c=[[1],[2]] # here c[0] have value [1] & c[1] have value [2] 
c[0],c[1] = c[1],c[0] # here we are interchanging the value of c[0] & c[1]
print(c)
这些示例可以帮助您转换数组

对于您的代码,这可能会起作用:

RestoredData_Array = np.dot(featuresT.reshape(2,2), FinalData1_Matrix.reshape(2,20))
RestoredData_Array[0],RestoredData_Array[1] = RestoredData_Array[1],RestoredData_Array[0]
print(RestoredData_Array)

看起来您只需要将
[a,b]
更改为
[b,a]
,其中
a
b
是您的列表

有很多方法。下面的代码是最短的,它同时适用于列表和numpy数组

Reversed_Array = RestoredData_Array[::-1]

这就是切片技巧。请参阅以了解其工作原理。

RestoredData_Array[:-1]如果答案解决了您的问题,您应该接受答案。否则,问题将保留在等待更多答案的队列中。此外,如果你觉得答案有帮助,你应该投票表决。谢谢。这起作用了。谢谢你的帮助@很高兴知道。正如你所知道的:如果你发现一个答案对你有帮助,你应该投票表决,而不是写评论。这样其他人就知道哪些答案是好的。