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 1847
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)
iD8DBQFBkoRZJd01MZaTXX0RAvpNAJ4vYvie7kADHprwdOjuoa 605/8eRwCeIBwX
vyDcQKHMKSgRsgB1Y/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***********@Acm.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...
|
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...
|
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...
|
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...
|
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...
|
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':...
|
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.
...
|
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...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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: 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...
|
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...
| |