Re:dict生成器问题 [英] Re: dict generator question

查看:67
本文介绍了Re:dict生成器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Simon Mullis写道:





假设我有一个任意的次要软件列表

虚构软件产品的版本:


l = [" 1.1.1.1"," 1.2.2.2"," 1.2.2.3 "," 1.3.1.2"," 1.3.4.5"]


我想用major_version创建一个dict:count。


(所以,在这种情况下:


dict_of_counts = {" 1.1":" 1",

" 1.2":" 2",

" 1.3":" 2"}



[...]

data = [" 1.1.1.1"," 1.2.2.2"," 1.2.2.3"," 1.3.1.2"," 1.3.4.5"]

来自itertools导入组的


datadict = \

dict((k,len(list(g) )))对于k,g in groupby(data,lambda s:s [:3]))

print datadict


解决方案

9月18日上午11:43,Gerard flanagan< grflana ... @ gmail.comwrote:
< blockquote class =post_quotes>
Simon Mullis写道:




假设我有一个

假想软件产品的次要软件版本的任意列表:


l = [" 1.1.1.1"," 1.2.2.2"," 1.2.2.3"," 1.3.1.2"," 1.3.4.5"]


我想用major_version创建一个dict:count。


(所以,在这种情况下:


dict_of_counts = { 1.1:1,

" 1.2":2,

" 1.3":" 2"}



[...]

data = [" 1.1.1.1"," 1.2.2.2"," 1.2 .2.3"," 1.3.1.2"," 1.3.4.5"]

来自itertools导入组的


datadict = \

dict((k,len(list(g)))k,g in groupby(data,lambda s:s [:3]))

print datadict



请注意,只有当版本已按照主要版本排序时才能正常工作



George


George Sakkis写道:


9月18日上午11:43, Gerard flanagan< grflana ... @ gmail.comwro te:


> Simon Mullis写道:


>>
假设我有一个任意的虚构软件产品的次要软件版本列表:
l = [" 1.1.1.1",1.2.2.2,1.2 .2.3"," 1.3.1.2"," 1.3.4.5"]
我想用major_version创建一个dict:count。
(所以,在这种情况下:
dict_of_counts = {" 1.1" :1,
" 1.2" :2,
1.3 :2 }


[...]
data = [" 1.1.1.1"," 1.2.2.2"," 1.2.2.3"," ; 1.3.1.2"," 1.3.4.5"]

来自itertools import groupby

datadict = \
dict((k,len( list(g)))for k,g in groupby(data,lambda s:s [:3]))
print datadict



注意这个只有当版本已经排序时才能正常工作

按主要版本。



是的,我应该提到它。以下是一个更全面的例子。可能有更好的方法来排序版本号,但这就是我所做的。

data = [" 1.2.2.2"," 1.2。 2.3,1.3.1.2,1.1.1.1,1.3.14.5,

" 1.3.21.6" ]


来自itertools import groupby

import re


RXBUILDSORT = re.compile(r''\ d + | [a-zA-Z]'')


def versionsort(s):

key = []

部分在RXBUILDSORT.findall(s.lower()):

尝试:

key.append(int(part))

除外ValueError:

key.append(ord(part))

返回元组(键)

data.sort(key =版本)

打印数据

datadict = \

dict((k,len(list(g)))for k,g in groupby(data,lambda s:s [:3]))

print datadict



Gerard flanagan:


data.sort()

datadict = \

dict((k, len(list(g)))对于k,g in groupby(data,lambda s:

''。''。join(s.split(''。'',2)[: 2])))



该代码可能正常运行,但它是完全不可读的,而良好的

Python程序员重视高可读性。所以正确的做法是将b / b
分成几行,给出有意义的名字,甚至可以添加评论




len(list(g)))看起来对我的小leniter()函数来说是个好工作

(或者更好的只是对len的语义的扩展)前一段时间

这里的一些人判断为无用,而我经常使用它来
Python和D ;-)


再见,

bearophile


Simon Mullis wrote:

Hi,

Let''s say I have an arbitrary list of minor software versions of an
imaginary software product:

l = [ "1.1.1.1", "1.2.2.2", "1.2.2.3", "1.3.1.2", "1.3.4.5"]

I''d like to create a dict with major_version : count.

(So, in this case:

dict_of_counts = { "1.1" : "1",
"1.2" : "2",
"1.3" : "2" }

[...]
data = [ "1.1.1.1", "1.2.2.2", "1.2.2.3", "1.3.1.2", "1.3.4.5"]

from itertools import groupby

datadict = \
dict((k, len(list(g))) for k,g in groupby(data, lambda s: s[:3]))
print datadict


解决方案

On Sep 18, 11:43 am, Gerard flanagan <grflana...@gmail.comwrote:

Simon Mullis wrote:

Hi,

Let''s say I have an arbitrary list of minor software versions of an
imaginary software product:

l = [ "1.1.1.1", "1.2.2.2", "1.2.2.3", "1.3.1.2", "1.3.4.5"]

I''d like to create a dict with major_version : count.

(So, in this case:

dict_of_counts = { "1.1" : "1",
"1.2" : "2",
"1.3" : "2" }


[...]
data = [ "1.1.1.1", "1.2.2.2", "1.2.2.3", "1.3.1.2", "1.3.4.5"]

from itertools import groupby

datadict = \
dict((k, len(list(g))) for k,g in groupby(data, lambda s: s[:3]))
print datadict

Note that this works correctly only if the versions are already sorted
by major version.

George


George Sakkis wrote:

On Sep 18, 11:43 am, Gerard flanagan <grflana...@gmail.comwrote:

>Simon Mullis wrote:

>>Hi,
Let''s say I have an arbitrary list of minor software versions of an
imaginary software product:
l = [ "1.1.1.1", "1.2.2.2", "1.2.2.3", "1.3.1.2", "1.3.4.5"]
I''d like to create a dict with major_version : count.
(So, in this case:
dict_of_counts = { "1.1" : "1",
"1.2" : "2",
"1.3" : "2" }

[...]
data = [ "1.1.1.1", "1.2.2.2", "1.2.2.3", "1.3.1.2", "1.3.4.5"]

from itertools import groupby

datadict = \
dict((k, len(list(g))) for k,g in groupby(data, lambda s: s[:3]))
print datadict


Note that this works correctly only if the versions are already sorted
by major version.

Yes, I should have mentioned it. Here''s a fuller example below. There''s
maybe better ways of sorting version numbers, but this is what I do.
data = [ "1.2.2.2", "1.2.2.3", "1.3.1.2", "1.1.1.1", "1.3.14.5",
"1.3.21.6" ]

from itertools import groupby
import re

RXBUILDSORT = re.compile(r''\d+|[a-zA-Z]'')

def versionsort(s):
key = []
for part in RXBUILDSORT.findall(s.lower()):
try:
key.append(int(part))
except ValueError:
key.append(ord(part))
return tuple(key)

data.sort(key=versionsort)
print data

datadict = \
dict((k, len(list(g))) for k,g in groupby(data, lambda s: s[:3]))
print datadict



Gerard flanagan:

data.sort()
datadict = \
dict((k, len(list(g))) for k,g in groupby(data, lambda s:
''.''.join(s.split(''.'',2)[:2])))

That code may run correctly, but it''s quite unreadable, while good
Python programmers value high readability. So the right thing to do is
to split that line into parts, giving meaningful names, and maybe even
add comments.

len(list(g))) looks like a good job for my little leniter() function
(or better just an extension to the semantics of len) that time ago
some people here have judged as useless, while I use it often in both
Python and D ;-)

Bye,
bearophile


这篇关于Re:dict生成器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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