当我想要数据时获取生成器对象返回 [英] Getting a Generator Object returned when I want the data

查看:134
本文介绍了当我想要数据时获取生成器对象返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python 3的新手,所以请保持友善.

I am very very new to Python 3, so please be nice.

我查看了所有可以找到的与此相关的文档-确实有很多,只是我仍然不知道该怎么做.

I have looked at all the documentation I could find regarding this - there does seem to be a lot, it's just I still cannot figure out what to do.

这是我的代码:

poly = [[36606.0,53223.0],[37332.0,52224.0],[39043.0,53223.0],
[41603.0,53223.0],[42657.0,53278.0],[43123.0,52060.0],
[44054.0,51156.0],[45806.0,51498.0],[46751.0,53237.0],
[46983.0,54606.0],[45861.0,57057.0],[44971.0,58836.0],
[44054.0,60616.0],[43451.0,58138.0],[41850.0,59179.0],
[40850.0,60370.0],[39906.0,59233.0],[38674.0,59179.0],
[35566.0,56249.0],[37592.0,57536.0]]

def perimeter():
     return (sum((abs((x1 - x0)**2)+abs((y1 - y0)**2))**.5)
                        for ((x0, y0), (x1, y1)) in parts1(poly))

def parts1(poly):
    return poly[0:] + [poly[0]]

print(perimeter())

它正在运行而没有任何错误,但我得到的回报是:

It is running without any errors but I am getting a return of:

generator object perimeter.locals.genexpr at 0x7f47d8671c50

如何使它具有价值/答案? 我真的不确定发生了什么.

How do I make it so that it gives me the value/answer? I am really not sure what is going on.

推荐答案

第一个问题是括号放在错误的位置.您想在生成器表达式上调用sum(),但是相反,您已经使用sum()编写了生成器表达式.因此,您可以尝试以下方法:

The first problem is that you have your parentheses in the wrong places. You want to call sum() on a generator expression, but instead you have written a generator expression using sum(). So you might try this:

def perimeter():
    return sum(((x1 - x0) ** 2) + ((y1 - y0) ** 2) ** .5
                 for (x0, y0), (x1, y1) in parts1(poly))

(我也删除了您的abs()呼叫,因为对数字进行平方将使其为正,使abs()不相关,并删除了一些不必要的括号.)

(I have also taken out your abs() calls, since squaring a number will make it positive, making abs() irrelevant, and deleted some unnecessary parentheses.)

但这仍然行不通:现在您获得'float' object is not iterable.

But this still doesn't work: now you get 'float' object is not iterable.

这是因为您试图从列表的每个元素中解压缩四个值,但是每个元素仅包含两个. Python将每个元素解压缩为两个浮点数,然后尝试将每个浮点解压缩为两个变量.这是错误消息抬起头的地方.

This is because you are trying to unpack four values from each element of the list, but each element contains only two. Python unpacks each element to two floats, then tries to unpack each float to two variables. This is where the error message rears its ugly head.

因此,您需要更改parts1()以返回列表列表中的列表列表.也就是说,列表中的每个项目都是一个列表,其中包含两个列表,每个列表包含一个点(给定点及其后继点)的坐标.一种方法是将内置的zip()函数与列表的偏移量或旋转副本一起使用.

So you need to change parts1() to return a list of a list of lists of lists. That is, each item in the list is a list, which contains two lists, each containing the coordinates of a point (a given point and its successor). One way to do this is to use the built-in zip() function with an offset or rotated copy of the list.

def parts1(poly):
    return zip(poly, poly[1:] + poly[:1])

最后,您实际上并不需要单独的功能parts1(),它可以直接在perimeter()功能中使用.并且您应该将poly传递到perimeter().

Finally, you don't really need the separate function parts1()—it can go right in the perimeter() function. And you should pass poly into perimeter().

def perimeter(poly):
    return sum(((x1 - x0) ** 2) + ((y1 - y0) ** 2) ** .5
                 for (x0, y0), (x1, y1) in zip(poly, poly[1:] + poly[:1]))

您可以通过遍历poly中的坐标并跟踪看到的最后一项,而无需列表的额外副本.但是您不能将其写为生成器表达式.相反,您应该使用常规的for循环.

You could do this without the extra copy of the list by iterating over the coordinates in poly and keeping track of the last items you saw. But you couldn't write it as a generator expression. Instead you'd use a regular for loop.

def perimeter(poly):
    x0, y0 = poly[-1][0], poly[-1][1]
    per = 0.0
    for x1, y1 in poly:
        per += ((x1 - x0) ** 2) + ((y1 - y0) ** 2) ** .5
        x0, y0 = x1, y1
    return per 

不够简洁,也没有那么快,但是并没有占用太多内存.

Not quite as succinct, and not as fast either, but doesn't use as much memory.

这篇关于当我想要数据时获取生成器对象返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆