+ 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 Respuestas
+ 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.