从python 2.7中的列表中删除每个第n个元素

时间:2012-11-20 16:46:59

标签: python python-2.7

我已经完成了为其创建代码的任务。任务如下:

  

你是一艘帆船的船长,你和你的船员都有   被海盗捕获。海盗船长让你们所有人都站着   在他的船甲板上的一个圆圈试图决定在哪个顺序   你应该走木板。最终他决定以下内容   方法:

     

(a)海盗船长要求你挑选一个号码。

     

(b)   第一个走板的人将是第N人(从   你)。

     

(c)然后队长将围绕圆圈继续施力   每个第N个人都可以走板。

     

(d)一旦只有一个人   离开时,那个人将获得自由。

     

例如:船员   包括:Andrew,Brenda,Craig,Deidre,Edward,Felicity,Greg和   哈丽特。安德鲁选择N = 2。船员将按顺序走板:   Brenda,Deidre,Felicity,Harriet,Craig,Greg,Edward。安德鲁会   给予自由。

我到目前为止的代码是:

def survivor(names, step):
    names =  ["Andrew", "Brenda", "Craig", "Deidre", "Edward", "Felicity", "Greg", "Harriet"]
    Next = step - 1
    names.pop(Next)
    print names

这将删除列表中的第一个第n个人,但我不知道如何遍历列表以继续删除第n个人。

我需要它,所以我们假设步骤= 3,然后我需要它去除克雷格然后从克雷格开始计算并删除下一个第三个元素,这是幸福等等,直到有一个人离开。

我该怎么做?

2 个答案:

答案 0 :(得分:6)

这似乎有效:

from collections import deque
def survivor(names, step):     
    circle = deque(names)
    while len(circle) > 1:
        circle.rotate(1-step)
        print circle.popleft()
    return circle[0]

它会打印海盗受害者的姓名并返回幸存者的姓名:

In [17]: crew = ["Andrew", "Brenda", "Craig", "Deidre",
   ....: "Edward", "Felicity", "Greg", "Harriet"]

In [18]: survivor(crew, 2)
Brenda
Deidre
Felicity
Harriet
Craig
Greg
Edward
Out[18]: 'Andrew'

In [19]: survivor(crew, 3)
Craig
Felicity
Andrew
Edward
Brenda
Harriet
Deidre
Out[19]: 'Greg'

答案 1 :(得分:1)

以下代码应该执行您要求的所有操作,包括实现safeN功能:

import collections
import itertools

def walk_plank(names, N):
    "Walk everyone down the plank."
    circle = collections.deque(names)
    while circle:
        circle.rotate(-N)
        yield circle.pop()

def save_last(names, N):
    "Save the last person from walking the plank."
    for name in walk_plank(names, N):
        pass
    return name

def safeN(names, name):
    "Find the best N to save someone from walking the plank."
    assert name in names, 'Name must be in names!'
    for N in itertools.count(1):
        if save_last(names, N) == name:
            return N

编辑:以上是在Windows中使用IDLE时上面代码的一些示例用法。

Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import collections, itertools
>>> def walk_plank(names, N):
        "Walk everyone down the plank."
        circle = collections.deque(names)
        while circle:
            circle.rotate(-N)
            yield circle.pop()

>>> def save_last(names, N):
        "Save the last person from walking the plank."
        for name in walk_plank(names, N):
            pass
        return name

>>> def safeN(names, name):
        "Find the best N to save someone from walking the plank."
        assert name in names, 'Name must be in names!'
        for N in itertools.count(1):
            if save_last(names, N) == name:
                return N

>>> names = 'Andrew Brenda Craig Deidre Edward Felicity Greg Harriet'.split()
>>> tuple(walk_plank(names, 2))
('Brenda', 'Deidre', 'Felicity', 'Harriet', 'Craig', 'Greg', 'Edward', 'Andrew')
>>> save_last(names, 2)
'Andrew'
>>> safeN(names, 'Andrew')
2
>>> safeN(names, 'Brenda')
19
>>> save_last(names, 19)
'Brenda'
>>> tuple(walk_plank(names, 19))
('Craig', 'Harriet', 'Andrew', 'Felicity', 'Deidre', 'Edward', 'Greg', 'Brenda')
>>>