1

My script in progress:

def plotRegression(data):
    '''read labdata.txt and plot
    x,y coordinates using formula'''

    import turtle

    wn = turtle.Screen()
    t = turtle.Turtle()
    t.speed()

    #set up variables
    x_list = [i[0] for i in data]
    y_list = [i[1] for i in data]

    #formula goes here

    #set window size here

with open("labdata.txt") as f:
    #coords = [map(int, line.split()) for line in f]
    coords = list(map(int, line.split()) for line in f)

plotRegression(coords)

labdata.txt sample:

44 71
79 37
78 24

Error when running script:

Traceback (most recent call last):
  File "plot_regression.py", line 23, in <module>
    plotRegression(coords)
  File "plot_regression.py", line 12, in plotRegression
    x_list = [i[0] for i in data]
  File "plot_regression.py", line 12, in <listcomp>
    x_list = [i[0] for i in data]
TypeError: 'map' object is not subscriptable

My goal from this question is to read the data from labdata.txt and have the data ready as integers for the function to read. I think I have overcomplicated things at this point but I have something to learn so your help is appreciated!

In the with statement I have commented out a line. I saw this map method for the first time in someone else's code and I wanted to give it a shot as it seemed useful. However, after some errors and research it looks like this is Python 2 code and I am using Python 3 so there are some differences which will not allow me to run the code properly.

Searches here on stackoverflow explain that the map function "returns a generator" but I am not sure what that means. Can someone explain to me why my attempt is not working?

wq_
  • 27
  • 1
  • 8
  • It does not. It returns an iterator. If you need a list call `list` on it – juanpa.arrivillaga Feb 21 '18 at 17:28
  • @juanpa.arrivillaga I have "coords = list(map(int, line.split()) for line in f) " in my code above but I was still getting an error. I have a gap somewhere in my knowledge and I'm trying to figure it out. I've tried reading through the documentation but there's so much I don't understand. Apologies, as I just started learning programming about a month ago and everything is so new! – wq_ Feb 21 '18 at 17:56
  • If you are just learning, then I would avoid generator expressions/list-comprehensions and mixing that with `map`, but anyway`list(map(int, line.split()) for line in f)` should really use a list comprehension: `[map(int, line.split() for line in f]` and you could use `[list(map(int, line.split())) for line in f]` or go with nested list-comprehensions: `[[int(x) for x in line.split()] for line in f]` But for now, stick to for-loops. – juanpa.arrivillaga Feb 21 '18 at 18:00
  • @juanpa.arrivillaga It seems I have dived too deep for my level of understanding. I'll stick to for loops for now. Thank you! – wq_ Feb 21 '18 at 18:43

1 Answers1

0
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> map(int, '1 2 3 4'.split())
<map object at 0x7f9202ae9518>
>>> m = map(int, '1 2 3 4'.split())
>>> m[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'map' object is not subscriptable

>>> list(map(int, '1 2 3 4'.split()))
[1, 2, 3, 4]
>>> list(map(int, line.split()) for line in '1 2\n3 4'.split('\n'))
[<map object at 0x7f9202ae9780>, <map object at 0x7f9202ae9828>]
>>> list(list(map(int, line.split())) for line in '1 2\n3 4'.split('\n'))
[[1, 2], [3, 4]]
>>> [[int(num) for num in line.split()] for line in '1 2\n3 4'.split('\n')]
[[1, 2], [3, 4]]
G_M
  • 3,342
  • 1
  • 9
  • 23