Reputation: 471
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
Upvotes: 3
Views: 19735
Reputation: 11
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']
Upvotes: 1
Reputation: 2496
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)
Upvotes: 0
Reputation: 4538
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']
Upvotes: 10