473,385 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1484
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 I´m 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 I´m 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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.