473,789 Members | 2,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

lists and dictionaries

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

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

Could you help me, please?

Lada

Jul 11 '07 #1
5 1499
li = [ {'index': 0, 'transport': 'udp', 'service_domain ':
'dp0.example.co m'},
{'index': 1, 'transport': 'udp', 'service_domain ':
'dp1.example.co m'},
{'index': 0, 'transport': 'tcp', 'service_domain ':
'dp0.example.co m'},
{'index': 1, 'transport': 'tcp', 'service_domain ':
'dp1.example.co m'}]
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 indexBasedToSDB ased(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.o rgwrote:
Hi,
I have a list of dictionaries.
e.g.
[{'index': 0, 'transport': 'udp', 'service_domain ': 'dp0.example.co m'},
{'index': 1, 'transport': 'udp', 'service_domain ': 'dp1.example.co m'},
{'index': 0, 'transport': 'tcp', 'service_domain ': 'dp0.example.co m'},
{'index': 1, 'transport': 'tcp', 'service_domain ': 'dp1.example.co m'}]

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

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...@gmai l.comwrote:
li = [ {'index': 0, 'transport': 'udp', 'service_domain ':
'dp0.example.co m'},
{'index': 1, 'transport': 'udp', 'service_domain ':
'dp1.example.co m'},
{'index': 0, 'transport': 'tcp', 'service_domain ':
'dp0.example.co m'},
{'index': 1, 'transport': 'tcp', 'service_domain ':
'dp1.example.co m'}]

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...@gmai l.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.co m'},
{'index': 1, 'ip_addr': '5.6.7.8', 'server-name':'Asterisk ', 'transport': 'udp', 'service_domain ': 'dp1.example.co m'},
{'index': 0, 'ip_addr': '1.2.3.4', 'server-name': 'Yes', 'transport': 'tcp', 'service_domain ': 'dp0.example.co m'},
{'index': 1, 'ip_addr': '5.6.7.8', 'server-name': 'Yes', 'transport': 'tcp', 'service_domain ': 'dp1.example.co m'}]

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.co m', 'ip_addr': '1.2.3.4', 'server-name':'Asterisk '},
{'transports': ['udp','tcp'], 'service_domain ': 'dp1.example.co m', '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.o rgwrote:
>Hi,
I have a list of dictionaries.
e.g.
[{'index': 0, 'transport': 'udp', 'service_domain ': 'dp0.example.co m'},
{'index': 1, 'transport': 'udp', 'service_domain ': 'dp1.example.co m'},
{'index': 0, 'transport': 'tcp', 'service_domain ': 'dp0.example.co m'},
{'index': 1, 'transport': 'tcp', 'service_domain ': 'dp1.example.co m'}]

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

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
2658
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 (with a key = a string from the list and value = None) for the purpose of insertion and removal? Basically, if Python really implements lists as linked lists but dictionaries as hash tables, it may well be that hashing a key takes negligible time...
6
8448
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
2516
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 domain and a frequency of that domain. Now that I have the two, I want to delete each entry from one that the two have in common, leaving only those that are unique to the dictionary? Say I have a dictionary called domains_black
2
4638
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 turn the contents of the 3rd list (call it states would be updated by a change in the 2nd list. If anyone can share a recipe or some ideas, I'd be grateful! Here's some sample code that displays three OptionMenus, but doesn't update the list...
1
1215
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 the right track with the logic I'm asking for hints again here at the list because i think i'm already into premature optimization...
10
2183
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 accordingly. Any idea is welcome.
9
3752
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? Here is an example of just such a usage: <dl class="faq"> <di>
9
4576
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 Name', 'value': , 'label': 'Last Name', 'width': 0L, 'separator': ',', 'height': 0L, 'type': 2L, 'order': 2L},
3
1271
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 leg runner time It is my job to write a code which reads this file and outputs a list of the teams with their total times. I have had no issues importing the file, but I am completely lost on how to link team names (which are not specified) with...
0
9511
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10404
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
10195
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
10136
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
9016
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7525
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
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4090
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
3
2906
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.