Python使用while循环操作元组列表

时间:2017-09-17 08:43:56

标签: python list while-loop tuples

我正在尝试编写一个python函数,该函数使用while循环来迭代包含日期和每个日期所走步数的元组的输入列表。我的函数的两个输入是列表和许多必需的步骤。该功能需要遍历列表并计算达到所需步数所需的天数。下面是我的代码到目前为止,它适用于初始部分,但我还需要添加它,以便如果在元组的输入列表中未达到所需的步数,则该函数返回'None'。另一件事是我只能有一个return语句,因为问题是这样说的,这也是我坚持下去的部分原因。

def days_to_reach_n_steps(step_records, n):
  """DOCSTRING"""
  total_steps = 0
  counter = 0
  while total_steps < n:
      total_steps = total_steps + step_records[counter][1]
      counter = counter + 1
  return(counter)

我正在测试我的函数的示例,此特定示例应返回None,因为列表中的步骤不会达到或超过50。

step_records = [('2010-01-01',3), ('2010-01-02',2), ('2010-01-03',1)] days = days_to_reach_n_steps(step_records, 50) print(days)

2 个答案:

答案 0 :(得分:1)

在我看来,最简单的解决方案是for循环,从根本上说你想要的是迭代。您还可以使用enumerate跟踪当天:

sample_steps = [("2010-01-1", 1),
                ("2010-01-2", 3),
                ("2010-01-3", 5),
                ("2010-01-4", 7),
                ("2010-01-5", 9),
                ("2010-01-6", 11)]

def days_to_reach_n_steps(step_records, n):
    total_steps = 0
    for counter, (date, steps) in enumerate(step_records, 1):
        total_steps += steps
        if total_steps >= n:
            return counter, date

我选择了奇数作为步骤序列,因为它们的累积总和是正方形(这样可以更容易通过眼睛检查):

for boundary in range(1, 7):
    for steps in range(boundary ** 2 - 1, boundary ** 2 + 2):
        result = days_to_reach_n_steps(sample_steps, steps)
        if result:
            days, date = result
            print("{} steps in {} days (arrived at {})".format(steps, days, date))
        else:
            print("{} was unreached".format(steps))

这将返回:

0 steps in 1 days (arrived at 2010-01-1)
1 steps in 1 days (arrived at 2010-01-1)
2 steps in 2 days (arrived at 2010-01-2)
3 steps in 2 days (arrived at 2010-01-2)
4 steps in 2 days (arrived at 2010-01-2)
5 steps in 3 days (arrived at 2010-01-3)
8 steps in 3 days (arrived at 2010-01-3)
9 steps in 3 days (arrived at 2010-01-3)
10 steps in 4 days (arrived at 2010-01-4)
15 steps in 4 days (arrived at 2010-01-4)
16 steps in 4 days (arrived at 2010-01-4)
17 steps in 5 days (arrived at 2010-01-5)
24 steps in 5 days (arrived at 2010-01-5)
25 steps in 5 days (arrived at 2010-01-5)
26 steps in 6 days (arrived at 2010-01-6)
35 steps in 6 days (arrived at 2010-01-6)
36 steps in 6 days (arrived at 2010-01-6)
37 was unreached

请注意,days_to_reach_n_steps只有一个return语句,但仍然可以为return设置None 37。这是因为隐式地没有返回任何内容的函数返回None。但是,这并不符合你的0规格。如果你想让0成为例外,我建议这样做:

for counter, (date, steps) in enumerate([("start", 0)] + step_records):

答案的第一行将更改为

0 steps in 0 days (arrived at start)

这保留了算法的其余部分,因此您不需要编写边缘情况。

如果它必须是while循环,你可以稍微刻意地将for循环重写为:

def days_to_reach_n_steps(step_records, n):
    total_steps = 0
    counter = 0
    step_records = [("start", 0)] + step_records
    while counter < len(step_records):
        date, steps = step_records[counter]
        total_steps += steps
        if total_steps >= n:
            return counter, date
        counter += 1

这与for循环方法的第二次迭代完全相同($ diff <(python while.py) <(python code.py)完全退出)。

要使其适应for循环的第一次迭代,请将重新分配移至step_records并返回counter + 1

请注意,这并不是一个很好的while循环应用程序 - 也许我的意图是使用while循环练习,但我并不赞成强制使用丑陋的代码 - Python已经有了简单的习惯用法迭代列表并保持索引。请参阅the Zen of Python

答案 1 :(得分:0)

退出IndexError并在未到达时将c设置为None

def daystoreach(steprecords, n):
total = 0
x = 0
while total < n:
    try:
        total += sr[x][1]
        x += 1
    except IndexError:
        break
if total < n:
    x = None
return x