【问题标题】:How to use Wildcard in Numpy array operations?如何在 Numpy 数组操作中使用通配符?
【发布时间】:2022-01-26 22:40:17
【问题描述】:

例如,定义一个numpy.str_格式的numpy数组,并做numpy.char支持的替换操作:

import numpy as np
a = np.array(['a-b-c-d','e-f-g-h'],np.str_)
print (np.char.replace(a,'*-','i-'))

返回的结果应该是['a-b-c-d', 'e-f-g-h'],但应该是['i-i-i-d', 'i-i-i-h']

【问题讨论】:

    标签: python numpy


    【解决方案1】:

    有任何理由使用 numpy 数组吗? numpy.char.replace 不能使用通配符。

    我建议在这里使用 python 列表和re 模块:

    l = ['a-b-c-d', 'e-f-g-h']
    import re
    out = [re.sub('.-', 'i-', i) for i in l]
    

    输出:['i-i-i-d', 'i-i-i-h']

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-06
      • 1970-01-01
      • 1970-01-01
      • 2023-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-06
      相关资源
      最近更新 更多