我正在尝试通过使用以下代码来压缩两个列表。
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
理想的结果是将x和y元素逐个元素
[(56,0.96780319), (84,0.97895697), (112,0.97842111)....(1316,0.96211249)]
关于代码为什么会遇到此错误以及如何解决该问题的任何建议?
谢谢。
编辑:我意识到我错过了名单之后的(i)。
代替row = np.array([list for i in zip(x, y)])
,
row = np.array([list (i) for i in zip(x, y)])
我错过了名单之后的(i)。现在带有更正的版本(后者)。代码运行正常。多谢您的回覆。
答案 0 :(得分:1)
似乎是您想要得到的:
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)