Python 2.7:从特殊格式的列表对象中创建字典对象

kmo*_*oor 4 python dictionary list python-2.7

我有一个列表类型对象,如:

     f = [77.0, 'USD', 77.95, 
     103.9549, 'EUR', 107.3634,
     128.1884, 'GBP', 132.3915,
     0.7477, 'JPY', 0.777]
Run Code Online (Sandbox Code Playgroud)

我想创建一个如下的字典:

d = 
{'EUR': [103.9549, 107.3634],
'GBP': [128.1884, 132.3915],
'JPY': [0.7477, 0.777],
'USD': [77.0, 77.95]}
Run Code Online (Sandbox Code Playgroud)

我试图利用这些答案将列表转换为Python中的字典,并使用python从列表中创建字典.

但是,无法找到正确的方法.

截至目前,我的解决方案是:

cs = [str(x) for x in f if type(x) in [str, unicode]]
vs = [float(x) for x in f if type(x) in [int, float]]
d =  dict(zip(cs, [[vs[i],vs[i+1]] for i in range(0,len(vs),2)]))
Run Code Online (Sandbox Code Playgroud)

但是,什么是聪明的单线?

NPE*_*NPE 7

怎么样:

In [5]: {f[i+1]: [f[i], f[i+2]] for i in range(0, len(f), 3)}
Out[5]: 
{'EUR': [103.9549, 107.3634],
 'GBP': [128.1884, 132.3915],
 'JPY': [0.7477, 0.777],
 'USD': [77.0, 77.95]}
Run Code Online (Sandbox Code Playgroud)