如何将 'yield' 生成器结果存储在 Python 2.7 中的列表中?

How to store 'yield' generator result in a list in Python 2.7?

我有这样的代码:

import math    
nList = [[[0, 0, 0], [3, 2, 1]],\
     [[]],\
     [[1, 1, 12]],\
     [[0, 0, 0], [30000, 40, 3010], [32000, 40500, 7520], [0, 0, 10520]],\
     [[15340, 0, 0], [104300, 0, 3630], [434000, 4434000, 63460]]]

def calculate_length(x1, y1, x2, y2):
    return math.sqrt((x1-x2)**2 + (y1 - y2)**2)

def calculate_time (t1,t2):
    return abs(t1-t2)

def lengths(trace):
    previous_x, previous_y = 0, 0
    for index, point in enumerate(trace):
        if point:
            x, y, t = point
            if index > 0:
                yield calculate_length(x, y, previous_x, previous_y)
            previous_x, previous_y = x, y

如何将 calculate_length(x, y, previous_x, previous_y)yield 结果存储在列表中?

nList 中有 traces,每个 tracepoints 和 3 个元素,[x,y,t]。我需要存储每条迹线的长度,以便生成的输出为:

all_lengths_list=[[3]],[[]],[[]],[[30000.02667],[40509.40138],[51616.37337]],[[88960],[4446240]]]

只需使用list:

result = list(lengths(trace))

你可以通过

获得yield
l = lengths(nList)
next(l) # gives 1st yield
next(l) # gives 2nd

等等

将所有内容都放在一个列表中

>>>list(lengths(nList))

您不需要那些续行反斜杠 - 现代 Python 足够聪明,可以发现列表定义不完整,因此它会自动为您进行续行。

此外,不需要为了计算平方根而导入数学模块,您可以使用内置的幂运算符:** 0.5.

要获得您想要的输出,您需要将 nList 中的跟踪列表一次一个地提供给您的生成器,将结果数据捕获到列表中,然后保存在 all_lengths_list 中。这可以在普通的 for 循环中完成,但使用列表理解更紧凑,如下面的代码所示。要显示 all_lengths_list 的内容,我使用 pprint 模块中的 pprint() 并不漂亮,但总比把它全部放在一行要好。 :)

#!/usr/bin/env python

from pprint import pprint

nList = [
    [[0, 0, 0], [3, 2, 1]],
    [[]],
    [[1, 1, 12]],
    [[0, 0, 0], [30000, 40, 3010], [32000, 40500, 7520], [0, 0, 10520]],
    [[15340, 0, 0], [104300, 0, 3630], [434000, 4434000, 63460]]
]

def calculate_length(x1, y1, x2, y2):
    return ((x1-x2)**2 + (y1 - y2)**2) ** 0.5

def calculate_time (t1,t2):
    return abs(t1-t2)

def lengths(trace):
    previous_x, previous_y = 0, 0
    for index, point in enumerate(trace):
        if point:
            x, y, t = point
            if index > 0:
                yield calculate_length(x, y, previous_x, previous_y)
            previous_x, previous_y = x, y

all_lengths_list = [list(lengths(trace)) for trace in nList]

pprint(all_lengths_list, indent=4)

输出

[   [3.6055512754639891],
    [],
    [],
    [30000.026666654816, 40509.401377951763, 51616.373371247231],
    [88960.0, 4446240.8942836197]]

代码中 nList 的定义不完整,需要几个右括号 ] 才能在语法上正确。此外,一旦某个内容的定义以开头 ([ 开始,则无需显式添加行继续符 \ 反斜杠字符(如果它随后分布在多个后续行中)。这使它们更容易编写,并且看起来更干净自然。

通过该更正,您可以将 yieldlengths() 函数编辑的值存储在 list 中,如下所示:

all_lengths_list = [[length for length in lengths(trace)] for trace in nList]

或者更简洁,像这样:

all_lengths_list = list((list(lengths(trace)) for trace in nList))

我还想提一些您可以简化和优化代码的事情。除了 sqrt(),内置的 math 模块还有一个 hypot() 函数,可以像 calculate_length() 函数一样轻松计算距离,并且会加快速度,因为更多的数学运算将在模块的 C 代码中完成,而不是 Python.

我注意到的另一件事是您的 lengths() 功能似乎过于复杂,可以通过使用 Python 的一些更高级的功能来大大简化。对于初学者,为什么不直接 return 点列表本身,而不是一次产生一个点的片段。

除此之外,Python 的 iterools 模块包含许多基于它们的函数和方法,可以轻松执行迭代操作,例如计算。特别是有一个名为 pairwise() 的生成器函数的配方,它以元组或成对的形式生成序列的元素,这使得它对于计算距离非常有用,就像在 lengths() 中所做的那样。

以下是您的代码的修改版本,其中包含上述所有更正和建议。我还在最后添加了一些东西,以一种相当易读的格式显示结果。

import itertools
import math

nList = [[[0, 0, 0], [3, 2, 1]],
         [[]],
         [[1, 1, 12]],
         [[0, 0, 0], [30000, 40, 3010], [32000, 40500, 7520], [0, 0, 10520]],
         [[15340, 0, 0], [104300, 0, 3630], [434000, 4434000, 63460]]]

def calculate_length(x1, y1, x2, y2):
    return math.hypot(x1-x2, y1-y2)

def calculate_time (t1,t2):
    return abs(t1-t2)

def pairwise(iterable):  # see http://preview.tinyurl.com/mzbfqlt
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = itertools.tee(iterable)
    next(b, None)
    return itertools.izip(a, b)

def lengths(trace):
    return [calculate_length(x1, y1, x2, y2)
                for (x1, y1, t1), (x2, y2, t2) in pairwise(trace)]

all_lengths_list = list(lengths(trace) for trace in nList)

for pts_list, length_list in zip(nList, all_lengths_list):
    print('   points: {}\n'
          'distances: [{}]'.format(
              pts_list,
              ', '.join((format(length, '.2f') for length in length_list))))

输出:

   points: [[0, 0, 0], [3, 2, 1]]
distances: [3.61]
   points: [[]]
distances: []
   points: [[1, 1, 12]]
distances: []
   points: [[0, 0, 0], [30000, 40, 3010], [32000, 40500, 7520], [0, 0, 10520]]
distances: [30000.03, 40509.40, 51616.37]
   points: [[15340, 0, 0], [104300, 0, 3630], [434000, 4434000, 63460]]
distances: [88960.00, 4446240.89]