Python 3 - How to switch the case of a dictionary then adding numeric values
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
So I have this string
"Hello"
I want to convert that to an uppercase dictionary with empty values
something like:
{ "H":0, "E":0, "L":0, "L":0, "O":0 }
and then I want to apply a range of values into the value slots using a for loop:
{ "H":1, "E":2, "L":3, "L":4, "O":5 }
and finally, use these values to print back out the letter with their assigned values, something like this:
"HEELLLLLLLOOOOO"
python string dictionary
add a comment |
So I have this string
"Hello"
I want to convert that to an uppercase dictionary with empty values
something like:
{ "H":0, "E":0, "L":0, "L":0, "O":0 }
and then I want to apply a range of values into the value slots using a for loop:
{ "H":1, "E":2, "L":3, "L":4, "O":5 }
and finally, use these values to print back out the letter with their assigned values, something like this:
"HEELLLLLLLOOOOO"
python string dictionary
3
What have you tried? What specifically do you need help with? Also note that depending on what version of Python you're using, dictionaries may or may not guarantee a certain order.
– Carcigenicate
Nov 16 '18 at 16:00
2
You know that you cannot have two times the same key inside of a dictionary? So getting{ "H":0, "E":0, "L":0, "L":0, "O":0 }
is impossible.
– quant
Nov 16 '18 at 16:02
To complete @Carcigenicate the order guarantee was declared to be part of language spec with Python 3.7. Of course if you rely on that, your code is not retro-compatible with older Python versions. Also it looks that the change was motivated by related conveniences described in PEP 468 more than because of insertion order (like kwargs order and attributes order, because dicts are used for that).
– progmatico
Nov 16 '18 at 16:36
Ok, I don't mean to go off the beaten path, but ... I don't understand ... I have asked just a few questions and they always get negative ratings? I tried to search this out, I tagged it, I am new to python and tried to make the question and title clear ... Can anyone help me understand why I get this negative feedback ? I thought we are supposed to ask questions to the community ? I am trying to learn and understand programming ... very confused
– Finstercode
Nov 17 '18 at 4:02
add a comment |
So I have this string
"Hello"
I want to convert that to an uppercase dictionary with empty values
something like:
{ "H":0, "E":0, "L":0, "L":0, "O":0 }
and then I want to apply a range of values into the value slots using a for loop:
{ "H":1, "E":2, "L":3, "L":4, "O":5 }
and finally, use these values to print back out the letter with their assigned values, something like this:
"HEELLLLLLLOOOOO"
python string dictionary
So I have this string
"Hello"
I want to convert that to an uppercase dictionary with empty values
something like:
{ "H":0, "E":0, "L":0, "L":0, "O":0 }
and then I want to apply a range of values into the value slots using a for loop:
{ "H":1, "E":2, "L":3, "L":4, "O":5 }
and finally, use these values to print back out the letter with their assigned values, something like this:
"HEELLLLLLLOOOOO"
python string dictionary
python string dictionary
edited Nov 16 '18 at 16:01
pault
17k42652
17k42652
asked Nov 16 '18 at 15:59
FinstercodeFinstercode
14
14
3
What have you tried? What specifically do you need help with? Also note that depending on what version of Python you're using, dictionaries may or may not guarantee a certain order.
– Carcigenicate
Nov 16 '18 at 16:00
2
You know that you cannot have two times the same key inside of a dictionary? So getting{ "H":0, "E":0, "L":0, "L":0, "O":0 }
is impossible.
– quant
Nov 16 '18 at 16:02
To complete @Carcigenicate the order guarantee was declared to be part of language spec with Python 3.7. Of course if you rely on that, your code is not retro-compatible with older Python versions. Also it looks that the change was motivated by related conveniences described in PEP 468 more than because of insertion order (like kwargs order and attributes order, because dicts are used for that).
– progmatico
Nov 16 '18 at 16:36
Ok, I don't mean to go off the beaten path, but ... I don't understand ... I have asked just a few questions and they always get negative ratings? I tried to search this out, I tagged it, I am new to python and tried to make the question and title clear ... Can anyone help me understand why I get this negative feedback ? I thought we are supposed to ask questions to the community ? I am trying to learn and understand programming ... very confused
– Finstercode
Nov 17 '18 at 4:02
add a comment |
3
What have you tried? What specifically do you need help with? Also note that depending on what version of Python you're using, dictionaries may or may not guarantee a certain order.
– Carcigenicate
Nov 16 '18 at 16:00
2
You know that you cannot have two times the same key inside of a dictionary? So getting{ "H":0, "E":0, "L":0, "L":0, "O":0 }
is impossible.
– quant
Nov 16 '18 at 16:02
To complete @Carcigenicate the order guarantee was declared to be part of language spec with Python 3.7. Of course if you rely on that, your code is not retro-compatible with older Python versions. Also it looks that the change was motivated by related conveniences described in PEP 468 more than because of insertion order (like kwargs order and attributes order, because dicts are used for that).
– progmatico
Nov 16 '18 at 16:36
Ok, I don't mean to go off the beaten path, but ... I don't understand ... I have asked just a few questions and they always get negative ratings? I tried to search this out, I tagged it, I am new to python and tried to make the question and title clear ... Can anyone help me understand why I get this negative feedback ? I thought we are supposed to ask questions to the community ? I am trying to learn and understand programming ... very confused
– Finstercode
Nov 17 '18 at 4:02
3
3
What have you tried? What specifically do you need help with? Also note that depending on what version of Python you're using, dictionaries may or may not guarantee a certain order.
– Carcigenicate
Nov 16 '18 at 16:00
What have you tried? What specifically do you need help with? Also note that depending on what version of Python you're using, dictionaries may or may not guarantee a certain order.
– Carcigenicate
Nov 16 '18 at 16:00
2
2
You know that you cannot have two times the same key inside of a dictionary? So getting
{ "H":0, "E":0, "L":0, "L":0, "O":0 }
is impossible.– quant
Nov 16 '18 at 16:02
You know that you cannot have two times the same key inside of a dictionary? So getting
{ "H":0, "E":0, "L":0, "L":0, "O":0 }
is impossible.– quant
Nov 16 '18 at 16:02
To complete @Carcigenicate the order guarantee was declared to be part of language spec with Python 3.7. Of course if you rely on that, your code is not retro-compatible with older Python versions. Also it looks that the change was motivated by related conveniences described in PEP 468 more than because of insertion order (like kwargs order and attributes order, because dicts are used for that).
– progmatico
Nov 16 '18 at 16:36
To complete @Carcigenicate the order guarantee was declared to be part of language spec with Python 3.7. Of course if you rely on that, your code is not retro-compatible with older Python versions. Also it looks that the change was motivated by related conveniences described in PEP 468 more than because of insertion order (like kwargs order and attributes order, because dicts are used for that).
– progmatico
Nov 16 '18 at 16:36
Ok, I don't mean to go off the beaten path, but ... I don't understand ... I have asked just a few questions and they always get negative ratings? I tried to search this out, I tagged it, I am new to python and tried to make the question and title clear ... Can anyone help me understand why I get this negative feedback ? I thought we are supposed to ask questions to the community ? I am trying to learn and understand programming ... very confused
– Finstercode
Nov 17 '18 at 4:02
Ok, I don't mean to go off the beaten path, but ... I don't understand ... I have asked just a few questions and they always get negative ratings? I tried to search this out, I tagged it, I am new to python and tried to make the question and title clear ... Can anyone help me understand why I get this negative feedback ? I thought we are supposed to ask questions to the community ? I am trying to learn and understand programming ... very confused
– Finstercode
Nov 17 '18 at 4:02
add a comment |
1 Answer
1
active
oldest
votes
You get the first dictionary with
>>> s = "Hello"
>>> d = dict.fromkeys(s.upper(), 0)
>>> d
>>> {'E': 0, 'H': 0, 'L': 0, 'O': 0}
Note that the key 'L'
appears only once because dictionary keys are unique.
Your second desired dictionary is therefore impossible to create.
But if you only care about the final string, creating any dict is unnecessary, because:
>>> ''.join(c*i for i, c in enumerate(s.upper(), 1))
>>> 'HEELLLLLLLOOOOO'
If this looks complicated, just use a simple for
loop.
>>> to_join =
>>> for i, c in enumerate(s.upper(), 1):
...: to_join.append(c*i)
...:
>>> result = ''.join(to_join)
>>> result
>>> 'HEELLLLLLLOOOOO'
yes, +1 but their first dict also contains double L and no guarantees on the order either (prior to 3.7)
– Chris_Rands
Nov 16 '18 at 16:11
1
While this is a good answer I think there's an arguable case against answering a question with virutally no effort from OP's end...
– Idlehands
Nov 16 '18 at 16:39
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53341403%2fpython-3-how-to-switch-the-case-of-a-dictionary-then-adding-numeric-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You get the first dictionary with
>>> s = "Hello"
>>> d = dict.fromkeys(s.upper(), 0)
>>> d
>>> {'E': 0, 'H': 0, 'L': 0, 'O': 0}
Note that the key 'L'
appears only once because dictionary keys are unique.
Your second desired dictionary is therefore impossible to create.
But if you only care about the final string, creating any dict is unnecessary, because:
>>> ''.join(c*i for i, c in enumerate(s.upper(), 1))
>>> 'HEELLLLLLLOOOOO'
If this looks complicated, just use a simple for
loop.
>>> to_join =
>>> for i, c in enumerate(s.upper(), 1):
...: to_join.append(c*i)
...:
>>> result = ''.join(to_join)
>>> result
>>> 'HEELLLLLLLOOOOO'
yes, +1 but their first dict also contains double L and no guarantees on the order either (prior to 3.7)
– Chris_Rands
Nov 16 '18 at 16:11
1
While this is a good answer I think there's an arguable case against answering a question with virutally no effort from OP's end...
– Idlehands
Nov 16 '18 at 16:39
add a comment |
You get the first dictionary with
>>> s = "Hello"
>>> d = dict.fromkeys(s.upper(), 0)
>>> d
>>> {'E': 0, 'H': 0, 'L': 0, 'O': 0}
Note that the key 'L'
appears only once because dictionary keys are unique.
Your second desired dictionary is therefore impossible to create.
But if you only care about the final string, creating any dict is unnecessary, because:
>>> ''.join(c*i for i, c in enumerate(s.upper(), 1))
>>> 'HEELLLLLLLOOOOO'
If this looks complicated, just use a simple for
loop.
>>> to_join =
>>> for i, c in enumerate(s.upper(), 1):
...: to_join.append(c*i)
...:
>>> result = ''.join(to_join)
>>> result
>>> 'HEELLLLLLLOOOOO'
yes, +1 but their first dict also contains double L and no guarantees on the order either (prior to 3.7)
– Chris_Rands
Nov 16 '18 at 16:11
1
While this is a good answer I think there's an arguable case against answering a question with virutally no effort from OP's end...
– Idlehands
Nov 16 '18 at 16:39
add a comment |
You get the first dictionary with
>>> s = "Hello"
>>> d = dict.fromkeys(s.upper(), 0)
>>> d
>>> {'E': 0, 'H': 0, 'L': 0, 'O': 0}
Note that the key 'L'
appears only once because dictionary keys are unique.
Your second desired dictionary is therefore impossible to create.
But if you only care about the final string, creating any dict is unnecessary, because:
>>> ''.join(c*i for i, c in enumerate(s.upper(), 1))
>>> 'HEELLLLLLLOOOOO'
If this looks complicated, just use a simple for
loop.
>>> to_join =
>>> for i, c in enumerate(s.upper(), 1):
...: to_join.append(c*i)
...:
>>> result = ''.join(to_join)
>>> result
>>> 'HEELLLLLLLOOOOO'
You get the first dictionary with
>>> s = "Hello"
>>> d = dict.fromkeys(s.upper(), 0)
>>> d
>>> {'E': 0, 'H': 0, 'L': 0, 'O': 0}
Note that the key 'L'
appears only once because dictionary keys are unique.
Your second desired dictionary is therefore impossible to create.
But if you only care about the final string, creating any dict is unnecessary, because:
>>> ''.join(c*i for i, c in enumerate(s.upper(), 1))
>>> 'HEELLLLLLLOOOOO'
If this looks complicated, just use a simple for
loop.
>>> to_join =
>>> for i, c in enumerate(s.upper(), 1):
...: to_join.append(c*i)
...:
>>> result = ''.join(to_join)
>>> result
>>> 'HEELLLLLLLOOOOO'
answered Nov 16 '18 at 16:02
timgebtimgeb
51.4k126794
51.4k126794
yes, +1 but their first dict also contains double L and no guarantees on the order either (prior to 3.7)
– Chris_Rands
Nov 16 '18 at 16:11
1
While this is a good answer I think there's an arguable case against answering a question with virutally no effort from OP's end...
– Idlehands
Nov 16 '18 at 16:39
add a comment |
yes, +1 but their first dict also contains double L and no guarantees on the order either (prior to 3.7)
– Chris_Rands
Nov 16 '18 at 16:11
1
While this is a good answer I think there's an arguable case against answering a question with virutally no effort from OP's end...
– Idlehands
Nov 16 '18 at 16:39
yes, +1 but their first dict also contains double L and no guarantees on the order either (prior to 3.7)
– Chris_Rands
Nov 16 '18 at 16:11
yes, +1 but their first dict also contains double L and no guarantees on the order either (prior to 3.7)
– Chris_Rands
Nov 16 '18 at 16:11
1
1
While this is a good answer I think there's an arguable case against answering a question with virutally no effort from OP's end...
– Idlehands
Nov 16 '18 at 16:39
While this is a good answer I think there's an arguable case against answering a question with virutally no effort from OP's end...
– Idlehands
Nov 16 '18 at 16:39
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53341403%2fpython-3-how-to-switch-the-case-of-a-dictionary-then-adding-numeric-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
What have you tried? What specifically do you need help with? Also note that depending on what version of Python you're using, dictionaries may or may not guarantee a certain order.
– Carcigenicate
Nov 16 '18 at 16:00
2
You know that you cannot have two times the same key inside of a dictionary? So getting
{ "H":0, "E":0, "L":0, "L":0, "O":0 }
is impossible.– quant
Nov 16 '18 at 16:02
To complete @Carcigenicate the order guarantee was declared to be part of language spec with Python 3.7. Of course if you rely on that, your code is not retro-compatible with older Python versions. Also it looks that the change was motivated by related conveniences described in PEP 468 more than because of insertion order (like kwargs order and attributes order, because dicts are used for that).
– progmatico
Nov 16 '18 at 16:36
Ok, I don't mean to go off the beaten path, but ... I don't understand ... I have asked just a few questions and they always get negative ratings? I tried to search this out, I tagged it, I am new to python and tried to make the question and title clear ... Can anyone help me understand why I get this negative feedback ? I thought we are supposed to ask questions to the community ? I am trying to learn and understand programming ... very confused
– Finstercode
Nov 17 '18 at 4:02