Extracting values of elements in a list of dictionaries

后端 未结 5 712
甜味超标
甜味超标 2021-01-29 11:46

I have a python data-structure as follows:

A = [{\'abc\': \'kjkjl\'},{\'abc\': \'hjhjh\'},{\'abc\': \'78787\'}]

How can I remove the \'abc\' fr

相关标签:
5条回答
  • 2021-01-29 12:08
    B = []
    for key, value in A.iteritems():
        B.append(value)
    
    0 讨论(0)
  • 2021-01-29 12:10

    Given your list as

    >>> A
    [{'abc': 'kjkjl'}, {'abc': 'hjhjh'}, {'abc': '78787'}]
    

    You can do something like

    >>> list(itertools.chain(*[x.values() for x in A]))
    ['kjkjl', 'hjhjh', '78787']
    >>> 
    
    0 讨论(0)
  • 2021-01-29 12:13

    You can use list comprehension:

    B = sum((i.values() for i in A), [])
    
    0 讨论(0)
  • 2021-01-29 12:21
    >>> B = [a["abc"] for a in A]
    >>> B
    ['kjkjl', 'hjhjh', '78787']
    
    0 讨论(0)
  • 2021-01-29 12:30

    If all the dicts in A only have one element, you can do this ...

    >>> A = [{'abc': 'kjkjl'},{'abc': 'hjhjh'},{'abc': '78787'}]
    >>> B = [x.values()[0] for x in A]
    >>> B
    ['kjkjl', 'hjhjh', '78787']
    
    0 讨论(0)
提交回复
热议问题