给我一份排名投票名单和一份要淘汰的候选人名单,我需要返回一份新的排名投票名单,其中要淘汰的候选人名单中的所有候选人都已被删除
这是我到目前为止试过的代码
import doctest
def eliminate_candidate(ballots, to_eliminate):
'''
(list, list) -> list
>>> eliminate_candidate([['NDP', 'LIBERAL'], ['GREEN', 'NDP'], \
['NDP', 'BLOC']], ['NDP', 'LIBERAL'])
[[], ['GREEN'], ['BLOC']]
>>> eliminate_candidate([], [])
[]
>>> eliminate_candidate([[]], [])
[[]]
'''
new_list = []
# Copy ballots onto new list
for item in ballots:
new_list.append(item)
for item in new_list:
for element in item:
if element in to_eliminate:
item.remove(element)
return new_list
我的第一次doctest失败了,结果是:
[['LIBERAL'], ['GREEN'], ['BLOC']]
这是必需的功能。它搜索子列表:
输出:
使用
sets
变得非常简单或:
印刷品
相关问题 更多 >
编程相关推荐