How do I filter out non-string keys in a dictionary in Python?

前端 未结 5 1492
星月不相逢
星月不相逢 2021-01-24 07:13

I\'m new to Python and trying to figure out how to filter out all the non-string keys in a dictionary. I appreciate any help you can provide.

5条回答
  •  甜味超标
    2021-01-24 07:46

    For sake of variety of answers, here is another method:

    >>> d = {1: 'ONE', 2: 'TWO', 3: 'THREE', 'T': 'THREE'}
    >>> b = {k:d[k] for k in filter(lambda s: type(s) is int, d.iterkeys())}
    >>> b
    {1: 'ONE', 2: 'TWO', 3: 'THREE'}
    

提交回复
热议问题