471,337 Members | 923 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

lists and dictionaries

Hi,
I have a list of dictionaries.
e.g.
[{'index': 0, 'transport': 'udp', 'service_domain': 'dp0.example.com'},
{'index': 1, 'transport': 'udp', 'service_domain': 'dp1.example.com'},
{'index': 0, 'transport': 'tcp', 'service_domain': 'dp0.example.com'},
{'index': 1, 'transport': 'tcp', 'service_domain': 'dp1.example.com'}]

how could I make a new list of dictionaries which would look like:
[{'transports': ['udp','tcp'], 'service_domain': 'dp0.example.com'},
{'transports': ['udp','tcp'], 'service_domain': 'dp1.example.com'}]

Could you help me, please?

Lada

Jul 11 '07 #1
5 1390
li = [ {'index': 0, 'transport': 'udp', 'service_domain':
'dp0.example.com'},
{'index': 1, 'transport': 'udp', 'service_domain':
'dp1.example.com'},
{'index': 0, 'transport': 'tcp', 'service_domain':
'dp0.example.com'},
{'index': 1, 'transport': 'tcp', 'service_domain':
'dp1.example.com'}]
I like this solution:

[{ 'transports' : [d['transport'] for d in li if
d['service_domain'] == dom],
'service_domain': dom,
} for dom in set(d2['service_domain'] for d2 in li)]

merely because it takes one line. Humorously enough, it appears to be
twice as efficient, at least when profiled on my computer, if speed is
important in this problem. Not that this is the best way to do it
either.

Anyway, since the generator expression isn't very clear:

def indexBasedToSDBased(li):
newli = []
# For each service domain in the set read from the original list
for sdom in set(d['service_domain'] for d in li):
# Append a new dictionary mapping all transports for this
domain
newli.append({
'service_domain': sdom,
'transports': [d['transport'] for d in li if
d['service_domain'] == sdom]
})
return newli

(which is slower than the one-line generator :-D I love generator
expressions)

- Jeremy

Jul 12 '07 #2
On 11 jul, 21:08, Ladislav Andel <lad...@iptel.orgwrote:
Hi,
I have a list of dictionaries.
e.g.
[{'index': 0, 'transport': 'udp', 'service_domain': 'dp0.example.com'},
{'index': 1, 'transport': 'udp', 'service_domain': 'dp1.example.com'},
{'index': 0, 'transport': 'tcp', 'service_domain': 'dp0.example.com'},
{'index': 1, 'transport': 'tcp', 'service_domain': 'dp1.example.com'}]

how could I make a new list of dictionaries which would look like:
[{'transports': ['udp','tcp'], 'service_domain': 'dp0.example.com'},
{'transports': ['udp','tcp'], 'service_domain': 'dp1.example.com'}]

Could you help me, please?
doms = {}
for entry in oldList:
doms.setdefault(entry['service_domain'],
[]).append(entry['transport'])
newList = [{'transports': t, 'service_domain': d} for d,t in
doms.iteritems()]
Jul 12 '07 #3
On 12 jul, 04:49, anethema <jefish...@gmail.comwrote:
li = [ {'index': 0, 'transport': 'udp', 'service_domain':
'dp0.example.com'},
{'index': 1, 'transport': 'udp', 'service_domain':
'dp1.example.com'},
{'index': 0, 'transport': 'tcp', 'service_domain':
'dp0.example.com'},
{'index': 1, 'transport': 'tcp', 'service_domain':
'dp1.example.com'}]

I like this solution:

[{ 'transports' : [d['transport'] for d in li if
d['service_domain'] == dom],
'service_domain': dom,
} for dom in set(d2['service_domain'] for d2 in li)]

merely because it takes one line. Humorously enough, it appears to be
twice as efficient,
Correct me if Im wrong, that is a O(n**2) solution, to O(n) problem.

Jul 12 '07 #4
Thank you to all of you guys.
It's exactly I was looking for.

Lada
Bart Ogryczak wrote:
On 12 jul, 04:49, anethema <jefish...@gmail.comwrote:
>>li = [ {'index': 0, 'transport': 'udp', 'service_domain':
'dp0.example.com'},
{'index': 1, 'transport': 'udp', 'service_domain':
'dp1.example.com'},
{'index': 0, 'transport': 'tcp', 'service_domain':
'dp0.example.com'},
{'index': 1, 'transport': 'tcp', 'service_domain':
'dp1.example.com'}]
I like this solution:

[{ 'transports' : [d['transport'] for d in li if
d['service_domain'] == dom],
'service_domain': dom,
} for dom in set(d2['service_domain'] for d2 in li)]

merely because it takes one line. Humorously enough, it appears to be
twice as efficient,

Correct me if Im wrong, that is a O(n**2) solution, to O(n) problem.

Jul 12 '07 #5
Hi,
could you actually help me further?
I thought it will quite easy but I've been programming in python just
for a month.
I need to add extra items in new list.
So here is example:

I have a list of dictionaries.
e.g.
[{'index': 0, 'ip_addr': '1.2.3.4', 'server-name':'Asterisk', 'transport': 'udp', 'service_domain': 'dp0.example.com'},
{'index': 1, 'ip_addr': '5.6.7.8', 'server-name':'Asterisk', 'transport': 'udp', 'service_domain': 'dp1.example.com'},
{'index': 0, 'ip_addr': '1.2.3.4', 'server-name': 'Yes', 'transport': 'tcp', 'service_domain': 'dp0.example.com'},
{'index': 1, 'ip_addr': '5.6.7.8', 'server-name': 'Yes', 'transport': 'tcp', 'service_domain': 'dp1.example.com'}]

The server-name within tcp test is not known because I just open connection at port where the server should run and if succeeded then server-name = Yes. In newlist should be the server name Asterisk though.

how could I make a new list of dictionaries which would look like:
[{'transports': ['udp','tcp'], 'service_domain': 'dp0.example.com', 'ip_addr': '1.2.3.4', 'server-name':'Asterisk'},
{'transports': ['udp','tcp'], 'service_domain': 'dp1.example.com', 'ip_addr': '5.6.7.8', 'server-name':'Asterisk'}]

I was trying to implement it to existing code but I'm not really
successful so far.

Thank you for your help.

Lada

Bart Ogryczak wrote:
On 11 jul, 21:08, Ladislav Andel <lad...@iptel.orgwrote:
>Hi,
I have a list of dictionaries.
e.g.
[{'index': 0, 'transport': 'udp', 'service_domain': 'dp0.example.com'},
{'index': 1, 'transport': 'udp', 'service_domain': 'dp1.example.com'},
{'index': 0, 'transport': 'tcp', 'service_domain': 'dp0.example.com'},
{'index': 1, 'transport': 'tcp', 'service_domain': 'dp1.example.com'}]

how could I make a new list of dictionaries which would look like:
[{'transports': ['udp','tcp'], 'service_domain': 'dp0.example.com'},
{'transports': ['udp','tcp'], 'service_domain': 'dp1.example.com'}]

Could you help me, please?

doms = {}
for entry in oldList:
doms.setdefault(entry['service_domain'],
[]).append(entry['transport'])
newList = [{'transports': t, 'service_domain': d} for d,t in
doms.iteritems()]
Jul 17 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Narendra C. Tulpule | last post: by
6 posts views Thread by ruari mactaggart | last post: by
2 posts views Thread by Stewart Midwinter | last post: by
1 post views Thread by Gabriel B. | last post: by
10 posts views Thread by Philippe C. Martin | last post: by
9 posts views Thread by Dave H | last post: by
9 posts views Thread by SMB | last post: by
3 posts views Thread by ELK | last post: by
reply views Thread by rosydwin | last post: by

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.