This question already has answers here:
Getting a map() to return a list in Python 3.x
(11 answers)
Closed 2 years ago.
I am taking input from the user in single line string input. Trying to convert the input string into a list of numbers using the below-mentioned command:
This command returns a of type <map at 0x1f3759638c8>.
How to iterate or access this a?
Try simply doing the following -
a = list(map(int, input().split(' ')))
print(a)
> 5 4 3
[5, 4, 3]
You will get an error for the input that you have given i.e. 'Python is fun' because map will try to map the input to function int().
Let me explain:-
When you use input() , the input taken is in form of string.
Using split(' ') splits the string on every occurrence of space and a list will be created.
Value of that list will be ['Python','is','fun']
Now what map function does is, it applies int() on every element of the list. So basically what you are trying to do is int('Python'), which will give an error.
To avoid this use the following code snippet:
a = map(int, input().split(' '))
'1 2 3 4'
for i in a:
print(i)
Output of the above code will be
1
2
3
4
Related
This question already has answers here:
Python map object is not subscriptable [duplicate]
(3 answers)
Closed 1 year ago.
for taking list input in the array I used
map(int,input().split())
but for using this input method I can't modify the array if I Iterated like a[I] then it shows
TypeError: 'map' object is not subscriptable
Can you please tell me how can I resolve this problem?
I am a competitive programmer but I am new to python I must have take input array by using map(int,input().split())
Use list(map(int,input().split())) instead of map(int,input().split()) to convert your input to list, because map function returns an generator which can not be indexed.
If you can't use another function like list, use:
# Input: 1, 2, 3, 4, 5
l = [*map(int, input().split())]
print(l)
# Output:
1 2 3 4 5
This question already has answers here:
Get a list of numbers as input from the user
(11 answers)
Closed 6 years ago.
How can I ask from the user to enter a list in Python3,so as every time the program runs,the input list is the mutable data of the program?
For example, number lists,that user maybe enters are:
L1 = [7,9,16,25],L2 = [5,10,15,20],L3 = [10,17,19,24],`L4 = [16,20,20,23]
Please write the commensurable commands/statements.
python3's input() now always returns a string instead of evaluating the text as it did in python2.7. this means that you have to convert an input string into the type of data you need. a simple way of achieving this is to prompt the user to enter a list of numbers separated by commas. the resulting string can then be split into a list of strings by calling the string's .split() method. after converting to a list of strings, you can call int() or float() to convert each string into an individual number.
Example:
s = input("enter a list of numbers separated by commas:") #string
l = s.split(",") #list
n = [int(x) for x in l] #numbers
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 7 years ago.
I really just can't understand why this code is not working...
It's probably just a typo. But even my more experienced friend is stumped here.
The prompt is simply "write a program that tells you how many 4s there are in a given list." Its all working except that count says zero no matter how many 4s I submit.
def num_four(number_list):
count = 0
print number_list
for i in number_list:
print i
if i == 4:
count = count + 1
return count
number_list = raw_input("Enter list of integers here: ")
print num_four(number_list)
the output looks like this:
Enter list of integers here: 123444
123444
1
2
3
4
4
4
0
raw_input returns a string like "8675409". When you iterate over this string (it's not a list), you get the string "8", then "6", then "7" -- it will eventually be "4", but never be the int 4.
The problem is that you don't pass a list of numbers to your function because raw_input returns a string.
number_list = raw_input("Enter list of integers here: ")
number_list = map(int, number_list.split())
print num_four(number_list)
This assumes that the numbers you input are separated by whitespace.
If you just put in strings like '1234', iterate over the string and check against '4' instead of the integer 4 in your function. If that's the case, consider renaming number_list to something more fitting like continuous_number_string.
And if you want to simplify your function, use the count method of either str or list:
>>> [1,2,3,4,4,5].count(4)
2
>>> '1234544'.count('4')
3
Please check the answer of #Mike Graham.
And just in case you are looking for a more Pythonic solution:
from collections import Counter
Counter(raw_input()).get("4")
raw_input will produce a string, not a list of integers, regardless of what you type in. So i will never equal 4 (integer) at most "4" (string).
This question already has answers here:
Converting a string representation of a list into an actual list object [duplicate]
(3 answers)
Closed 8 years ago.
I am pretty confused here.
I have a function that has a list as an argument. It then does list-specific functions on it. Here is an example:
def getValues( my_list ):
for q in my_list:
print(q)
But, I get my list from USER INPUT like this:
a_list = input("Please enter a list. ")
getValues(a_list)
The built-in input() function returns a string, not a list. My solution is to take that string of the list and turn it back into a list.
Only I don't know how.
Thanks!
Its what that ast.literal_eval is for :
>>> ast.literal_eval('[1,2,3]')
[1, 2, 3]
This question already has answers here:
Get a list of numbers as input from the user
(11 answers)
Closed 6 years ago.
I am trying to create a bounded rectangle using a random number of points that are provided by the user. The reason this is difficult for me is because all the numbers must be accepted on only one line, I don't know how many variables the user will provide, and since I am accepting points, I must have the right amount (evens).
Here is a sample run:
Enter the points:
``>>>4 1 3 5 1 5 9 0 2 5
My primary question is how do I unpack a random number of points? And also, how do I pair the even points together?
In Python 2:
points = map(int, raw_input().split())
In Python 3:
points = list(map(int, input().split()))
Another method - list comprehension:
points = [int(p) for p in input().split()]
To pair x and y of the points together you can use something like pairwise() on the points list: see https://stackoverflow.com/a/5389547/220700 for details.
If they are read as a string, you can use the split() method, which will return a list, then use map() to convert the items of the list to integers:
points_input = raw_input("Enter the points:")
points = map(int, points_input.split())
print points
Notes
The result (points) will be a list of integers.
If you are using Python 3.x, you have to use the method input() instead of raw_input().