python 连接数组的各个元素

rqqzpn5f  于 2023-01-19  发布在  Python
关注(0)|答案(5)|浏览(160)

我有一个由标签组成的数组,但每个标签都被单独的字符分解。例如,这是数组的前2个元素:

array([['1', '.', ' ', 'I', 'd', 'e', 'n', 't', 'i', 'f', 'y', 'i', 'n',
        'g', ',', ' ', 'A', 's', 's', 'e', 's', 's', 'i', 'n', 'g', ' ',
        'a', 'n', 'd', ' ', 'I', 'm', 'p', 'r', 'o', 'v', 'i', 'n', 'g',
        ' ', 'C', 'a', 'r', 'e', '', ''],
       ['9', '.', ' ', 'N', 'o', 'n', '-', 'P', 'h', 'a', 'r', 'm', 'a',
        'c', 'o', 'l', 'o', 'g', 'i', 'c', 'a', 'l', ' ', 'I', 'n', 't',
        'e', 'r', 'v', 'e', 'n', 't', 'i', 'o', 'n', 's', '', '', '',
        '', ''], ...

我希望它的格式如下:

array(['1. Identifying, Assessing and Improving Care',
       '9. Non-Pharmacological Interventions', ...

我希望能够迭代通过一个concatenate标签输出,所以它是如上所示。
任何帮助实现这一点将不胜感激:)非常感谢!

flvtvl50

flvtvl501#

import numpy as np
k=np.array([['1', '.', ' ', 'I', 'd', 'e', 'n', 't', 'i', 'f', 'y', 'i', 'n',
        'g', ',', ' ', 'A', 's', 's', 'e', 's', 's', 'i', 'n', 'g', ' ',
        'a', 'n', 'd', ' ', 'I', 'm', 'p', 'r', 'o', 'v', 'i', 'n', 'g',
        ' ', 'C', 'a', 'r', 'e', '', ''],
       ['9', '.', ' ', 'N', 'o', 'n', '-', 'P', 'h', 'a', 'r', 'm', 'a',
        'c', 'o', 'l', 'o', 'g', 'i', 'c', 'a', 'l', ' ', 'I', 'n', 't',
        'e', 'r', 'v', 'e', 'n', 't', 'i', 'o', 'n', 's', '', '', '',
        '', '']])

for x in k:
    print(''.join(x))

#output
1. Identifying, Assessing and Improving Care
9. Non-Pharmacological Interventions

使用列表解析:

[''.join(x) for x in k]
#output
['1. Identifying, Assessing and Improving Care',
 '9. Non-Pharmacological Interventions']
x9ybnkn6

x9ybnkn62#

将数组视为列表的列表,可以通过循环遍历列表来连接所有字符:

r = [['1', '.', ' ', 'I', 'd', 'e', 'n', 't', 'i', 'f', 'y', 'i', 'n',
        'g', ',', ' ', 'A', 's', 's', 'e', 's', 's', 'i', 'n', 'g', ' ',
        'a', 'n', 'd', ' ', 'I', 'm', 'p', 'r', 'o', 'v', 'i', 'n', 'g',
        ' ', 'C', 'a', 'r', 'e', '', ''],
       ['9', '.', ' ', 'N', 'o', 'n', '-', 'P', 'h', 'a', 'r', 'm', 'a',
        'c', 'o', 'l', 'o', 'g', 'i', 'c', 'a', 'l', ' ', 'I', 'n', 't',
        'e', 'r', 'v', 'e', 'n', 't', 'i', 'o', 'n', 's', '', '', '',
        '', '']]

t = ["".join(i) for i in r]
print(t)

输出:

['1. Identifying, Assessing and Improving Care',
 '9. Non-Pharmacological Interventions']
clj7thdc

clj7thdc3#

array = [['1', '.', ' ', 'I', 'd', 'e', 'n', 't', 'i', 'f', 'y', 'i', 'n',
        'g', ',', ' ', 'A', 's', 's', 'e', 's', 's', 'i', 'n', 'g', ' ',
        'a', 'n', 'd', ' ', 'I', 'm', 'p', 'r', 'o', 'v', 'i', 'n', 'g',
        ' ', 'C', 'a', 'r', 'e', '', ''],
       ['9', '.', ' ', 'N', 'o', 'n', '-', 'P', 'h', 'a', 'r', 'm', 'a',
        'c', 'o', 'l', 'o', 'g', 'i', 'c', 'a', 'l', ' ', 'I', 'n', 't',
        'e', 'r', 'v', 'e', 'n', 't', 'i', 'o', 'n', 's', '', '', '',
        '', '']]

# array(['1. Identifying, Assessing and Improving Care',
#        '9. Non-Pharmacological Interventions', ...

array = [''.join(i) for i in array]
print(array)  #['1. Identifying, Assessing and Improving Care', '9. Non-Pharmacological Interventions']
kkih6yb8

kkih6yb84#

假设在array([...])中使用的是numpy,下面是一个解决方案

import numpy as np

a = np.array([['1', '.', ' ', 'I', 'd', 'e', 'n', 't', 'i', 'f', 'y', 'i', 'n',
        'g', ',', ' ', 'A', 's', 's', 'e', 's', 's', 'i', 'n', 'g', ' ',
        'a', 'n', 'd', ' ', 'I', 'm', 'p', 'r', 'o', 'v', 'i', 'n', 'g',
        ' ', 'C', 'a', 'r', 'e', '', ''],
       ['9', '.', ' ', 'N', 'o', 'n', '-', 'P', 'h', 'a', 'r', 'm', 'a',
        'c', 'o', 'l', 'o', 'g', 'i', 'c', 'a', 'l', ' ', 'I', 'n', 't',
        'e', 'r', 'v', 'e', 'n', 't', 'i', 'o', 'n', 's', '', '', '',
        '', '']])

b = np.empty(a.shape[0], dtype=object)
for i, x in enumerate(a): b[i] = ''.join(x)
31moq8wy

31moq8wy5#

如果对数组中的每个元素进行循环,那么可以使用list .join来获取所需的内容。
比如:

arr = [['1', '.', ' ', 'I', ...], ...]

output = list()
for x in arr:
   output.append(''.join(x))

output

>>>
['1. Identifying, Assessing and Improving Care', ...]

相关问题