In [10]: foo={'name':'lee', 'age':None, 'address':''}
In [11]: for k in foo:
if not foo[k]:
del foo[k]
....:
....:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call
last)
RuntimeError: dictionary changed size during iteration
oops
最简单的方法
In [15]: new_foo = {}
In [16]: for k,v in foo:
if v:
new_foo[k] = v
----
上面的方法很弱智,各位好人有更好的办法不?
删除字典里的value为非真的元素 ,并且该非真的元素的key是未知的
> --
> 来自: `python-cn`:CPyUG ~ 华蟒用户组 | 发言:pyth...@googlegroups.com
> 退订: http://tinyurl.com/45a9tb //针对163/qq邮箱:http://tinyurl.com/4dg6hc
> 详情: https://groups.google.com/group/python-cn
> 严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
>
--
http://zoomquiet.org 人生苦短? Pythonic!
向靠谱,反脑残! Kaopulity,小白退散! [Kaopulity~= Keep all processes usablity!]
"未知"的意思是指,key的name是不知道的。
On Feb 8, 3:35 pm, 诚子 <zhicheng1...@gmail.com> wrote:
> Python 3.1.1 (r311:74480, Feb 3 2010, 13:43:54)
> [GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.>>> a = {1:2, 3:4, 5:None}
> >>> a
>
> {1: 2, 3: 4, 5: None}
>
> >>> {key:value for key, value in a.items() if value is not None}
> {1: 2, 3: 4}
>
> 在 2010年2月8日 下午3:27,Zoom.Quiet <zoom.qu...@gmail.com>写道:
>
>
>
> > 2010/2/8 Shuge Lee <shuge....@gmail.com>:
> > > 退订:http://tinyurl.com/45a9tb//针对163/qq邮箱:http://tinyurl.com/4dg6hc
> > > 详情:https://groups.google.com/group/python-cn
> > > 严正: 理解列表! 智慧提问!http://wiki.woodpecker.org.cn/moin/AskForHelp
>
> > --
> >http://zoomquiet.org人生苦短? Pythonic!
> > 向靠谱,反脑残! Kaopulity,小白退散! [Kaopulity~= Keep all processes usablity!]
>
> > --
> > 来自: `python-cn`:CPyUG ~ 华蟒用户组 | 发言:pyth...@googlegroups.com
> > 退订:http://tinyurl.com/45a9tb//针对163/qq邮箱:http://tinyurl.com/4dg6hc
> > 详情:https://groups.google.com/group/python-cn
> > 严正: 理解列表! 智慧提问!http://wiki.woodpecker.org.cn/moin/AskForHelp
>
> --
{key:value for key, value in foo.items() if value is not None}
^
SyntaxError: invalid syntax
On Feb 8, 3:35 pm, 诚子 <zhicheng1...@gmail.com> wrote:
> Python 3.1.1 (r311:74480, Feb 3 2010, 13:43:54)
> [GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.>>> a = {1:2, 3:4, 5:None}
> >>> a
>
> {1: 2, 3: 4, 5: None}
>
> >>> {key:value for key, value in a.items() if value is not None}
> {1: 2, 3: 4}
>
> 在 2010年2月8日 下午3:27,Zoom.Quiet <zoom.qu...@gmail.com>写道:
>
>
>
> > 2010/2/8 Shuge Lee <shuge....@gmail.com>:
> > > 退订:http://tinyurl.com/45a9tb//针对163/qq邮箱:http://tinyurl.com/4dg6hc
> > > 详情:https://groups.google.com/group/python-cn
> > > 严正: 理解列表! 智慧提问!http://wiki.woodpecker.org.cn/moin/AskForHelp
>
> > --
> >http://zoomquiet.org人生苦短? Pythonic!
> > 向靠谱,反脑残! Kaopulity,小白退散! [Kaopulity~= Keep all processes usablity!]
>
> > --
> > 来自: `python-cn`:CPyUG ~ 华蟒用户组 | 发言:pyth...@googlegroups.com
> > 退订:http://tinyurl.com/45a9tb//针对163/qq邮箱:http://tinyurl.com/4dg6hc
> > 详情:https://groups.google.com/group/python-cn
> > 严正: 理解列表! 智慧提问!http://wiki.woodpecker.org.cn/moin/AskForHelp
>
> --
那好像是3的语法,
2.X里你可以这样:
>>> dict((key,value) for key, value in foo.items() if value is not None)
{1: 2, 3: 4}
>>>
--
Best Regards,
Leo Jay
退订: http://tinyurl.com/45a9tb //针对163/qq邮箱:http://tinyurl.com/4dg6hc
详情: https://groups.google.com/group/python-cn
机械唯物主义 : linjunhalida 写道:
> 总之就是循环的东西不要改动,把列表的key都取出来先。
>
> 2010/2/8 诚子 <zhiche...@gmail.com <mailto:zhiche...@gmail.com>>
>
> >>> a = {1:2, 3:4, 5:None}
> >>> b = dict(filter(lambda x:x[1] is not None, a.items()))
> >>> print b
> {1: 2, 3: 4}
> >>>
>
> 在 2010年2月8日 下午3:41,Shuge Lee <shug...@gmail.com
> <mailto:shug...@gmail.com>>写道:
>
> Python 2.5.5 (r255:77872, Feb 1 2010, 19:53:42)
>
>
> {key:value for key, value in foo.items() if value is not None}
> ^
> SyntaxError: invalid syntax
>
>
> On Feb 8, 3:35 pm, 诚子 <zhicheng1...@gmail.com
> <mailto:zhicheng1...@gmail.com>> wrote:
> > Python 3.1.1 (r311:74480, Feb 3 2010, 13:43:54)
> > [GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
> > Type "help", "copyright", "credits" or "license" for more
> information.>>> a = {1:2, 3:4, 5:None}
> > >>> a
> >
> > {1: 2, 3: 4, 5: None}
> >
> > >>> {key:value for key, value in a.items() if value is not None}
> > {1: 2, 3: 4}
> >
> > 在 2010年2月8日 下午3:27,Zoom.Quiet <zoom.qu...@gmail.com
> <mailto:zoom.qu...@gmail.com>> 写道:
> >
> >
> >
> > > 2010/2/8 Shuge Lee <shuge....@gmail.com
> <mailto:shuge....@gmail.com>>:
> > > > 更正一下限制
> >
> > > > 删除字典里的value为非真的元素 ,并且该非真的元素的key是
> 未知的
> > > 怎判定 key 是意外的?
> >
> > > > On Feb 8, 3:23 pm, Shuge Lee <shuge....@gmail.com
> <mailto:shuge....@gmail.com>> wrote:
> > > >> 删除字典里的空元素
> >
> > > >> In [10]: foo={'name':'lee', 'age':None, 'address':''}
> >
> > > >> In [11]: for k in foo:
> > > >> if not foo[k]:
> > > >> del foo[k]
> > > >> ....:
> > > >> ....:
> >
> > >
> ---------------------------------------------------------------------------
> > > >> RuntimeError Traceback
> (most recent call
> > > >> last)
> >
> > > >> RuntimeError: dictionary changed size during iteration
> >
> > > >> oops
> >
> > > >> 最简单的方法
> >
> > > >> In [15]: new_foo = {}
> >
> > > >> In [16]: for k,v in foo:
> > > >> if v:
> > > >> new_foo[k] = v
> >
> > > >> ----
> >
> > > >> 上面的方法很弱智,各位好人有更好的办法不?
> >
> > > > --
> > > > 来自: `python-cn`:CPyUG ~ 华蟒用户组 | 发言:python-
> c...@googlegroups.com <mailto:pyth...@googlegroups.com>
> > > > 退订:http://tinyurl.com/45a9tb//针对163/qq
> <http://tinyurl.com/45a9tb//%E9%92%88%E5%AF%B9163/qq>邮箱:
> http://tinyurl.com/4dg6hc
> > > > 详情:https://groups.google.com/group/python-cn
> > > > 严正: 理解列表! 智慧提问!http:
> //wiki.woodpecker.org.cn/moin/AskForHelp
> >
> > > --
> > >http://zoomquiet.org人生苦短? Pythonic!
> > > 向靠谱,反脑残! Kaopulity,小白退散! [Kaopulity~= Keep all
> processes usablity!]
> >
> > > --
> > > 来自: `python-cn`:CPyUG ~ 华蟒用户组 | 发言:python-
> c...@googlegroups.com <mailto:pyth...@googlegroups.com>
> > > 退订:http://tinyurl.com/45a9tb//针对163/qq
> <http://tinyurl.com/45a9tb//%E9%92%88%E5%AF%B9163/qq>邮箱:
> http://tinyurl.com/4dg6hc
> > > 详情:https://groups.google.com/group/python-cn
> > > 严正: 理解列表! 智慧提问!http:
> //wiki.woodpecker.org.cn/moin/AskForHelp
> >
> > --
> > my.unix-center.net/~WeiZhicheng
> <http://my.unix-center.net/%7EWeiZhicheng>
>
> --
> 来自: `python-cn`:CPyUG ~ 华蟒用户组 | 发言:python-
> c...@googlegroups.com <mailto:pyth...@googlegroups.com>
> 退订: http://tinyurl.com/45a9tb //针对163/qq邮箱:http:
> //tinyurl.com/4dg6hc
> 详情: https://groups.google.com/group/python-cn
> 严正: 理解列表! 智慧提问!
> http://wiki.woodpecker.org.cn/moin/AskForHelp
>
>
>
>
> --
> my.unix-center.net/~WeiZhicheng
> <http://my.unix-center.net/%7EWeiZhicheng>
> --
> 来自: `python-cn`:CPyUG ~ 华蟒用户组 | 发言:python-
> c...@googlegroups.com <mailto:pyth...@googlegroups.com>
删除字典里的空元素
In [10]: foo={'name':'lee', 'age':None, 'address':''}
In [11]: for k in foo:
if not foo[k]:
del foo[k]
....:
....:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call
last)
RuntimeError: dictionary changed size during iteration
oops
最简单的方法
In [15]: new_foo = {}
In [16]: for k,v in foo:
if v:
new_foo[k] = v
----
上面的方法很弱智,各位好人有更好的办法不?