+ 6
Python Dictionary - Two keys and one value
Is it possible to have two keys, and one value? a = { "a"|"b": "c", } Or like: a = { "a", "b": "c", }
29 Respostas
+ 7
https://code.sololearn.com/cXheE2AMrxEf/#py
dictionary comprehension
+ 9
Two different keys may have the same value, so you can have:
a = {“a” : ”c” , “b” : ”c”}
if that helps?
+ 7
PyDan This will use the tuple ('a', 'b') as a dictionary key, not the letters a and b individually.
Dolan Hêriş If you look at a syntax like a = {'a', 'b': 'c', 'd': 'e', 'f', 'g', 'h': 'i'}, it would be really difficult to tell where a value ends and the next key begins. Also, whatever you use before the colon (:) would probably be treated as one key and not as a set of keys, so you'd end up defining a tuple, list or whatever as a key instead of a set of individual keys.
I don't think using multiple keys with the same value is possible other than the way Russ explained.
+ 5
CodeIt uses a dict comprehension. Dict/list/set comprehensions are a very effective way to create dicts/lists/sets with an "obvious" pattern. For example, if you want to create a list with all numbers from 0 to 10,000 that are divisible by 3, that's a perfect use case for a list comprehension.
Your dictionary contains highly specific data. There are eight keys and only two of them share a set of common values. The other key/value pairs consist of unique combinations. That's not a very obvious pattern and not something you'd usually use a dict comprehension for. Thinking about how to set up the comprehension probably needs more time and effort than just defining the dict manually.
+ 3
Dolan Hêriş After doing a bit of digging, I’ve found out it is possible to do it in one line, but it does create the exact same dictionary as previously suggested:
dct = dict.fromkeys(['a','b'], 'c')
print(dct) #{‘b’: ‘c’, ‘a’: ‘c’}
To add further key-value pairs, you can use:
dct.update(dct.fromkeys(['d','e'], 'f'))
print(dct) #{‘e’: ‘f’, ‘d’: ‘f’, ‘b’: ‘c’, ‘a’: ‘c’}
Hope this is what you’re after!
+ 3
ok... not as a oneliner but easy to add further values
https://code.sololearn.com/cXheE2AMrxEf/#py
+ 2
You can write it in one line like I did in my comment!
Are you trying to get a[“a”] = “c” and a[“b”] = “c” in just one key-value pair? And, if so, why may I ask?
+ 2
d = {
("a", "b"): "c"
}
+ 2
Thank you Russ . I will check them out to see if it works or not 🤨
Thanks again 🙏
+ 2
CodeIt This is exactly what I wanted. Thank you.
+ 2
you can do it as complicated as u want.
I just changed the prog.
+ 2
exactly Anna . I tried to add some pairs but it didn’t work! 😔
+ 2
Dolan Hêriş please tell us the whole idea of your prog.
python has an answer for nearky everything.
just look here for a further application:
https://code.sololearn.com/cJqUMn3ZjkLt/?ref=app
+ 2
CodeIt a dictionary for replacing characters between two languages. Some of the characters have two equals and it is OKAY to have them individually in my dictionary but it pops up a question in my mind that what if we could have two keys with one value!? so here we are ;-)
+ 2
I think not, because a dict is language specific. Rules should be stored in an independent format.
maybe a csv or other file.
we dont have the op. on SL... so this is more a kind of proof of concept.
+ 2
Dolan Hêriş i fully trust in u😊😊
+ 1
Russ Thanks for replying. actually I wanted sth else. Guess I wanna replace “a” and “b” with “c”. but I’m not gonna write it like this:
a = {
"a": "c",
"b": "c"
}
I want it in one line! 🤨
+ 1
sure. We have two latin-based characters which equals to one in a RTL. so instead of writing both individually, I wanna use sth like pipe (|) between two keys. but it will cause an error.