Python化方式来转换http://stardict.sourceforge.net/Dictionaries.php下载列表变为namedtuples名单 [英] Pythonic way to convert list of dicts into list of namedtuples

查看:167
本文介绍了Python化方式来转换http://stardict.sourceforge.net/Dictionaries.php下载列表变为namedtuples名单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有字典列表。需要将其转换为列表 namedtuple 的(preferred)或简单的元组,而用空格来分割第一个变量。

更重要的是Python的方式做到这一点?

我简化了我的$ C C一点点$。 COM prehensions,创EX pressions和itertools使用欢迎。

数据的:

  DL = {['一':'1 2 3',
       'D':'*',
       'N':'第一'},
      {'一':'4',
       'D':'*','N':
       '第二'},
      {'一':'6',
       'D':'*',
       'N':'第三'},
      {'一':'7 8 9 10',
       'D':'*',
       'N':'第四'}]
 

简单的算法:

 从收藏导入namedtuple

一些= namedtuple('有些',['一','D','N'])

项= []
对于米DL:
    A,D,N = m.values​​()
    A = a.split()
    items.append(一些(A,D,n))的
 

输出:

  [部分(A = ['1','2','3'],D ='*',N =第一),
 一些(一个= ['4','5']峰,d ='*',N ='第二'),
 一些(一个= ['6'〕,D ='*'中,n =第三),
 一些(一个= ['7','8','9','10'],D ='*',N'权利'=)]
 

解决方案

下面,@Petr Viktorin指出了问题,我原来的答复与您最初的解决方案:

  

警告!字典的值()不以任何特定的顺序!如果此解决方案的工作原理,以及A,D,N是按照这个顺序确实回来了,这只是一个巧合。如果你以不同的方式使用不同版本的Python或创建类型的字典,它有可能打破。

(我有点羞愧我没接这件事摆在首位,并获得45代表吧!)

使用@ eryksun的建议,而不是:

 项目= [为米DL一些(M ['一']。分割(),M ['D'],M ['N'])]
 


我的原创,不正确的答案。不要使用它,除非你有 OrderedDict 列表。

 项目= [一些(a.split(),D,N)为A,D,N中(m.values​​()的米DL)
 

I have a list of dict. Need to convert it to list of namedtuple(preferred) or simple tuple while to split first variable by whitespace.

What is more pythonic way to do it?

I simplified my code a little. Comprehensions, gen expressions and itertools usage welcomed.

Data-in:

dl = [{'a': '1 2 3',
       'd': '*',
       'n': 'first'},
      {'a': '4 5',
       'd': '*', 'n':
       'second'},
      {'a': '6',
       'd': '*',
       'n': 'third'},
      {'a': '7 8 9 10',
       'd': '*',
       'n': 'forth'}]

Simple algorithm:

from collections import namedtuple

some = namedtuple('some', ['a', 'd', 'n'])

items = []
for m in dl:
    a, d, n = m.values()
    a = a.split()
    items.append(some(a, d, n))

Output:

[some(a=['1', '2', '3'], d='*', n='first'),
 some(a=['4', '5'], d='*', n='second'),
 some(a=['6'], d='*', n='third'),
 some(a=['7', '8', '9', '10'], d='*', n='forth')]

解决方案

Below, @Petr Viktorin points out the problem with my original answer and your initial solution:

WARNING! The values() of a dictionary are not in any particular order! If this solution works, and a, d, n are really returned in that order, it's just a coincidence. If you use a different version of Python or create the dicts in a different way, it might break.

(I'm kind of mortified I didn't pick this up in the first place, and got 45 rep for it!)

Use @eryksun's suggestion instead:

items =  [some(m['a'].split(), m['d'], m['n']) for m in dl]


My original, incorrect answer. Don't use it unless you have a list of OrderedDict.

items =  [some(a.split(), d, n) for a,d,n in (m.values() for m in dl)]

这篇关于Python化方式来转换http://stardict.sourceforge.net/Dictionaries.php下载列表变为namedtuples名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆