Home > Software engineering >  Joining individual elements of an array
Joining individual elements of an array

Time:10-28

I have an array consisting of labels but each label has been broken down by individual characters. For example, this is the first 2 elements of the array:

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', '', '', '',
        '', ''], ...

I would like it to be formatted as such:

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

I want to be able to iterate through a concatenate the label output so it is as shown above.

Any help in achieving this would be much appreciated :) Many thanks!

CodePudding user response:

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

Using List comprehension:

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

CodePudding user response:

Considering the array as a list of lists, you could join all characters by looping through the list:

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)

Output:

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

CodePudding user response:

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']

CodePudding user response:

Assuming from array([...]) that you are using numpy, here's a solution

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)

CodePudding user response:

If you make a loop over each element of your array, you can then use list .join to get what you are looking for.

Something like:

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

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

output

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