如何创建一个新元素作为最后一个元素以及常数的列表



给定这些变量:

a_4th =  6.08
delta_a = 0.052

我想创建一个包含:

的列表
a = [a_4th, a_5th, a_6th, a_7th ... ]

其中:

a_5th = a_4th + delta_a
a_6th = a_5th + delta_a
a_7th = a_6th + delta_a

此代码:

a = []  # create the empty list
a.insert(0, a_4th) # initialize the 1st item of the list
for i in xrange (1,19):   # I would like the list to be 18 items long. 
                          # a[0] is defined in the previous line.
  a[i] = a[i-1] + delta_a 

正在给出此错误:

Traceback (most recent call last):
  File "so.py", line 8, in <module>
    a[i] = a[i-1] + delta_a 
IndexError: list assignment index out of range

该理解如何?

a_4th =  6.08
delta_a = 0.052
a = [a_4th + i * delta_a for i in range(20)]

或您的代码,修复

a_4th =  6.08
delta_a = 0.052
a = [a_4th]
for i in xrange (1,19):
  a.append(a[i-1] + delta_a)  # or the elegant a.append(a[-1] + delta_a) kudos @Prune

还请注意,从列表理解的速度好处中的APPART也适用于Python 2.xPython 3.x发行版,使其更易于维护。

让我们清理您的代码:

a = []  # create the empty list
a.insert(0, a_4th) # initialize the 1st item of the list

这在阅读和执行中都浪费了努力。只是初始化列表:

a = [a_4th]

至于循环,您无法将不存在的列表元素分配。您可以使用appendinsert,串联或其他定义的操作创建新列表元素。

for i in xrange (1,19):   # I would like the list to be 18 items long. 
                          # a[0] is defined in the previous line.
  a.append( a[-1] + delta_a )

是的,我从i-1删除了i;这引入了另一种错误的可能性。只需使用列表的最后一个(索引减一个(元素。

如果我是您,我会使用python发电机,因为它为您提供了表达"无限"大序列的额外表现力。

def succession(initial, delta):
  while True:        
    yield initial
    # value, then, the next time you call 
    initial += delta
values_generator = succession(6.08, 0.052)
# later we decide how many values we want to consume from the generator
for i in range(20):
  print(next(values_generator))

更新

添加有关发电机的更多解释。发电机是Python提供的一种机制,可以构建有效的数据处理管道。

说您有一个从0到100_000的数字列表,并且要对其进行一些转换,例如过滤,映射等等,依此类推,如果您使用常规的Python2 mapfilter,则您的解决方案不会很好地扩展,因为在每个步骤(过滤,映射等(中,将计算所有新的中间结果列表,其中可能会在最终结果中使用许多元素。构建这些功能性管道的关键思想是,我们希望它们成为组合,即我们可以组合的小功能以创建以后可以组合的新功能,但我们不想支付绩效价格。发电机基本上是在Python中解决此问题(在Clojure中,您有传感器和Haskell Lazy评估(。使用发电机Python不会构建中间列表,而是要在管道的一端推出一个值,将其转换并弹出另一端,这是您希望转换的初始数据集中的每个元素。

生成器的一个简单示例是:

def trivial_generator():
  print "running until first yield..."
  yield 1
  print "running until second yield..."
  yield 2
  print "exiting..."

generator = trivial_generator()
result = generator.next()
print "got result ", result
result = generator.next()
print "got result ", result
generator.next()

如果您运行的是:

running until first yield...
got result  1
running until second yield...
got result  2
exiting...
Traceback (most recent call last):
  File "foo.py", line 14, in <module>
    generator.next()
StopIteration

说明了发电机的行为:

  1. 您通过调用包含yield的函数来创建一个
  2. 每当您在其上调用.next()方法,或将其传递给next(generator)功能,发电机的正文将被执行,直到找到yield
  3. 当找到yield时,收益率中指定的值将返回,发电机将停留 paused ,直到您再次调用其.next()方法,此时,它将简历从最后一点开始暂停(请注意,发电机的所有内部状态都保持在呼叫之间(
  4. 如果您在发电机上调用.next(),并且找不到其他yield(这意味着它的末端(,则提高了StopIteration(这非常方便,因为这是例外for循环期望在eN d上发生。迭代器(

在我使用的示例解决方案中,我利用了发电机的暂停/简历功能,通过将yield放入无限循环中,我得到了一个生成器,它将给我带来我有效地需要的值。

简单的简单解决方案:

a = []
a.append(a_4th)
for i in xrange(1, 19):
    a.append(a[i-1] + delta_a)

但给定您的定义:

a_5th = a_4th delta_a

a_6th = a_5th delta_a

a_7th = a_6th delta_a

我们可以简单地将其视为:

a_5th = a_4th (delta_a * 1(

a_6th = a_4th (delta_a * 2(

a_7th = a_4th (delta_a * 3(

并将其写成简单(更快(的列表理解,CF EV Kounis答案。

相关内容

最新更新