3

I am trying to convert a set literal of integers into that of chars.

For example, if the array {0,1} is given as an input, I would like to firstly check if the individual elements are chars and if not convert them into chars so that I get {'0', '1'}

So far I have tried:

for i in alpha:

    i = str(i)

Where alpha is my array. However this does not change the overall array alpha.

Can sometime please give me an idea of how I can do this.

Thanks in advance for any help

Seb
  • 481
  • 1
  • 6
  • 18

3 Answers3

7

Use map function to convert a list of integres to list of strings , how ever map function retruns a map object so I used a list function to convert that to a list and assgin to first list (alpha) :

alpha = [1,2,3,4]    
alpha = list(map(str,alpha))
alpha # ['1', '2', '3', '4']
ᴀʀᴍᴀɴ
  • 3,931
  • 7
  • 32
  • 52
1

Here is my code:

import numpy as np
aa=['1','2','3','3','4']
aa=np.array(aa).astype('str').tolist()
aa # ['1', '2', '3', '4']
ZGski
  • 2,168
  • 18
  • 28
Zeal
  • 11
  • 1
0

When you do for i in alpha i gets a copy of elements from alpha.

But you need to modify elements which you can do as suggested by Arman or if you want to modify your code then:

for indx, i in enumerate(alpha):
    alpha[indx] = str(i)
dnit13
  • 2,377
  • 14
  • 31