python将两个数组合并_将两个数组合并为一个吗? (Numpy / Python)

I currently have an array of strings and I'm trying to join it together with another array of strings to form a complete word to do some web parsing. For example:`

Var1 [A B C, .... ....]

Var2 [1 2 3]

where A and B are at a variable length and I'm trying to join them together like:``

`

C [A+1 A+2 A+3

B+1 B+2 B+3

C+1 C+2 C+3

Here's what I've tried

for param in np.nditer(Var2):

List = np.append(np.core.defchararray.add(Var1, Var2))

So I'm trying to add them together and then create a list of lists, but this isn't working. any ideas how to do this?

解决方案

Is this what you are trying to do:

In [199]: list1 = ['abc','foo','bar']

In [200]: list2 = list('1234')

In [201]: [[a+b for b in list2] for a in list1]

Out[201]:

[['abc1', 'abc2', 'abc3', 'abc4'],

['foo1', 'foo2', 'foo3', 'foo4'],

['bar1', 'bar2', 'bar3', 'bar4']]

The equivalent using np.char.add and broadcasting:

In [210]: np.char.add(np.array(list1)[:,None], np.array(list2))

Out[210]:

array([['abc1', 'abc2', 'abc3', 'abc4'],

['foo1', 'foo2', 'foo3', 'foo4'],

['bar1', 'bar2', 'bar3', 'bar4']], dtype='

For this small example the list comprehension version is faster.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值