尝试打印numpy数组的内容时出现TypeError

时间:2017-08-30 15:26:43

标签: python arrays for-loop iteration typeerror

我尝试将数组从列表转换为数组:事实上我在9组中有滑动痕迹tempTracesHW [hw] hw = [0,9]然后我想显示每个temTracesHW中的一个元素,所以我转换tempTracesHW [i]进入这样一个表:

tempTraces = np.load(r'E:\\blockData\\Concatenated_Traces.npy')

print (tempTraces.shape)     
tempSbox = [inv_sbox[tempCText[i][0] ^ tempKey[i][0]] for i in range(len(tempCText))] 
tempHW   = [hw[s] for s in tempSbox]

tempTracesHW = [[] for _ in range(9)]
print tempTracesHW
        # Fill them up
for i in range(len(tempTraces)):
    HW = tempHW[i]
    tempTracesHW[HW].append(tempTraces[i])

# Switch to numpy arrays        
tempTracesHW = [np.array(tempTracesHW[HW]) for HW in range(9)]
print(tempTracesHW)

for i in range(HW):
    print('hw=',i)
    print(len(tempTracesHW[i]))
    print(tempTracesHW[i])
    Tab= np.asarray(tempTracesHW[i])
    print(Tab.shape)
    for k in len(Tab):
        print(Tab[k])

但它给了我这个错误:

    for k in len(Tab):

TypeError: 'int' object is not iterable

3 个答案:

答案 0 :(得分:1)

应该是:

for k in Tab:
   print k

len(Tab)给出Tab的长度。

答案 1 :(得分:1)

如果您想要迭代索引或实际值,可以使用enumerate

for i, k in enumerate(Tab):
    print(Tab[i]) # print(k)

你不能迭代len(Tab)因为它是一个标量,而不是一个可迭代的。

Tab是一个numpy数组,它们具有漂亮的__repr__实现。为什么不只是print(Tab)

答案 2 :(得分:0)

import numpy as np
ar = np.asarray([1,2,3])
a_list = list(ar)

代码:

for k in list(Tab):