TypeError: 'int' object is not possible error when using map

Asked 2 years ago, Updated 2 years ago, 319 views

An error occurs when you run the program below.
I think it's probably an error in the map part, but I don't know how it works, so I asked you a question.

error messages:

TypeError: 'int' object is not possible

program:

 nums1 = [1, 2, 3, 4, 5, 6, 7, 8]
nums2 = [2, 2, 3, 1, 2, 6, 7, 9]

defct(l):
    for i in l:
        print(i)

print(list(map(ct,nums1))))

python python3

2022-09-30 22:04

1 Answers

map takes out each element of the list and then
It's a built-in function that applies functions to each element, so
If you rewrite it as shown below, it will move without any errors.(ct function return value is not considered)

defect(l):
    # after modification
    print(l)

    # Before Modification
    # for i in l:
    #    print(i)

Apply functions and processing to elements in the list in Python map() as in the example in
Some people may find it intuitive to run the abs function on each element of the list.

print(list(map(abs, nums1))))


2022-09-30 22:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.