python 来自http://stackoverflow.com/questions/12325608/iterate-over-a-dict-or-list-in-python/12325691#

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 来自http://stackoverflow.com/questions/12325608/iterate-over-a-dict-or-list-in-python/12325691#相关的知识,希望对你有一定的参考价值。

>>> def seq_iter(obj):
...     return obj if isinstance(obj, dict) else xrange(len(obj))
... 
>>> x = [1,2,3]
>>> for i in seq_iter(x):
...     x[i] = 99
... 
>>> x
[99, 99, 99]
>>> 
>>> x = {1: 2, 2:3, 3:4}
>>> for i in seq_iter(x):
...     x[i] = 99
... 
>>> x
{1: 99, 2: 99, 3: 99}

以上是关于python 来自http://stackoverflow.com/questions/12325608/iterate-over-a-dict-or-list-in-python/12325691#的主要内容,如果未能解决你的问题,请参考以下文章