Hi all,
Continuing the search for interesting challenges with lists, tuples and
dictionaries I am looking for a way to do the following.
one = {
'a' : 1,
'b' : 2,
'c' : 3
}
two = {
'a' : [4,5,6],
'b' : [8,9,10],
'c' : [11,12,13]
}
The goal is
[('a', 1, [4,5,6]), ('b', 2, [8,9,10]), ('c', 3, [11,12,13])]
My attempts so far:
[(t, c, l) for t, c in one.items() if (t, l) in two.items()]
gives me a 'l is not defined'
---------------
[(t, c, l) for t, c in one.items() for (t, l) in two.items()]
gives me a lot more than I need :-)
---------------
Many thanks in advance!
Cheers,
-- Nickolay 3 1866
How about
[(k, v, two[v]) for k, v in one.items()]
or
[(k, v, two[v]) for k, v in one.items() if k in two]
or
[(k, v, two.get(v, None)) for k, v in one.items()]
depending on what you want to do when a key doesn't exist in "two"
Jeff
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
iD8DBQFBkoRZJd0 1MZaTXX0RAvpNAJ 4vYvie7kADHprwd Ojuoa605/8eRwCeIBwX
vyDcQKHMKSgRsgB 1Y/TtzAo=
=u8x/
-----END PGP SIGNATURE-----
Jeff Epler wrote: How about [(k, v, two[v]) for k, v in one.items()] or [(k, v, two[v]) for k, v in one.items() if k in two] or [(k, v, two.get(v, None)) for k, v in one.items()] depending on what you want to do when a key doesn't exist in "two"
Jeff
I should have thought of that, it is sooo simple... :-)
Thanks again.
Cheers,
-- Nickolay
Nickolay Kolev wrote: My attempts so far: ... [(t, c, l) for t, c in one.items() for (t, l) in two.items()]
Simply forget about items for two.
[(key, val1, two[key]) for key, val1 in one.items() if key in two]
--Scott David Daniels Sc***********@A cm.Org This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
by: Kerry Neilson |
last post by:
Hi,
Really hung up on this one. I'm trying to get all the fields of a
dictionary to be unique for each class:
class A {
my_dict =
dict_entry = { 'key1':0, 'key2':0 }
__init__(self):
for x in range(10):
|
by: Frohnhofer, James |
last post by:
My initial problem was to initialize a bunch of dictionaries at the start of a
function.
I did not want to do
def fn():
a = {}
b = {}
c = {}
. . .
z = {}
|
by: Bulba! |
last post by:
Hello everyone,
I'm reading the rows from a CSV file. csv.DictReader puts
those rows into dictionaries.
The actual files contain old and new translations of software
strings. The dictionary containing the row data looks like this:
o={'TermID':'4', 'English':'System Administration',
'Polish':'Zarzadzanie systemem'}
|
by: Christoph Zwerschke |
last post by:
This is probably a FAQ, but I dare to ask it nevertheless since I
haven't found a satisfying answer yet: Why isn't there an "ordered
dictionary" class at least in the standard list? Time and again I am
missing that feature. Maybe there is something wrong with my programming
style, but I rather think it is generally useful.
I fully agree with the following posting where somebody complains why so
very basic and useful things are not part...
| |
by: David Pratt |
last post by:
Hi. I like working with lists of dictionaries since order is preserved
in a list when I want order and the dictionaries make it explicit what
I have got inside them. I find this combination very useful for storing
constants especially. Generally I find myself either needing to
retrieve the values of constants in an iterative way (as in my
contrived example below). Perhaps even more frequent is given one value
is to look up the matching...
|
by: Ladislav Andel |
last post by:
Hi,
I have a list of dictionaries.
e.g.
how could I make a new list of dictionaries which would look like:
, 'service_domain': 'dp0.example.com'},
{'transports': , 'service_domain': 'dp1.example.com'}]
Could you help me, please?
|
by: Abandoned |
last post by:
Hi..
dict1={1: 4, 3: 5}... and 2 millions element
dict2={3: 3, 8: 6}... and 3 millions element
I want to combine dict1 and dict2 and i don't want to use FOR because
i need to performance.
I'm sorry my bed english.
King regards..
|
by: Jon Bowlas |
last post by:
Many thanks for all your reponses, much appreciated.
I'll get back to you on which is the best for me.
BTW - yes John thats exactly what I wanted.
Cheers
Jon
|
by: Edwin.Madari |
last post by:
by the way, iterating over bar will throw KeyError if that key does not exist in foo. to see that in action, simply set another key in bar after copy.deepcopy stmt in this example..
bar = 0
and re-run....
fun learning with python...
Edwin
-----Original Message-----
From: Madari, Edwin
Sent: Thursday, August 14, 2008 9:24 PM
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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,...
|
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...
|
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,...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
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
|
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...
| |