原始资料来源:
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'...]
如何做到更聪明?
这是我的代码中有太多命令:
`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() `>>> 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})`
`>>>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]`
>>>bar = bar[1:-3]
>>>bar
[{'Number': ['int', 'bool', 'float', 'complex'], 'Literal': ['str', 'None', 'bytes']}, {'Sequence': ['list', 'dict', 'tuple', 'bytearray'], 'Set': ['set', 'frozenset']}]
>>>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']])]
>>>zot = [list(i.values()) for i in bar]
>>>zot
>>>[[['int', 'bool', 'float', 'complex'], ['str', 'None', 'bytes']], [['list', 'dict', 'tuple', 'bytearray'], ['set', 'frozenset']]]
>>>str(zot)
"[[['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 = 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(zot)
('int', 'bool', 'float', 'complex', 'str', 'None', 'bytes', 'list', 'dict', 'tuple', 'bytearray', 'set', 'frozenset')
>>>list(eval(zot))
['int', 'bool', 'float', 'complex', 'str', 'None', 'bytes', 'list', 'dict', 'tuple', 'bytearray', 'set', 'frozenset']
可以做得更聪明吗?
答案 0 :(得分:1)
您可以使用列表理解:
var MotivoLocalID = motivoItems[motivoPicker.SelectedIndex].MotivoLocalID;