将多个词典中的元素提取到列表中?

时间:2017-08-11 00:21:13

标签: python

原始资料来源:

data_type = {'Number': ['int','bool','float','complex'],
             'Literal':['str','None','bytes']}
data_struture = {'Sequence':['list', 'dict', 'tuple','bytearray'],
                 'Set': ['set', 'frozenset']}
.
.
.
and others

我想要这个:

['int','bool','float','complex','str','None','bytes'...]

如何做到更聪明?

这是我的代码中有太多命令:

1。将它们打包成一个类

`class TableContent:
    data_type = {'Number': ['int','bool','float','complex'],
                 'Literal':['str','None','bytes']}
    data_struture = {'Sequence':['list', 'dict', 'tuple','bytearray'],
                     'Set': ['set', 'frozenset']}`

`>>> foo = TableContent`
通过vars()

2.get foo'属性
`>>> vars(foo)
mappingproxy({'__module__': '__main__', 'data_type': {'Number': ['int', 'bool', 'float', 'complex'], 'Literal': ['str', 'None', 'bytes']}, 'data_struture': {'Sequence': ['list', 'dict', 'tuple', 'bytearray'], 'Set': ['set', 'frozenset']}, '__dict__': <attribute '__dict__' of 'TableContent' objects>, '__weakref__': <attribute '__weakref__' of 'TableContent' objects>, '__doc__': None})`

3.将mappingproxy更改为dict:

`>>>bar = dict(vars(foo))`
`>>>bar
['__main__', {'Number': ['int', 'bool', 'float', 'complex'], 'Literal': ['str', 'None', 'bytes']}, {'Sequence': ['list', 'dict', 'tuple', 'bytearray'], 'Set': ['set', 'frozenset']}, <attribute '__dict__' of 'TableContent' objects>, <attribute '__weakref__' of 'TableContent' objects>, None]`

4。切片限定元素

>>>bar = bar[1:-3]
>>>bar
[{'Number': ['int', 'bool', 'float', 'complex'], 'Literal': ['str', 'None', 'bytes']}, {'Sequence': ['list', 'dict', 'tuple', 'bytearray'], 'Set': ['set', 'frozenset']}]

5。从dic列表中获取值

>>>baz = [ i.values() for i in bar]
>>>baz
[dict_values([['int', 'bool', 'float', 'complex'], ['str', 'None', 'bytes']]), dict_values([['list', 'dict', 'tuple', 'bytearray'], ['set', 'frozenset']])]

6。将视图对象更改为列表

>>>zot = [list(i.values()) for i in bar]
>>>zot
>>>[[['int', 'bool', 'float', 'complex'], ['str', 'None', 'bytes']], [['list', 'dict', 'tuple', 'bytearray'], ['set', 'frozenset']]]

7。将列表更改为字符串

>>>str(zot)
"[[['int', 'bool', 'float', 'complex'], ['str', 'None', 'bytes']], [['list', 'dict', 'tuple', 'bytearray'], ['set', 'frozenset']]]"

8。操纵字符串

>>>zot = zot.replace('[', '')
"'int', 'bool', 'float', 'complex'], 'str', 'None', 'bytes']], 'list', 'dict', 'tuple', 'bytearray'], 'set', 'frozenset']]]"
>>>zot = zot.replace(']', '')
"'int', 'bool', 'float', 'complex', 'str', 'None', 'bytes', 'list', 'dict', 'tuple', 'bytearray', 'set', 'frozenset'"

尝试将字符串转换为列表

>>>zot.split()
["'int',", "'bool',", "'float',", "'complex',", "'str',", "'None',", "'bytes',", "'list',", "'dict',", "'tuple',", "'bytearray',", "'set',", "'frozenset'"]

调用eval()

>>>eval(zot)
('int', 'bool', 'float', 'complex', 'str', 'None', 'bytes', 'list', 'dict', 'tuple', 'bytearray', 'set', 'frozenset')

9。然后列出

>>>list(eval(zot))
['int', 'bool', 'float', 'complex', 'str', 'None', 'bytes', 'list', 'dict', 'tuple', 'bytearray', 'set', 'frozenset']

可以做得更聪明吗?

1 个答案:

答案 0 :(得分:1)

您可以使用列表理解:

var MotivoLocalID = motivoItems[motivoPicker.SelectedIndex].MotivoLocalID;