我有一个defaultdict(dict)
值:如何从现有密钥设置新密钥:defaultdict的值对?
defaultdict(<class 'dict'>, {
'AL2G22360.t1_Sp': {
'locus': 'AL2G22360.t1',
'length': '663',
'strain': 'Sp'},
'AL2G22360.t1_My': {
'locus': 'AL2G22360.t1',
'length': '389',
'strain': 'My'},
'AL2G22220.t1_My': {
'locus': 'AL2G22220.t1',
'length': '865',
'strain': 'My'},
'AL2G22220.t1_My': {
'locus': 'AL2G22220.t1',
'length': '553',
'strain': 'My' ........}})
说,我想改变它的方式,在major key
是value of variable **locus**
。因为有一个locus value
(非唯一,但有些可能是唯一的)的副本,我想从variable **strain**
有另一个子键My vs. Sp
。其余数据可以保持原样。
预期输出:
defaultdict(<class 'dict'>, {
'AL2G22360.t1': {
'Sp': {
'keys': 'AL2G22360.t1_Sp',
'length': '663'},
'My': {
'keys': 'AL2G22360.t1_My',
'length': '389'}},
'AL2G22220.t1': {
'My': {
'keys': 'AL2G22220.t1_My',
'length': '865'},
'Sp': {
'keys': 'AL2G22220.t1_Sp',
'length': '553'}, .....}})
“期望的输出”在语法上不正确。我猜你的意思是什么,并据此编辑你的问题。请确认我没有误解你。 – Alfe
@Alfe:你做得很好。谢谢。我的头痛如何解决这个问题。你有任何提示吗? – everestial007
我刚更新了'variable keys',以确保问题很简洁。但是,这个问题的意图是一样的。 – everestial007