how to fill dictionary values with the counts of similar keys in another dictionary
I have a dictionary (studentPerf) which has all of the students in a school, with tuples as keys. I want to count the number of male students and the number of female students in the school, and use this to update the values in a second dictionary. The second dictionary (dictDemGender) has 2 keys, male and female, and 0s as the values. How can I change the 0s in dictDemGender to reflect the number of males and females in the school?
Could I do this with dictionary comprehension?
I've given the first few entries to studentPerf:
studentPerf = {('Jeffery','male','junior'):[0.81,0.75,0.74,0.8],
('Able','male','senior'):[0.87,0.79,0.81,0.81],
('Don','male','junior'):[0.82,0.77,0.8,0.8],
('Will','male','senior'):[0.86,0.78,0.77,0.78],
('John','male','junior'):[0.74,0.81,0.87,0.73]}
#Creates a dictionary with genders as keys and 0s as the values to fill later
dictDemGender = {k:0 for k in genders}
dictDemGender = ?
I did ask a similar question but had diagnosed the problem incorrectly. I previously asked for help with finding an average score. What I actually need is a count of the different key possibilities. I need to be able to do so without any outside packages unfortunately.
python dictionary
add a comment |
I have a dictionary (studentPerf) which has all of the students in a school, with tuples as keys. I want to count the number of male students and the number of female students in the school, and use this to update the values in a second dictionary. The second dictionary (dictDemGender) has 2 keys, male and female, and 0s as the values. How can I change the 0s in dictDemGender to reflect the number of males and females in the school?
Could I do this with dictionary comprehension?
I've given the first few entries to studentPerf:
studentPerf = {('Jeffery','male','junior'):[0.81,0.75,0.74,0.8],
('Able','male','senior'):[0.87,0.79,0.81,0.81],
('Don','male','junior'):[0.82,0.77,0.8,0.8],
('Will','male','senior'):[0.86,0.78,0.77,0.78],
('John','male','junior'):[0.74,0.81,0.87,0.73]}
#Creates a dictionary with genders as keys and 0s as the values to fill later
dictDemGender = {k:0 for k in genders}
dictDemGender = ?
I did ask a similar question but had diagnosed the problem incorrectly. I previously asked for help with finding an average score. What I actually need is a count of the different key possibilities. I need to be able to do so without any outside packages unfortunately.
python dictionary
Possible duplicate of How do I fill my dictionary values with the values from another dictionary where their keys are the same?
– slider
Nov 19 '18 at 17:37
1
What exactly is the expected output?{'male':5, 'female':0}
?
– timgeb
Nov 19 '18 at 17:37
1
You asked a similar question yesterday. What have you tried on your own?
– slider
Nov 19 '18 at 17:37
expected output is {'male':5, 'female:'0'} (of course with the full studentPerf dictionary those numbers will be much higher.
– Jacob Myer
Nov 19 '18 at 17:41
@slider , I'm not sure if you've seen my post edits yet or not. After I posted yesterday I realized I made a mistake and was looking for the wrong output. I was able to come up with a solution on my own that fit the criteria and I posted that solution here
– Jacob Myer
Nov 19 '18 at 19:55
add a comment |
I have a dictionary (studentPerf) which has all of the students in a school, with tuples as keys. I want to count the number of male students and the number of female students in the school, and use this to update the values in a second dictionary. The second dictionary (dictDemGender) has 2 keys, male and female, and 0s as the values. How can I change the 0s in dictDemGender to reflect the number of males and females in the school?
Could I do this with dictionary comprehension?
I've given the first few entries to studentPerf:
studentPerf = {('Jeffery','male','junior'):[0.81,0.75,0.74,0.8],
('Able','male','senior'):[0.87,0.79,0.81,0.81],
('Don','male','junior'):[0.82,0.77,0.8,0.8],
('Will','male','senior'):[0.86,0.78,0.77,0.78],
('John','male','junior'):[0.74,0.81,0.87,0.73]}
#Creates a dictionary with genders as keys and 0s as the values to fill later
dictDemGender = {k:0 for k in genders}
dictDemGender = ?
I did ask a similar question but had diagnosed the problem incorrectly. I previously asked for help with finding an average score. What I actually need is a count of the different key possibilities. I need to be able to do so without any outside packages unfortunately.
python dictionary
I have a dictionary (studentPerf) which has all of the students in a school, with tuples as keys. I want to count the number of male students and the number of female students in the school, and use this to update the values in a second dictionary. The second dictionary (dictDemGender) has 2 keys, male and female, and 0s as the values. How can I change the 0s in dictDemGender to reflect the number of males and females in the school?
Could I do this with dictionary comprehension?
I've given the first few entries to studentPerf:
studentPerf = {('Jeffery','male','junior'):[0.81,0.75,0.74,0.8],
('Able','male','senior'):[0.87,0.79,0.81,0.81],
('Don','male','junior'):[0.82,0.77,0.8,0.8],
('Will','male','senior'):[0.86,0.78,0.77,0.78],
('John','male','junior'):[0.74,0.81,0.87,0.73]}
#Creates a dictionary with genders as keys and 0s as the values to fill later
dictDemGender = {k:0 for k in genders}
dictDemGender = ?
I did ask a similar question but had diagnosed the problem incorrectly. I previously asked for help with finding an average score. What I actually need is a count of the different key possibilities. I need to be able to do so without any outside packages unfortunately.
python dictionary
python dictionary
edited Nov 19 '18 at 17:49
Jacob Myer
asked Nov 19 '18 at 17:34
Jacob MyerJacob Myer
496
496
Possible duplicate of How do I fill my dictionary values with the values from another dictionary where their keys are the same?
– slider
Nov 19 '18 at 17:37
1
What exactly is the expected output?{'male':5, 'female':0}
?
– timgeb
Nov 19 '18 at 17:37
1
You asked a similar question yesterday. What have you tried on your own?
– slider
Nov 19 '18 at 17:37
expected output is {'male':5, 'female:'0'} (of course with the full studentPerf dictionary those numbers will be much higher.
– Jacob Myer
Nov 19 '18 at 17:41
@slider , I'm not sure if you've seen my post edits yet or not. After I posted yesterday I realized I made a mistake and was looking for the wrong output. I was able to come up with a solution on my own that fit the criteria and I posted that solution here
– Jacob Myer
Nov 19 '18 at 19:55
add a comment |
Possible duplicate of How do I fill my dictionary values with the values from another dictionary where their keys are the same?
– slider
Nov 19 '18 at 17:37
1
What exactly is the expected output?{'male':5, 'female':0}
?
– timgeb
Nov 19 '18 at 17:37
1
You asked a similar question yesterday. What have you tried on your own?
– slider
Nov 19 '18 at 17:37
expected output is {'male':5, 'female:'0'} (of course with the full studentPerf dictionary those numbers will be much higher.
– Jacob Myer
Nov 19 '18 at 17:41
@slider , I'm not sure if you've seen my post edits yet or not. After I posted yesterday I realized I made a mistake and was looking for the wrong output. I was able to come up with a solution on my own that fit the criteria and I posted that solution here
– Jacob Myer
Nov 19 '18 at 19:55
Possible duplicate of How do I fill my dictionary values with the values from another dictionary where their keys are the same?
– slider
Nov 19 '18 at 17:37
Possible duplicate of How do I fill my dictionary values with the values from another dictionary where their keys are the same?
– slider
Nov 19 '18 at 17:37
1
1
What exactly is the expected output?
{'male':5, 'female':0}
?– timgeb
Nov 19 '18 at 17:37
What exactly is the expected output?
{'male':5, 'female':0}
?– timgeb
Nov 19 '18 at 17:37
1
1
You asked a similar question yesterday. What have you tried on your own?
– slider
Nov 19 '18 at 17:37
You asked a similar question yesterday. What have you tried on your own?
– slider
Nov 19 '18 at 17:37
expected output is {'male':5, 'female:'0'} (of course with the full studentPerf dictionary those numbers will be much higher.
– Jacob Myer
Nov 19 '18 at 17:41
expected output is {'male':5, 'female:'0'} (of course with the full studentPerf dictionary those numbers will be much higher.
– Jacob Myer
Nov 19 '18 at 17:41
@slider , I'm not sure if you've seen my post edits yet or not. After I posted yesterday I realized I made a mistake and was looking for the wrong output. I was able to come up with a solution on my own that fit the criteria and I posted that solution here
– Jacob Myer
Nov 19 '18 at 19:55
@slider , I'm not sure if you've seen my post edits yet or not. After I posted yesterday I realized I made a mistake and was looking for the wrong output. I was able to come up with a solution on my own that fit the criteria and I posted that solution here
– Jacob Myer
Nov 19 '18 at 19:55
add a comment |
3 Answers
3
active
oldest
votes
Use collections.Counter
:
from collections import Counter
studentPerf = {('Jeffery','male','junior'):[0.81,0.75,0.74,0.8],
('Able','male','senior'):[0.87,0.79,0.81,0.81],
('Don','male','junior'):[0.82,0.77,0.8,0.8],
('Will','male','senior'):[0.86,0.78,0.77,0.78],
('John','male','junior'):[0.74,0.81,0.87,0.73]}
print(Counter(x[1] for x in studentPerf))
# Counter({'male': 5})
Or, if you need empty counts also:
gender = {'male': 0, 'female': 0}
gender.update(Counter(x[1] for x in studentPerf))
# {'male': 5, 'female': 0}
Or, using dict.fromkeys()
with Counter
:
d = {'male', 'female'}
gender = dict.fromkeys(d, 0)
gender.update(Counter(x[1] for x in studentPerf))
# {'female': 0, 'male': 5}
add a comment |
Assuming the expected output is {'male':5, 'female':0}
, consider using a Counter
.
>>> from collections import Counter
>>> c = Counter(male=0, female=0)
>>> c.update(gen for _, gen, _ in studentPerf)
>>> c
Counter({'female': 0, 'male': 5})
Initializing the two keys with zeros is not really necessary, you could also write
>>> c = Counter(gen for _, gen, _ in studentPerf)
>>> c
Counter({'male': 5})
because Counter
lookup defaults to zero for missing keys:
>>> c['female']
0
add a comment |
As I said, I was looking for a solution that did not require outside packages. I know the way I've gone about this is rather cumbersome but this was for a class and the exercise had these requirements. I found a way to count all of the males and females and input those values into the dictDemGender dictionary.
genCounts = ([x[1] for x in list(studentPerf.keys())].count('female'), [x[1] for x in list(studentPerf.keys())].count('male'))
dictDemGender = dict(zip(dictDemGender.keys(), genCounts))
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%2f53379901%2fhow-to-fill-dictionary-values-with-the-counts-of-similar-keys-in-another-diction%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use collections.Counter
:
from collections import Counter
studentPerf = {('Jeffery','male','junior'):[0.81,0.75,0.74,0.8],
('Able','male','senior'):[0.87,0.79,0.81,0.81],
('Don','male','junior'):[0.82,0.77,0.8,0.8],
('Will','male','senior'):[0.86,0.78,0.77,0.78],
('John','male','junior'):[0.74,0.81,0.87,0.73]}
print(Counter(x[1] for x in studentPerf))
# Counter({'male': 5})
Or, if you need empty counts also:
gender = {'male': 0, 'female': 0}
gender.update(Counter(x[1] for x in studentPerf))
# {'male': 5, 'female': 0}
Or, using dict.fromkeys()
with Counter
:
d = {'male', 'female'}
gender = dict.fromkeys(d, 0)
gender.update(Counter(x[1] for x in studentPerf))
# {'female': 0, 'male': 5}
add a comment |
Use collections.Counter
:
from collections import Counter
studentPerf = {('Jeffery','male','junior'):[0.81,0.75,0.74,0.8],
('Able','male','senior'):[0.87,0.79,0.81,0.81],
('Don','male','junior'):[0.82,0.77,0.8,0.8],
('Will','male','senior'):[0.86,0.78,0.77,0.78],
('John','male','junior'):[0.74,0.81,0.87,0.73]}
print(Counter(x[1] for x in studentPerf))
# Counter({'male': 5})
Or, if you need empty counts also:
gender = {'male': 0, 'female': 0}
gender.update(Counter(x[1] for x in studentPerf))
# {'male': 5, 'female': 0}
Or, using dict.fromkeys()
with Counter
:
d = {'male', 'female'}
gender = dict.fromkeys(d, 0)
gender.update(Counter(x[1] for x in studentPerf))
# {'female': 0, 'male': 5}
add a comment |
Use collections.Counter
:
from collections import Counter
studentPerf = {('Jeffery','male','junior'):[0.81,0.75,0.74,0.8],
('Able','male','senior'):[0.87,0.79,0.81,0.81],
('Don','male','junior'):[0.82,0.77,0.8,0.8],
('Will','male','senior'):[0.86,0.78,0.77,0.78],
('John','male','junior'):[0.74,0.81,0.87,0.73]}
print(Counter(x[1] for x in studentPerf))
# Counter({'male': 5})
Or, if you need empty counts also:
gender = {'male': 0, 'female': 0}
gender.update(Counter(x[1] for x in studentPerf))
# {'male': 5, 'female': 0}
Or, using dict.fromkeys()
with Counter
:
d = {'male', 'female'}
gender = dict.fromkeys(d, 0)
gender.update(Counter(x[1] for x in studentPerf))
# {'female': 0, 'male': 5}
Use collections.Counter
:
from collections import Counter
studentPerf = {('Jeffery','male','junior'):[0.81,0.75,0.74,0.8],
('Able','male','senior'):[0.87,0.79,0.81,0.81],
('Don','male','junior'):[0.82,0.77,0.8,0.8],
('Will','male','senior'):[0.86,0.78,0.77,0.78],
('John','male','junior'):[0.74,0.81,0.87,0.73]}
print(Counter(x[1] for x in studentPerf))
# Counter({'male': 5})
Or, if you need empty counts also:
gender = {'male': 0, 'female': 0}
gender.update(Counter(x[1] for x in studentPerf))
# {'male': 5, 'female': 0}
Or, using dict.fromkeys()
with Counter
:
d = {'male', 'female'}
gender = dict.fromkeys(d, 0)
gender.update(Counter(x[1] for x in studentPerf))
# {'female': 0, 'male': 5}
edited Nov 19 '18 at 17:46
answered Nov 19 '18 at 17:37
AustinAustin
9,8733828
9,8733828
add a comment |
add a comment |
Assuming the expected output is {'male':5, 'female':0}
, consider using a Counter
.
>>> from collections import Counter
>>> c = Counter(male=0, female=0)
>>> c.update(gen for _, gen, _ in studentPerf)
>>> c
Counter({'female': 0, 'male': 5})
Initializing the two keys with zeros is not really necessary, you could also write
>>> c = Counter(gen for _, gen, _ in studentPerf)
>>> c
Counter({'male': 5})
because Counter
lookup defaults to zero for missing keys:
>>> c['female']
0
add a comment |
Assuming the expected output is {'male':5, 'female':0}
, consider using a Counter
.
>>> from collections import Counter
>>> c = Counter(male=0, female=0)
>>> c.update(gen for _, gen, _ in studentPerf)
>>> c
Counter({'female': 0, 'male': 5})
Initializing the two keys with zeros is not really necessary, you could also write
>>> c = Counter(gen for _, gen, _ in studentPerf)
>>> c
Counter({'male': 5})
because Counter
lookup defaults to zero for missing keys:
>>> c['female']
0
add a comment |
Assuming the expected output is {'male':5, 'female':0}
, consider using a Counter
.
>>> from collections import Counter
>>> c = Counter(male=0, female=0)
>>> c.update(gen for _, gen, _ in studentPerf)
>>> c
Counter({'female': 0, 'male': 5})
Initializing the two keys with zeros is not really necessary, you could also write
>>> c = Counter(gen for _, gen, _ in studentPerf)
>>> c
Counter({'male': 5})
because Counter
lookup defaults to zero for missing keys:
>>> c['female']
0
Assuming the expected output is {'male':5, 'female':0}
, consider using a Counter
.
>>> from collections import Counter
>>> c = Counter(male=0, female=0)
>>> c.update(gen for _, gen, _ in studentPerf)
>>> c
Counter({'female': 0, 'male': 5})
Initializing the two keys with zeros is not really necessary, you could also write
>>> c = Counter(gen for _, gen, _ in studentPerf)
>>> c
Counter({'male': 5})
because Counter
lookup defaults to zero for missing keys:
>>> c['female']
0
answered Nov 19 '18 at 17:42
timgebtimgeb
50.6k116393
50.6k116393
add a comment |
add a comment |
As I said, I was looking for a solution that did not require outside packages. I know the way I've gone about this is rather cumbersome but this was for a class and the exercise had these requirements. I found a way to count all of the males and females and input those values into the dictDemGender dictionary.
genCounts = ([x[1] for x in list(studentPerf.keys())].count('female'), [x[1] for x in list(studentPerf.keys())].count('male'))
dictDemGender = dict(zip(dictDemGender.keys(), genCounts))
add a comment |
As I said, I was looking for a solution that did not require outside packages. I know the way I've gone about this is rather cumbersome but this was for a class and the exercise had these requirements. I found a way to count all of the males and females and input those values into the dictDemGender dictionary.
genCounts = ([x[1] for x in list(studentPerf.keys())].count('female'), [x[1] for x in list(studentPerf.keys())].count('male'))
dictDemGender = dict(zip(dictDemGender.keys(), genCounts))
add a comment |
As I said, I was looking for a solution that did not require outside packages. I know the way I've gone about this is rather cumbersome but this was for a class and the exercise had these requirements. I found a way to count all of the males and females and input those values into the dictDemGender dictionary.
genCounts = ([x[1] for x in list(studentPerf.keys())].count('female'), [x[1] for x in list(studentPerf.keys())].count('male'))
dictDemGender = dict(zip(dictDemGender.keys(), genCounts))
As I said, I was looking for a solution that did not require outside packages. I know the way I've gone about this is rather cumbersome but this was for a class and the exercise had these requirements. I found a way to count all of the males and females and input those values into the dictDemGender dictionary.
genCounts = ([x[1] for x in list(studentPerf.keys())].count('female'), [x[1] for x in list(studentPerf.keys())].count('male'))
dictDemGender = dict(zip(dictDemGender.keys(), genCounts))
answered Nov 19 '18 at 19:52
Jacob MyerJacob Myer
496
496
add a comment |
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%2f53379901%2fhow-to-fill-dictionary-values-with-the-counts-of-similar-keys-in-another-diction%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
Possible duplicate of How do I fill my dictionary values with the values from another dictionary where their keys are the same?
– slider
Nov 19 '18 at 17:37
1
What exactly is the expected output?
{'male':5, 'female':0}
?– timgeb
Nov 19 '18 at 17:37
1
You asked a similar question yesterday. What have you tried on your own?
– slider
Nov 19 '18 at 17:37
expected output is {'male':5, 'female:'0'} (of course with the full studentPerf dictionary those numbers will be much higher.
– Jacob Myer
Nov 19 '18 at 17:41
@slider , I'm not sure if you've seen my post edits yet or not. After I posted yesterday I realized I made a mistake and was looking for the wrong output. I was able to come up with a solution on my own that fit the criteria and I posted that solution here
– Jacob Myer
Nov 19 '18 at 19:55