Python 3.x Python3.x:压缩两个列表
我尝试使用以下代码压缩两个列表Python 3.x Python3.x:压缩两个列表,python-3.x,Python 3.x,我尝试使用以下代码压缩两个列表 x= array([ 56, 84, 112, 140, 168, 196, 224, 252, 280, 308, 336, 364, 392, 420, 448, 476, 504, 532, 560, 588, 616, 644, 672, 700, 728, 756, 784, 812, 840, 868, 896, 924, 952, 980,
x= array([ 56, 84, 112, 140, 168, 196, 224, 252, 280, 308, 336,
364, 392, 420, 448, 476, 504, 532, 560, 588, 616, 644,
672, 700, 728, 756, 784, 812, 840, 868, 896, 924, 952,
980, 1008, 1036, 1064, 1092, 1120, 1148, 1176, 1204, 1232, 1260,
1288, 1316])
y= array([0.96780319, 0.97895697, 0.97842111, 0.97283027, 0.97018866,
0.96909372, 0.96666816, 0.96606516, 0.96241692, 0.96401202,
0.96687579, 0.9639144 , 0.96337408, 0.95347031, 0.94973264,
0.94029729, 0.93110676, 0.91811095, 0.90408523, 0.89094813,
0.86007405, 0.82883029, 0.81545714, 0.80478483, 0.78737364,
0.75932569, 0.63460651, 0.47195082, 0.47093429, 0.55858831,
0.88054987, 0.96316967, 0.93770625, 0.82751165, 0.76109564,
0.73998599, 0.77669407, 0.785424 , 0.76852611, 0.78837932,
0.79866485, 0.8308753 , 0.89322146, 0.92456099, 0.9673221 ,
0.96211249])
zipped_list=[]
row = np.array([list for i in zip(x, y)])
for a, b in row:
zipped_list.append((a,b))
我收到以下错误消息
Traceback (most recent call last):
File "<ipython-input-106-2294469b69dd>", line 4, in <module>
for a, b in row:
TypeError: 'type' object is not iterable
关于代码为什么会遇到此错误以及如何修复它,有什么建议吗
谢谢
编辑:我意识到我错过了列表之后的(I)。
而不是row=np.array([zip中i的列表(x,y)])
我错过了清单后面的内容。现在是正确的版本(后者)。代码运行良好。谢谢你的回复 似乎这就是你想要得到的:
import numpy as np
x= np.array([ 56, 84, 112, 140, 168, 196, 224, 252, 280, 308, 336,
364, 392, 420, 448, 476, 504, 532, 560, 588, 616, 644,
672, 700, 728, 756, 784, 812, 840, 868, 896, 924, 952,
980, 1008, 1036, 1064, 1092, 1120, 1148, 1176, 1204, 1232, 1260,
1288, 1316])
y= np.array([0.96780319, 0.97895697, 0.97842111, 0.97283027, 0.97018866,
0.96909372, 0.96666816, 0.96606516, 0.96241692, 0.96401202,
0.96687579, 0.9639144 , 0.96337408, 0.95347031, 0.94973264,
0.94029729, 0.93110676, 0.91811095, 0.90408523, 0.89094813,
0.86007405, 0.82883029, 0.81545714, 0.80478483, 0.78737364,
0.75932569, 0.63460651, 0.47195082, 0.47093429, 0.55858831,
0.88054987, 0.96316967, 0.93770625, 0.82751165, 0.76109564,
0.73998599, 0.77669407, 0.785424 , 0.76852611, 0.78837932,
0.79866485, 0.8308753 , 0.89322146, 0.92456099, 0.9673221 ,
0.96211249])
zipped_list = [i for i in zip(x, y)]
print(zipped_list)
您的数组中充满了Python类型
列表
。我怀疑这是你想要的。你希望[list for I in zip(x,y)]
产生什么?list(zip(x,y))
会给你这个output@WarrenWeckesser我添加了编辑[list for i in zip(x,y)]
不会做任何事情,因为它缺少(i)after列表。以下操作应该有效:[在zip(x,y)中列出i的(i)]
。
row = np.array([list (i) for i in zip(x, y)])
import numpy as np
x= np.array([ 56, 84, 112, 140, 168, 196, 224, 252, 280, 308, 336,
364, 392, 420, 448, 476, 504, 532, 560, 588, 616, 644,
672, 700, 728, 756, 784, 812, 840, 868, 896, 924, 952,
980, 1008, 1036, 1064, 1092, 1120, 1148, 1176, 1204, 1232, 1260,
1288, 1316])
y= np.array([0.96780319, 0.97895697, 0.97842111, 0.97283027, 0.97018866,
0.96909372, 0.96666816, 0.96606516, 0.96241692, 0.96401202,
0.96687579, 0.9639144 , 0.96337408, 0.95347031, 0.94973264,
0.94029729, 0.93110676, 0.91811095, 0.90408523, 0.89094813,
0.86007405, 0.82883029, 0.81545714, 0.80478483, 0.78737364,
0.75932569, 0.63460651, 0.47195082, 0.47093429, 0.55858831,
0.88054987, 0.96316967, 0.93770625, 0.82751165, 0.76109564,
0.73998599, 0.77669407, 0.785424 , 0.76852611, 0.78837932,
0.79866485, 0.8308753 , 0.89322146, 0.92456099, 0.9673221 ,
0.96211249])
zipped_list = [i for i in zip(x, y)]
print(zipped_list)