简体   繁体   English

从 numpy 数组中的字符串中按索引删除元素

[英]Remove elements by index from a string within numpy array

I have the following example array with strings:我有以下带有字符串的示例数组:

 ['100000000' '101010100' '110101010' '111111110']

I would like to be able to remove some elements by index in each of the strings in the array simultaneously.我希望能够同时通过数组中每个字符串中的索引删除一些元素。 For example if I will remove elements with index 6 and 8, I should receive the following outcome:例如,如果我要删除索引为 6 和 8 的元素,我应该会收到以下结果:

 ['1000000' '1010110' '1101000' '1111110']

All my attempts failed so far maybe because the string is immutable, but am not sure whether I have to convert and if so - to what and how.到目前为止我所有的尝试都失败了,可能是因为字符串是不可变的,但我不确定我是否必须转换,如果是的话 - 转换成什么以及如何转换。

import numpy as np

a = np.array(['100000000', '101010100', '110101010', '111111110'])

list(map(lambda s: "".join([c for i, c in enumerate(str(s)) if i not in {5, 7}]), a))

Returns:退货:

['1000000', '1010110', '1101000', '1111110']

Another way to do this is to convert a into a 2D array of single characters, mask out the values you don't want, and then convert back into a 1D array of strings.另一种方法是将a转换为单个字符的二维数组,屏蔽掉不需要的值,然后再转换回字符串的一维数组。

import numpy as np

a = np.array(['100000000', '101010100', '110101010', '111111110'])

b = a.view('U1').reshape(*a.shape, -1)

mask = np.ones(b.shape[-1], dtype=bool)
mask[[5, 7],] = False

b = b[:, mask].reshape(-1).view(f'U{b.shape[-1] - (~mask).sum()}')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM