开发者

pythonic way of finding a value in a list which hasn't been supplied

Here is an example of what I mean:

a = 0
b = 1

c = range(3)

so I would like to find the missing number in the list which in this case would be 2.

The way I have programmed it at the moment is cumbersome and ugly.

If there was a function opposite to开发者_C百科 list.append() so that I could remove values from the list instead that would be great to.

Thanks


Use sets:

>>> a = 0
>>> b = 1
>>> c = range(3)
>>> set(c) - set([a, b])
set([2])


Use set difference by converting the list to a set, then preforming the set difference operation.

>>> supplied_list = [0, 1]
>>> list(set(range(3)) - set(supplied_list))
[2]


list.remove will remove the first occurrence of a given value from a list. If you want to specifically remove the last item (the one that was appended), use list.pop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜