【问题标题】:Generalize a formula for an entire list of lists泛化整个列表列表的公式
【发布时间】:2018-11-20 20:07:09
【问题描述】:

我想这样做

firstdates = dates[0]
paystring = []
for i in range(len(payment_months)):
    if payment_months[i] < firstdates[i]:
        paystring.append(0)
    else:
        paystring.append(((payment_months[i] + 12 - firstdates[i]) + 1) % 12)
print(paystring)

但我想对列表列表中的所有日期执行此操作。例如,让我们只关注前两行日期。

print(dates[0:2])

给我

[[8, 8, 7, 7, 6, 5, 4, 4, 11, 10, 10, 8], [7, 6, 3, 2, 2, 2, 1, 0, 11, 10, 10, 9]]

print(payment_months)

给我

[8, 7, 6, 5, 4, 3, 2, 1, 12, 11, 10, 9]

然后运行后:

paystring = []
for i in range(len(payment_months)):
    paystring.append([])
    for j in range(len(dates[i])):
        if payment_months[i] < dates[i][j]:
            paystring[i].append(0)
        elif NDD_day[j] > 1:
            paystring[i].append((payment_months[i] + 12 - dates[i][j]) % 12)
        else:
            paystring[i].append( ((payment_months[i] + 12 - dates[i][j]) + 1) % 12)
print(paystring[0:2])

我应该得到这个:

[[1, 0, 0, 0, 0, 0, 0, 0, 2, 2, 1, 2], [2, 2, 4, 4, 3, 2, 2, 2, 2, 2, 1, 1]]

但我得到了这个:

[[0, 0, 1, 1, 2, 3, 4, 5, 0, 0, 0, 0], [0, 1, 4, 5, 5, 5, 6, 8, 0, 0, 0, 0]]

有人可以帮我修复我的代码吗?

这是一个手写的例子,应该很清楚:

我尝试将我的代码更改为:

# Calculate paystring
paystring = []
for i in range(len(payment_months)):
    paystring.append([])
    for j in range(len(dates[i])):
        if payment_months[i] < dates[j][i]:
            paystring[i].append(0)
#         elif NDD_day[j] > 1:
#             paystring[i].append((payment_months[i] + 12 - dates[j][i]) % 12)
        else:
            paystring[i].append( ((payment_months[i] + 12 - dates[j][i]) + 1) % 12)
print(paystring[0:2])

这给了我这个:

[[1, 2, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1], [0, 2, 0, 0, 0, 2, 0, 0, 1, 1, 0, 1]]

希望这会有所帮助,我不知道为什么这会令人困惑,也许我没有最好地解释这一点。

【问题讨论】:

  • 您能用文字而不是代码来解释您想要实现的目标吗?如果您的代码没有按照您的意愿执行,那么我们需要知道您认为它应该执行的操作。
  • @AlexanderReynolds 一定让我编辑一下
  • @AlexanderReynolds 我用一张图片发了一个帖子,应该可以清楚地了解我想要完成的工作
  • 您(几乎是逐字逐句地)复制了您拥有的代码并将其放在一张纸上。我不认为这就是亚历山大雷诺兹的意思。使用简单的英语来描述你想要什么。不要诉诸代码,这意味着没有循环,没有索引(如i),没有类似的东西。 描述你想做什么。您的代码在某些方面显然是错误的,因此您无法用代码描述问题。非常不清楚(至少对我来说)你实际上想要做什么。 “我期待这个但得到这个”,但是为什么你期待一个胜过另一个?
  • 翻译没有骗你。我的意思是,你说你想要,你输入了,它给了你的。但是您对问题的描述是,当我输入 时,我期望 并且得到 。你看到混乱了吗?您正在使用显然没有按照您的预期执行的代码循环地描述问题,但不清楚为什么您期望有所不同。也许一个接一个地遍历每个项目/行,并在一个较小的例子中展示你想要做什么?

标签: python list numpy matrix


【解决方案1】:

在摆弄了一下之后,最终很明显你为你的payment_months 使用了错误的索引,并且如果你想要dates 中的每一行,你正在迭代错误的range

设置:

In [35]: dates

Out[35]:
[[8, 8, 7, 7, 6, 5, 4, 4, 11, 10, 10, 8],
 [7, 6, 3, 2, 2, 2, 1, 0, 11, 10, 10, 9]]

In [37]: payment_months
Out[37]: [8, 7, 6, 5, 4, 3, 2, 1, 12, 11, 10, 9]

那么我们有:

paystring = []
for i in range(len(dates)): # len(payment_months) -> len(dates)
    paystring.append([])
    for j in range(len(dates[i])):
        if payment_months[j] < dates[i][j]: # Switched from i to j
            paystring[i].append(0)
        else:
            paystring[i].append( ((payment_months[j] + 12 - dates[i][j]) + 1) % 12) # Switched from i to j

输出:

In [42]: paystring
Out[42]: [[1, 0, 0, 0, 0, 0, 0, 0, 2, 2, 1, 2], [2, 2, 4, 4, 3, 2, 2, 2, 2, 2, 1, 1]]

根据需要。

您实际上可以完全避免这个问题,只需要更 Pythonic 一点(避免索引,只访问元素)。这也更清楚地表明dates 中的每一行与payment_months 是一一对应的:

paystring = []
for date_row in dates:
    paystring.append([])
    for date, payment_month in zip(date_row, payment_months):
        if payment_month < date:
            paystring[-1].append(0)
        else:
            paystring[-1].append( ((payment_month + 12 - date) + 1) % 12)

HTH。

【讨论】:

猜你喜欢
  • 2021-11-20
  • 2013-11-15
  • 2021-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-11
  • 1970-01-01
相关资源
最近更新 更多