Converting all elements in a 2D array to uppercase in python
Question:
I have a 2D array that looks a bit like this:
array =[['a', 'b', 'c', 'd'], ['e', 'f'], ['g', 'h', 'i', 'k', 'l']]
Is there a way of converting all elements of the array into capitals? The only methods I could fine only worked on 1D array or 2D arrays when the sub arrays are the same size.
Answers:
Could this be what you need, quite simple list comprehension:
[[item.upper() for item in sub] for sub in array]
A possible solution would be to use map
function:
>>> map(lambda x: map(lambda y: y.upper(), x), array)
[['A', 'B', 'C', 'D'], ['E', 'F'], ['G', 'H', 'I', 'K', 'L']]
You can also use list comprehensions:
>>> [[x.upper() for x in y] for y in array]
[['A', 'B', 'C', 'D'], ['E', 'F'], ['G', 'H', 'I', 'K', 'L']]
You can use .join()
and .split()
:
array = [['a', 'b', 'c', 'd'], ['e', 'f'], ['g', 'h', 'i', 'k', 'l']]
# 1. join letters with ' '. 2. UP them. 3. split them with ' '
new_array = [' '.join(a).upper().split() for a in array]
print(new_array) # => [['A', 'B', 'C', 'D'], ['E', 'F'], ['G', 'H', 'I', 'K', 'L']]
In case the elements have more then one letter and have spaces between the letters, use this :
array = [['a x', 'bq', 'c', 'd'], ['e', 'f'], ['g', 'ha', 'd i', 'k', 'l']]
new_array = [[x.upper() for x in a] for a in array]
print(new_array) # => [['A X', 'BQ', 'C', 'D'], ['E', 'F'], ['G', 'HA', 'D I', 'K', 'L']]
for x in array:
[y.upper() for y in x]
Then you have to wrap them into a new array.
A way to do it all together could be:
newarr = []
for x in array:
newarr.append([y.upper() for y in x])
your array should be, elements separated by commas
array =[['a', 'b', 'c', 'd'],['e', 'f'],['g', 'h', 'i', 'k', 'l']]
mapping all elements of all array elemts to upper case
array_new = list(map(lambda x:list(map(lambda y:str.upper(y),x)),array))
Do you want the result to be the same shape, or one array?
If you want a single array, you could do this:
foo = [["a", "b", "c"], ["d", "e"], ["f", "g", "h"]]
[x.upper() for inner in foo for x in inner]
If you want the same shape do:
[[x.upper() for x in inner] for inner in foo]
Note that the ordering is different in the two examples. That’s because in the second case Python “loops over” the outer comprehension first, and uses the value over inner
at each point to evaluate the inner comprehension.
Either way, read up list comprehensions. They’re the best solution for a lot of things in Python.
1. Converting to uppercase lists of letters in a list
array = [['a', 'b', 'c', 'd'], ['e', 'f'], ['g', 'h', 'i', 'k', 'l']]
for lists in array:
for elements in lists:
lists[lists.index(elements)] = elements.upper()
print(array)
output
[[‘A’, ‘B’, ‘C’, ‘D’], [‘E’, ‘F’], [‘G’, ‘H’, ‘I’, ‘K’, ‘L’]]
2. Getting all the elements of the different lists in just one list with a list comprehension
array = [['a', 'b', 'c', 'd'], ['e', 'f'], ['g', 'h', 'i', 'k', 'l']]
array = [lists[lists.index(elements)].upper() for lists in array for elements in lists]
print(array)
output
[‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘I’, ‘K’, ‘L’]
names = np.array([["Izzy", "Monica", "Marvin"],
["Weber", "Patel", "Hernandez"]])
Vectorize the .upper() string method
vectorized_upper = np.vectorize(str.upper)
Apply vectorized_upper to the names array
uppercase_names = vectorized_upper(names)
print(uppercase_names)
I have a 2D array that looks a bit like this:
array =[['a', 'b', 'c', 'd'], ['e', 'f'], ['g', 'h', 'i', 'k', 'l']]
Is there a way of converting all elements of the array into capitals? The only methods I could fine only worked on 1D array or 2D arrays when the sub arrays are the same size.
Could this be what you need, quite simple list comprehension:
[[item.upper() for item in sub] for sub in array]
A possible solution would be to use map
function:
>>> map(lambda x: map(lambda y: y.upper(), x), array)
[['A', 'B', 'C', 'D'], ['E', 'F'], ['G', 'H', 'I', 'K', 'L']]
You can also use list comprehensions:
>>> [[x.upper() for x in y] for y in array]
[['A', 'B', 'C', 'D'], ['E', 'F'], ['G', 'H', 'I', 'K', 'L']]
You can use .join()
and .split()
:
array = [['a', 'b', 'c', 'd'], ['e', 'f'], ['g', 'h', 'i', 'k', 'l']]
# 1. join letters with ' '. 2. UP them. 3. split them with ' '
new_array = [' '.join(a).upper().split() for a in array]
print(new_array) # => [['A', 'B', 'C', 'D'], ['E', 'F'], ['G', 'H', 'I', 'K', 'L']]
In case the elements have more then one letter and have spaces between the letters, use this :
array = [['a x', 'bq', 'c', 'd'], ['e', 'f'], ['g', 'ha', 'd i', 'k', 'l']]
new_array = [[x.upper() for x in a] for a in array]
print(new_array) # => [['A X', 'BQ', 'C', 'D'], ['E', 'F'], ['G', 'HA', 'D I', 'K', 'L']]
for x in array:
[y.upper() for y in x]
Then you have to wrap them into a new array.
A way to do it all together could be:
newarr = []
for x in array:
newarr.append([y.upper() for y in x])
your array should be, elements separated by commas
array =[['a', 'b', 'c', 'd'],['e', 'f'],['g', 'h', 'i', 'k', 'l']]
mapping all elements of all array elemts to upper case
array_new = list(map(lambda x:list(map(lambda y:str.upper(y),x)),array))
Do you want the result to be the same shape, or one array?
If you want a single array, you could do this:
foo = [["a", "b", "c"], ["d", "e"], ["f", "g", "h"]]
[x.upper() for inner in foo for x in inner]
If you want the same shape do:
[[x.upper() for x in inner] for inner in foo]
Note that the ordering is different in the two examples. That’s because in the second case Python “loops over” the outer comprehension first, and uses the value over inner
at each point to evaluate the inner comprehension.
Either way, read up list comprehensions. They’re the best solution for a lot of things in Python.
1. Converting to uppercase lists of letters in a list
array = [['a', 'b', 'c', 'd'], ['e', 'f'], ['g', 'h', 'i', 'k', 'l']]
for lists in array:
for elements in lists:
lists[lists.index(elements)] = elements.upper()
print(array)
output
[[‘A’, ‘B’, ‘C’, ‘D’], [‘E’, ‘F’], [‘G’, ‘H’, ‘I’, ‘K’, ‘L’]]
2. Getting all the elements of the different lists in just one list with a list comprehension
array = [['a', 'b', 'c', 'd'], ['e', 'f'], ['g', 'h', 'i', 'k', 'l']]
array = [lists[lists.index(elements)].upper() for lists in array for elements in lists]
print(array)
output
[‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘I’, ‘K’, ‘L’]
names = np.array([["Izzy", "Monica", "Marvin"],
["Weber", "Patel", "Hernandez"]])
Vectorize the .upper() string method
vectorized_upper = np.vectorize(str.upper)
Apply vectorized_upper to the names array
uppercase_names = vectorized_upper(names)
print(uppercase_names)