当尝试将字符串分成字符数组时,出现此错误:“ TypeError:'builtin_function_or_method'对象不可迭代”
我尝试过“ [[char in string in string”]” 和列表(字符串) 没有一个起作用。
奇怪的是,它在python 2.7.9中工作。
即使在函数中,我也尝试仅使用列表,但不会出现错误,该错误仅在此脚本中出现并且我有0个主意。
import numpy as np
def hex2bin(hex):
numpy = list(hex)
array = np.array(numpy)
output = np.array([])
for i in range(array.size):
slice = array[i:i+1]
if slice == "0":
result = "0000"
if slice == "1":
result = "0001"
if slice == "2":
result = "0010"
if slice == "3":
result = "0011"
if slice == "4":
result = "0100"
if slice == "5":
result = "0101"
if slice == "6":
result = "0110"
if slice == "7":
result = "0111"
if slice == "8":
result = "1000"
if slice == "9":
result = "1001"
if slice == "A":
result = "1010"
if slice == "B":
result = "1011"
if slice == "C":
result = "1100"
if slice == "D":
result = "1101"
if slice == "E":
result = "1110"
if slice == "F":
result = "1111"
output = np.append(output, list(result))
return output
^^^不起作用
def test(string):
char = list(string)
print(char)
test("test")
^^^有效吗?输出:['t','e','s','t']
如果我要打印numpy,它应该打印一个字符数组。这是完整的错误:
File "/Users/internone/github/Dynamic-Document/Hex2Bin.py", line 4, in
hex2bin
numpy = list(hex)
TypeError: 'builtin_function_or_method' object is not iterable