473,770 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dict generator question

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" }

Something like:

dict_of_counts = dict([(v[0:3], "count") for v in l])

I can't seem to figure out how to get "count", as I cannot do x += 1
or x++ as x may or may not yet exist, and I haven't found a way to
create default values.

I'm most probably not thinking pythonically enough... (I know I could
do this pretty easily with a couple more lines, but I'd like to
understand if there's a way to use a dict generator for this).

Thanks in advance

SM
--
Simon Mullis
Sep 18 '08 #1
6 1444
Simon Mullis napisa³(a):
Something like:

dict_of_counts = dict([(v[0:3], "count") for v in l])

I can't seem to figure out how to get "count", as I cannot do x += 1
or x++ as x may or may not yet exist, and I haven't found a way to
create default values.
It seems to me that the "count" you're looking for is the number of
elements from l whose first 3 characters are the same as the v[0:3]
thing. So you may try:
>>dict_of_count s = dict((v[0:3], sum(1 for x in l if x[:3] == v[:3])) for v in l)
But this isn't particularly efficient. The 'canonical way' to
construct such histograms/frequency counts in python is probably by
using defaultdict:
>>dict_of_count s = collections.def aultdict(int)
for x in l:
dict_of_counts[x[:3]] += 1
Regards,
Marek
Sep 18 '08 #2
On Sep 18, 10:54 am, "Simon Mullis" <si...@mullis.c o.ukwrote:
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" }

Something like:

dict_of_counts = dict([(v[0:3], "count") for v in l])

I can't seem to figure out how to get "count", as I cannot do x += 1
or x++ as x may or may not yet exist, and I haven't found a way to
create default values.

I'm most probably not thinking pythonically enough... (I know I could
do this pretty easily with a couple more lines, but I'd like to
understand if there's a way to use a dict generator for this).

Thanks in advance

SM

--
Simon Mullis
3 lines:

from collections import defaultdict
dd=defaultdict( int)
for x in l: dd[x[0:3]]+=1
Sep 18 '08 #3
On Sep 18, 10:54 am, "Simon Mullis" <si...@mullis.c o.ukwrote:
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" }

Something like:

dict_of_counts = dict([(v[0:3], "count") for v in l])

I can't seem to figure out how to get "count", as I cannot do x += 1
or x++ as x may or may not yet exist, and I haven't found a way to
create default values.

I'm most probably not thinking pythonically enough... (I know I could
do this pretty easily with a couple more lines, but I'd like to
understand if there's a way to use a dict generator for this).
Not everything has to be a one-liner; also v[0:3] is wrong if any sub-
version is greater than 9. Here's a standard idiom (in 2.5+ at least):

from collection import defaultdict
versions = [ "1.1.1.1", "1.2.2.2", "1.2.2.3", "1.3.1.2", "1.3.4.5"]

major2count = defaultdict(int )
for v in versions:
major2count['.'.join(v.spli t('.',2)[:2])] += 1
print major2count

HTH,
George
Sep 18 '08 #4
On Sep 18, 10:54 am, "Simon Mullis" <si...@mullis.c o.ukwrote:
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" }

Something like:

dict_of_counts = dict([(v[0:3], "count") for v in l])

I can't seem to figure out how to get "count", as I cannot do x += 1
or x++ as x may or may not yet exist, and I haven't found a way to
create default values.

I'm most probably not thinking pythonically enough... (I know I could
do this pretty easily with a couple more lines, but I'd like to
understand if there's a way to use a dict generator for this).

Thanks in advance

SM

--
Simon Mullis
Considering 3 identical "simultpost " solutions I'd say:
"one obvious way to do it" FTW :-)
Sep 18 '08 #5
Haha!

Thanks for all of the suggestions... (I love this list!)

SM

2008/9/18 <pr*******@lati nmail.com>:
On Sep 18, 10:54 am, "Simon Mullis" <si...@mullis.c o.ukwrote:
>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_coun ts = { "1.1" : "1",
"1.2" : "2",
"1.3" : "2" }

Something like:

dict_of_coun ts = dict([(v[0:3], "count") for v in l])

I can't seem to figure out how to get "count", as I cannot do x += 1
or x++ as x may or may not yet exist, and I haven't found a way to
create default values.

I'm most probably not thinking pythonically enough... (I know I could
do this pretty easily with a couple more lines, but I'd like to
understand if there's a way to use a dict generator for this).

Thanks in advance

SM

--
Simon Mullis

Considering 3 identical "simultpost " solutions I'd say:
"one obvious way to do it" FTW :-)
--
http://mail.python.org/mailman/listinfo/python-list


--
Simon Mullis
_______________ __
si***@mullis.co .uk
Sep 18 '08 #6
"Simon Mullis" <si***@mullis.c o.ukwrites:
l = [ "1.1.1.1", "1.2.2.2", "1.2.2.3", "1.3.1.2", "1.3.4.5"]
...
dict_of_counts = dict([(v[0:3], "count") for v in l])
Untested:

def major_version(v ersion_string):
"convert '1.2.3.2' to '1.2'"
return '.'.join(versio n_string.split( '.')[:2])

dict_of_counts = defaultdict(int )

for x in l:
dict_of_counts[major_version(l )] += 1
Sep 19 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1681
by: George Young | last post by:
I am puzzled that creating large dicts with an explicit iterable of key,value pairs seems to be slow. I thought to save time by doing: palettes = dict((w,set(w)) for w in words) instead of: palettes={} for w in words: palettes=set(w)
10
1678
by: Gerard flanagan | last post by:
Simon Mullis wrote: data = from itertools import groupby datadict = \ dict((k, len(list(g))) for k,g in groupby(data, lambda s: s)) print datadict
6
2731
by: Ernst-Ludwig Brust | last post by:
Given 2 Number-Lists say l0 and l1, count the various positiv differences between the 2 lists the following part works: dif= da={} for d in dif: da=da.get(d,0)+1 i wonder, if there is a way, to avoid the list dif
10
1722
by: bearophileHUGS | last post by:
I'd like to know why Python 2.6 doesn't have the syntax to create sets/ dicts of Python 3.0, like: {x*x for x in xrange(10)} {x:x*x for x in xrange(10)} Bye, bearophile
0
10254
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7451
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3607
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.