Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Argument 2 must support iteration (even though it does?)

Tags:

python

numpy

I'm new to python and am getting an error with the map function that doesn't make sense to me. When I call the function with a list as the second parameter it returns the error 'TypeError: Argument 2 must support iteration' which confuses me because a list should support iteration.

import numpy as np
print(np.array(map(int, raw_input().split().reverse()), float))

The code is meant to take in a list, and print out a numpy that is the reverse of the list. Any help as to why the second paramter isn't iteratable would be appreciated. Thanks!

like image 562
zebra14420 Avatar asked Feb 07 '23 03:02

zebra14420


2 Answers

list.reverse() function reverses the list in-place and returns None. If you want to write this as one line you may write reversed(raw_input().split()) instead.

like image 140
wRAR Avatar answered May 01 '23 01:05

wRAR


.reverse() does the reverse in-place and returns None.

Get rid of the .reverse() and call reversed() instead like so, which should fix your issue:

import numpy as np
print(np.array(map(int, reversed(raw_input().split())), float))
like image 23
Gareth Webber Avatar answered May 01 '23 00:05

Gareth Webber