Python map object is not subscriptable
In Python 3, map
returns an iterable object of type map
, and not a subscriptible list, which would allow you to write map[i]
. To force a list result, write
payIntList = list(map(int,payList))
However, in many cases, you can write out your code way nicer by not using indices. For example, with list comprehensions:
payIntList = [pi + 1000 for pi in payList]
for pi in payIntList:
print(pi)
map' object is not subscriptable,How to extract a part of map
So I believe that given the information, at some point you may have tried to create a list (edge_list) by mapping a function to another list using map. Please take the following as an example.
lst = [1,2,3]
new_lst = map(lambda x: x**2, lst)
type(new_lst)
This returns a 'map' object which is not iterable. Try casting the new_lst in this example, the edge_list in yours, to a list by:
lst = [1,2,3]
new_lst = list(map(lambda x: x**2, lst))
type(new_lst)
Python 3 - TypeError: 'map' object is not subscriptable
In Python 3 map
returns a generator. Try creating a list
from it first.
with open("/bin/ls", "rb") as fin: #rb as text file and location
buf = fin.read()
bytes = list(map(ord, buf))
print (bytes[:10])
saveFile = open('exampleFile.txt', 'w')
saveFile.write(bytes)
saveFile.close()
If you think that is ugly, you can replace it with a list generator:
bytes = [ord(b) for f in buf]
Related Topics
How to Install a Python Package from Within Ipython
Use of True, False, and None as Return Values in Python Functions
Python Django Global Variables
Sending Mail from Python Using Smtp
When Do You Use 'Self' in Python
How to Read Unicode Input and Compare Unicode Strings in Python
Find Element's Index in Pandas Series
Get All Object Attributes in Python
Reset Color Cycle in Matplotlib
Install MySQL-Python (Windows)
Python Time + Timedelta Equivalent
Check for Identical Rows in Different Numpy Arrays
How to Schedule a Function to Run Every Hour on Flask