numpy.delete改变花车阵列串 [英] numpy.delete changing arrays of floats to strings

查看:104
本文介绍了numpy.delete改变花车阵列串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了 numpy.delete 函数删除多个数组元素。我从这个问题开始做这个简单的/如果块更Python 抵达,将给予那里的答案,这个后:

I'm using the numpy.delete function to remove elements from several arrays. I started from this question Make this simple for/if block more pythonic and arrived, after applying the answer given there, to this:

# Data lists.
a = [3,4,5,12,6,8,78,5,6]
b = [6,4,1.,2,8,784.,43,6.,2]
c = [8,4.,32.,6,1,7,2.,9,23]

# Maximum limit.
max_limit = 20.

# Store indexes of elements to be removed.
indexes = [i for i, t in enumerate(zip(b, c)) if any(e > max_limit for e in t)]

# Remove elements from all lists.
big_array = np.array([a, b, c])
clean_array = np.delete(big_array, indexes, axis=1)

# Final clean lists.
a_clean, b_clean, c_clean = clean_array

我遇到的问题是,我的列表中的一个实际上由的字符串的而不是花车,像这样的:

The issue I'm having is that one of my lists is actually made of strings rather than floats, like this:

# Data lists.
a = ['a','b','c','d','e','f','g','h','i'] # <-- strings
b = [6,4,1.,2,8,784.,43,6.,2] # <-- floats
c = [8,4.,32.,6,1,7,2.,9,23] # <-- floats

当我将code中的 numpy.delete 线以上转换中的所有的最终名单的(<$ C $的所有元素C> a_clean,b_clean,c_clean )以字符串,而不是preserving只有第一个字符串,其余为浮动。

and when I apply the code above the numpy.delete line converts all elements in all final lists (a_clean, b_clean, c_clean) to strings rather than preserving only the first one as strings and the rest as floats.

如何prevent这种情况的发生或修复它,如果它不能得到帮助?

How can I prevent this from happening or fix it if it can't be helped?

推荐答案

如果您要使用numpy的,你会好起来的转换您的列表,以数组。它看起来是这样的:

If you are going to use numpy, you are going to be better off converting your lists to arrays. It would look something like:

a = np.array(a)
b = np.array(b)
c = np.array(c)

mask = (b > max_limit) | (c > max_limit)

a = a[mask]
b = b[mask]
c = c[mask]

>>> a
array(['c', 'f', 'g', 'i'],
      dtype='|S1')
>>> b
array([   1.,  784.,   43.,    2.])
>>> c
array([ 32.,   7.,   2.,  23.])

这篇关于numpy.delete改变花车阵列串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆