Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.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_Numpy - Fatal编程技术网

Python 用字母替换数组中的数字

Python 用字母替换数组中的数字,python,arrays,numpy,Python,Arrays,Numpy,我正在尝试将数组中的数字转换为字母。所以“001”将改为“A”,002改为“B”,一直改为“025”改为“Y” 到目前为止,我已经尝试使用字典替换这些值,但这似乎不起作用,使用np.place不起作用,因为它是一个if/else条件,并且我有更多的变量 Polymer_data = Polymer_data.sort_values(['ID']) for i in Polymer_data.ID: first_arr = np.array(i.split('-')) print(

我正在尝试将数组中的数字转换为字母。所以“001”将改为“A”,002改为“B”,一直改为“025”改为“Y”

到目前为止,我已经尝试使用字典替换这些值,但这似乎不起作用,使用np.place不起作用,因为它是一个if/else条件,并且我有更多的变量

Polymer_data = Polymer_data.sort_values(['ID'])
for i in Polymer_data.ID:
    first_arr = np.array(i.split('-'))
    print(first_arr)
数组中数据的小样本

['001' '001' '001' '021']
['001' '001' '001' '022']
['006' '009' '019' '016']
['006' '009' '019' '017']
['019' '025' '001' '025']
['019' '025' '002' '022']
['025' '013' '025' '025']
['025' '014' '017' '025']
['025' '014' '020' '025']
['025' '015' '022' '025']
['025' '015' '025' '025']
['025' '017' '017' '025']
['025' '017' '017' '025']
因此,上述数据应转换为

['A' 'A' 'A' 'U']
['A' 'A' 'A' 'V']
['F' 'I' 'S' 'P']
['F' 'I' 'S' 'Q']
['S' 'Y' 'A' 'Y']
['S' 'Y' 'B' 'V']
['Y' 'M' 'Y' 'Y']
['Y' 'N' 'Q' 'Y']
['Y' 'N' 'T' 'Y']
['Y' 'O' 'V' 'Y']
['Y' 'O' 'Y' 'Y']
['Y' 'Q' 'Q' 'Y']
['Y' 'Q' 'Q' 'Y']
编辑:在代码上设置格式


此外,就数组结构而言,“001”到“025”按四个顺序排列,重复排列,直到所有排列都被考虑在内,因此完整的数组列表有180000多行。

我建议像这样使用chr:

def numToChar(num):
    asciiInt = int(num) + 64
    character = str(chr(asciiInt))
    return character

a = '002'    
print(numToChar(a)) # prints 'B'
编辑:

假设您的数据如下所示:

arr = ['001', '001', '001', '021', '001', '001', '001', '022', '006', '009', '019', '016', '006', '009', '019', '017', '019', '025', '001', '025', '019', '025', '002', '022', '025', '013', '025', '025', '025', '014', '017', '025']


def numToChar(num):
    asciiInt = int(num) + 64
    character = str(chr(asciiInt))
    return character


for i in range(len(arr)):
    arr[i] = numToChar(arr[i])


print(arr)
# Would print ['A', 'A', 'A', 'U', 'A', 'A', 'A', 'V', 'F', 'I', 'S', 'P', 'F', 'I', 'S', 'Q', 'S', 'Y', 'A','Y', 'S', 'Y', 'B', 'V', 'Y', 'M', 'Y', 'Y', 'Y', 'N', 'Q', 'Y']

我的方法是创建一个字典,将整数映射为字母,并使用with dict.get将数组中的值映射为:

您可以使用字符串模块在整数和ascii字符之间创建映射:

import string

alphabet = string.ascii_uppercase

numbers = ["001", "0022", "003", "005"]
letters = [alphabet[int(number)-1] for number in numbers]
print(letters)
返回

['A', 'V', 'C', 'E']

如果速度是一个问题,那么如果您创建一个映射数组并首先将数组转换为实整数,则可以将此操作矢量化

我们可以转换为int/uint数据类型,向其中添加64,使其转换为这些数字的ascii等效值,然后简单地将其视为字符串格式。视图部分在运行时实际上是免费的,因此应该非常有效-

# a is input array
def convert_to_char_typeconv(a):
    return (a.astype(np.uint32)+64).view('U1')
另一种方法是查看uint8/uint4数据类型值,将每个三元组转换为数字,然后查看为字符串格式。同样,ascii等效思想也会出现在该方法中。因此,实施将是必要的-

def convert_to_char_view_sumr(a):    
    b = (a.view('u4')-48).reshape(-1,3)[:,1:]
    return (b[:,0]*10+b[:,1]+64).view('U1').reshape(len(a),-1)
样本运行-

# Input array of dtype <U3
In [419]: a
Out[419]: 
array([['001', '001', '001', '021'],
       ['001', '017', '001', '022']], dtype='<U3')

In [420]: convert_to_char_typeconv(a)
Out[420]: 
array([['A', 'A', 'A', 'U'],
       ['A', 'Q', 'A', 'V']], dtype='<U1')

In [421]: convert_to_char_view_sumr(a)
Out[421]: 
array([['A', 'A', 'A', 'U'],
       ['A', 'Q', 'A', 'V']], dtype='<U1')
180000行数据的计时-

为了设置输入数据,让我们使用带有2行的示例a,并沿行重复90000x次,以模拟OP的180000行的情况


请提供更清晰的列表结构,以便我更新答案并给出完整答案。原始数组是包含三位整数的字符串数组还是整数数组?for循环似乎为空。记住,for循环中的语句必须在Python中缩进。@marc我已经用列表结构的更多信息更新了帖子。@Linuxios它只是一个从1到25convert_to_char_view_dot的整数数组,返回AA而不是fo a。结尾应该是viewU1吗?另外,你能把tensordot换成b@s“?@Brella嗯,在我这边很好。所以,我猜问题可能是由于不同的输入数据类型或其他原因。第一个方法使用显式类型转换进行编辑,第二个方法删除,第三个方法使用u4。你能在你这边看看这些是怎么工作的吗?感谢您的反馈!我的a型是180000,4,D型
def convert_to_char_view_sumr(a):    
    b = (a.view('u4')-48).reshape(-1,3)[:,1:]
    return (b[:,0]*10+b[:,1]+64).view('U1').reshape(len(a),-1)
# Input array of dtype <U3
In [419]: a
Out[419]: 
array([['001', '001', '001', '021'],
       ['001', '017', '001', '022']], dtype='<U3')

In [420]: convert_to_char_typeconv(a)
Out[420]: 
array([['A', 'A', 'A', 'U'],
       ['A', 'Q', 'A', 'V']], dtype='<U1')

In [421]: convert_to_char_view_sumr(a)
Out[421]: 
array([['A', 'A', 'A', 'U'],
       ['A', 'Q', 'A', 'V']], dtype='<U1')
import string
from string import ascii_uppercase

# @neko's soln
def neko(numbers):
    alphabet = string.ascii_uppercase
    letters = [alphabet[int(number)-1] for number in numbers]
    return letters

# @yatu's soln
def yatu(a):
    d = dict(enumerate(ascii_uppercase))
    return np.vectorize(d.get)(a.astype(int)-1)

# @Nils Werner's soln
def nils(data):
    map = np.array(list(string.ascii_uppercase))
    data = data.astype(int)
    return map[data - 1]
In [425]: a
Out[425]: 
array([['001', '001', '001', '021'],
       ['001', '017', '001', '022']], dtype='<U3')

In [426]: a = np.repeat(a,90000,axis=0)

In [427]: %timeit neko(a.ravel())
     ...: %timeit yatu(a)
     ...: %timeit nils(a)
254 ms ± 1.89 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
285 ms ± 1.71 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
206 ms ± 882 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [428]: %timeit convert_to_char_typeconv(a)
     ...: %timeit convert_to_char_view_sumr(a)
206 ms ± 1.61 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
2.83 ms ± 20.5 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)