【问题标题】:Unable to return none for data from list of tuples无法为元组列表中的数据返回无
【发布时间】:2018-09-28 00:41:49
【问题描述】:

我的函数应该使用一个元组列表并返回车辆达到或超过 n 所需的天数,从列表中的第一天开始,只使用一个 while 循环和一个 return 语句。

def days_to_reach_n_vehicles(vehicle_records, n):
    """Returns number of days taken to reach or exceed a total of n vehicles"""
    cumulative_total = 0
    num_days = 0
    index = 0

    while index < len(vehicle_records):
        cumulative_total += vehicle_records[index][1]
        index += 1
        num_days += 1

        if cumulative_total >= n:
            break

    return num_days

它给了我以下测试代码的正确输出 2:

some_records = [('2010-01-01',1),
                ('2010-01-02',2),
                ('2010-01-03',3)]
days = days_to_reach_n_vehicles(some_records, 3)
print(days)

但是如果在vehicle_records 中的最后一天没有到达n 辆车,它需要返回None。我无法让它为下一个测试数据返回 None,有人可以告诉我我需要修复什么吗?

some_records = [('2010-01-01',1),
                ('2010-01-02',2),
                ('2010-01-03',3)]
days = days_to_reach_n_vehicles(some_records, 40)
print(days)

【问题讨论】:

  • 我的测试代码输出为 1
  • 什么情况下不应该返回None?
  • 我认为您需要将return num_days 移回缩进。目前它每次都返回第一次迭代。或者取决于你想要什么 - 你可能想把它放在你的 if cumulative_total &gt;= n: 块中。编辑:我看到你现在已经改变了。
  • return num_days if cumulative_total &gt;= n else None 应该是你要找的。​​span>
  • 将您的break 替换为return num_days。并且没有第二次返回 - 该函数将自动返回None。这应该可以解决它。

标签: python python-3.x list while-loop tuples


【解决方案1】:

也许你需要return num_days if cumulative_total &gt;= n else None

def days_to_reach_n_vehicles(vehicle_records, n):
    """Returns number of days taken to reach or exceed a total of n vehicles"""
    cumulative_total = 0
    num_days = 0
    index = 0

    while index < len(vehicle_records):
        cumulative_total += vehicle_records[index][1]
        index += 1
        num_days += 1

        if cumulative_total >= n:
            break

    return num_days if cumulative_total >= n else None

【讨论】:

  • 如何更改它以返回日期而不是天数,例如对于上面的第一个测试代码,我需要它返回 2010-01-02
  • 在休息之前,设置date_var = vehicle_records[index][0]return date_var。此外,当您初始化这些零值时,初始化 date_var = None。您可以从您的退货声明中删除if cumulative_total &gt;= n else None,因为date_var 将是None,除非您点击了那个休息时间
  • 我需要它来返回车辆总数达到或超过n的日期。目前对于上述测试代码 (n=3),它返回 2010-01-03 而不是 2010-01-02,您能告诉我需要更改的地方吗?
  • 你能发布一个新问题吗?只是为了让这个更干净。 cmets的文本编辑器不太好用(写代码时)
【解决方案2】:

您应该使用 for 循环来迭代记录,如下所示:

def days_to_reach_n_vehicles(vehicle_records, n):
    """Returns number of days taken to reach or exceed a total of n vehicles"""
    cumulative_total = 0
    num_days = 0

    for date, number_of_vehicle in vehicle_records:
        cumulative_total += number_of_vehicle
        num_days += 1
        if cumulative_total >= n:
            break
    else:
        num_days = None  # a break never happens in the loop

    return num_days

如果你想玩一些 python 的乐趣:

from itertools import accumulate

def days_to_reach_n_vehicles(vehicle_records, n):
    n_vehicule_per_day = list(zip(*vehicle_records))[1]
    for num_days, cumsum in enumerate(accumulate(n_vehicule_per_day)):
        if cumsum >= n: return num_days + 1
    return None

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 2014-06-05
    • 1970-01-01
    • 1970-01-01
    • 2014-04-28
    • 2020-04-17
    • 2011-07-05
    相关资源
    最近更新 更多