Python Forum
Convert element of list to integer(Two dimentional array)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert element of list to integer(Two dimentional array)
#1
I'm trying to learn arrays(with numpy) in python3 but i am not able to figure out how do i convert the elements of list to integer(in my case i have two dimentional array). I've tried two solutions that i found(its mentioned in the code) but it gives me error. Can someone help me? Thank you.
My main objective is to get inputs and store them like a matrix(for example like 3x3) so that later i can take any elements of the list and perform any operation on them.
[inline]
from numpy import array

r,c=input("Number of row and cloumn:").split('x')
r=int(r)
c=int(c)
m=[]

for i in range(r):
    row_col=input("Row values:").split()
    m.append(row_col)
    
m=list(map(int,m)) #not working
# m=[int(i) for i in m] not working

m=array(m)
print(m)
print(m[0:1,0:1])
[/inline]

Output:
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
If i dont use the conversion( m=list(map(int,m)) ) then i get
Output:
Number or row and cloumn:3x3 Row values:1 2 3 Row values:4 5 6 Row values:7 8 9 [['1' '2' '3'] ['4' '5' '6'] ['7' '8' '9']] [['1']]
My expected output
Output:
Row values:1 2 3 Row values:4 5 6 Row values:7 8 9 [[1, 2, 3] [4, 5, 6] [7, 8, 9]] [[1]]
Reply
#2
You need to transform each row first to integer, either when you read the values:
for i in range(r):
    row = input("Row values:")
    m.append([int(s) for s in row.split()])
Or later:
m = [[int(s) for s in row] for row in m]
Remember that numpy only transforms a matrix given as a list of lists to a 2D array if all the lists have the same number of elements, so you might want to use the c value that you are not using to guarantee that the user inputs the right number of elements (or to add 0 or truncate as needed)
Reply
#3
Or you could use the following which will allow mixed numbers and strings:
>>> mylist = ['1', '4', '6', '8', 'hello', '7']
>>> newlist = [int(item) if item.isdigit() else item for item in mylist]
>>> newlist
[1, 4, 6, 8, 'hello', 7]
>>>
Reply
#4
Thanks a lot. I'll try all the solutions.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Turn list of arrays into an array of lists Cola_Reb 6 1,661 Jul-20-2022, 06:55 PM
Last Post: Cola_Reb
  numpy 2-dimentional rwahdan 3 2,060 Sep-21-2021, 07:40 PM
Last Post: deanhystad
  RandomForest --ValueError: setting an array element with a sequence JaneTan 0 1,707 Sep-08-2021, 02:12 AM
Last Post: JaneTan
  convert a list of string+bytes into a list of strings (python 3) pacscaloupsu 4 10,744 Mar-17-2020, 07:21 AM
Last Post: markfilan
  I am trying to change the value of an element in a record array ingu 1 2,131 Jan-14-2020, 01:30 PM
Last Post: perfringo
  TensorFlow get error - array with more than one element is ambiguous vokoyo 3 5,502 Nov-07-2019, 01:12 PM
Last Post: ThomasL
  How convert multidimensional array to two dimensional array tkkhan44 1 2,735 Feb-20-2019, 05:00 AM
Last Post: scidam
  How to add an element such as an average to a multi-dimensional array? xhughesey 6 3,924 Jan-06-2019, 10:47 PM
Last Post: xhughesey
  ValueError: The truth value of an array with more than one element is ambiguous. Eliza5 1 14,280 Apr-02-2018, 12:03 AM
Last Post: scidam
  Importing matlab cell array (.mat) into a python list scanato 0 8,602 Nov-15-2017, 11:04 AM
Last Post: scanato

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020