用Numpy过滤数组字符串 [英] Filtering array strings with Numpy
本文介绍了用Numpy过滤数组字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我该如何编写一个numpy函数,使其仅过滤出以'USD'结尾的数组字符串.我如何在没有for循环的情况下执行此过滤器.
How could I write a numpy function where it only filters out the array strings that ends with 'USD'. How would I be able to execute this filter without a for loop.
import numpy as np
Array= ['BTCUSD', 'ETHUSD', 'David', 'georGe', 'XRPUSD', 'USDAUD', 'ETHUSDC' ]
预期产量
['BTCUSD', 'ETHUSD', 'XRPUSD']
推荐答案
使用numpy 输出:
['BTCUSD' 'ETHUSD' 'XRPUSD']
对于返回类型为 list
而不是 np.ndarray
的返回类型,可以使用理解:
For a return type of list
instead of np.ndarray
a comprehension can be used:
import numpy as np
lst = np.array(['BTCUSD', 'ETHUSD', 'David', 'georGe', 'XRPUSD', 'USDAUD', 'ETHUSDC'])
print([elem for elem in lst if elem.endswith('USD')])
输出:
['BTCUSD', 'ETHUSD', 'XRPUSD']
*理解方法可用于Python列表以及np数组.
*The comprehension approach can be used on Python lists as well as np arrays.
这篇关于用Numpy过滤数组字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文