472,989 Members | 3,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 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 1469
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Narendra C. Tulpule | last post by:
Hi, if you know the Python internals, here is a newbie question for you. If I have a list with 100 elements, each element being a long string, is it more efficient to maintain it as a dictionary...
6
by: ruari mactaggart | last post by:
can i write >>>dictionary to mean the 3rd item in the list that is the corresponding value for 'key' ?
11
by: Amy G | last post by:
I have received such good help on this message board. I wonder if I might not get a little more help from you on this. I am at the point where I have two dictionaries, with information of a...
2
by: Stewart Midwinter | last post by:
I would like to link the contents of three OptionMenu lists. When I select an item from the first list (call it continents), the contents of the 2nd list (call it countries) would update. And in...
1
by: Gabriel B. | last post by:
I just sent an email asking for hints on how to import data into a python program As i said earlier i'm really new to python and besides being confortable with the syntax, i'm not sure if i'm on...
10
by: Philippe C. Martin | last post by:
Hi, I'm looking for an easy algorithm - maybe Python can help: I start with X lists which intial sort is based on list #1. I want to reverse sort list #1 and have all other lists sorted...
9
by: Dave H | last post by:
Hello, I have a query regarding definition lists. Is it good practice semantically to use the dt and dd elements to mark up questions and answers in a frequently asked questions list, or FAQ? ...
9
by: SMB | last post by:
I have two lists of data like the following: LIST1 , ] LIST2 , 'label': 'First Name', 'width': 0L, 'separator': ',', 'height': 0L, 'type': 2L, 'order': 1L}, {'code': 14L, 'name': 'Last...
3
by: ELK | last post by:
I recently started a beginners programming course and am struggling with my latest assignment. I have been given a .txt file of the results of a relay race with the following columns of data: team...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.