I'm trying to convert all the Python list values into integers

Asked 2 years ago, Updated 2 years ago, 9 views

I'm a beginner. Enter the list as line = list(map(str, input().split())) Using the if statement, ["3", "@", "#"] I changed the list to [3", 5, 7] Is there any way to change the list[0] to int??

I tried line = map(int, line) The object value comes out when I load it with print, is that right?

python

2022-09-22 20:30

1 Answers

Hello, everyone

line = ['3',5,7]
line = map(int,line)
print(line) # where map object is output

For inside line: # If you want to access the inside, do this
    print(t,type(t))
'''
<map object at 0x109568b38>
3 <class 'int'>
5 <class 'int'>
7 <class 'int'>
'''

If you take a picture of each type, the int type is properly taken. :)

If you want the list to return

line= ['3',5,7]
new_line = list(map(int,line)) # To view as a non-map object..
print(new_line[0])

Do it like this.

In Python, many processes return objects that can be traversed

The map is also a circular object, so it can be returned like that and used as a for statement.

The reason why it made us return this wearable object is that it eventually reduces memory and is faster :)


2022-09-22 20:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.