How to make subset of Counter?
up vote
2
down vote
favorite
I am experimenting with Python standard library Collections.
I have a Counter of things as
>>> c = Counter('achdnsdsdsdsdklaffaefhaew')
>>> c
Counter({'a': 4,
'c': 1,
'h': 2,
'd': 5,
'n': 1,
's': 4,
'k': 1,
'l': 1,
'f': 3,
'e': 2,
'w': 1})
What I want now is to somehow get subset of this counter as another Counter object. Just like this:
>>> new_c = do_subset(c, [d,s,l,e,w])
>>> new_c
Counter({'d': 5,
's': 4,
'l': 1,
'e': 2,
'w': 1})
Thank you in advance.
python data-structures python-collections
add a comment |
up vote
2
down vote
favorite
I am experimenting with Python standard library Collections.
I have a Counter of things as
>>> c = Counter('achdnsdsdsdsdklaffaefhaew')
>>> c
Counter({'a': 4,
'c': 1,
'h': 2,
'd': 5,
'n': 1,
's': 4,
'k': 1,
'l': 1,
'f': 3,
'e': 2,
'w': 1})
What I want now is to somehow get subset of this counter as another Counter object. Just like this:
>>> new_c = do_subset(c, [d,s,l,e,w])
>>> new_c
Counter({'d': 5,
's': 4,
'l': 1,
'e': 2,
'w': 1})
Thank you in advance.
python data-structures python-collections
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am experimenting with Python standard library Collections.
I have a Counter of things as
>>> c = Counter('achdnsdsdsdsdklaffaefhaew')
>>> c
Counter({'a': 4,
'c': 1,
'h': 2,
'd': 5,
'n': 1,
's': 4,
'k': 1,
'l': 1,
'f': 3,
'e': 2,
'w': 1})
What I want now is to somehow get subset of this counter as another Counter object. Just like this:
>>> new_c = do_subset(c, [d,s,l,e,w])
>>> new_c
Counter({'d': 5,
's': 4,
'l': 1,
'e': 2,
'w': 1})
Thank you in advance.
python data-structures python-collections
I am experimenting with Python standard library Collections.
I have a Counter of things as
>>> c = Counter('achdnsdsdsdsdklaffaefhaew')
>>> c
Counter({'a': 4,
'c': 1,
'h': 2,
'd': 5,
'n': 1,
's': 4,
'k': 1,
'l': 1,
'f': 3,
'e': 2,
'w': 1})
What I want now is to somehow get subset of this counter as another Counter object. Just like this:
>>> new_c = do_subset(c, [d,s,l,e,w])
>>> new_c
Counter({'d': 5,
's': 4,
'l': 1,
'e': 2,
'w': 1})
Thank you in advance.
python data-structures python-collections
python data-structures python-collections
asked Nov 11 at 2:08
Jay Patel
463
463
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
You could simply build a dictionary and pass it to Counter:
from collections import Counter
c = Counter({'a': 4,
'c': 1,
'h': 2,
'd': 5,
'n': 1,
's': 4,
'k': 1,
'l': 1,
'f': 3,
'e': 2,
'w': 1})
def do_subset(counter, lst):
return Counter({k: counter.get(k, 0) for k in lst})
result = do_subset(c, ['d', 's', 'l', 'e', 'w'])
print(result)
Output
Counter({'d': 5, 's': 4, 'e': 2, 'l': 1, 'w': 1})
2
If you are lazy, you can also pass'dslew'
because strings are iterable.
– jpp
Nov 11 at 2:14
Thank you @daniel it worked.
– Jay Patel
Nov 11 at 2:17
@jpp you made it shorter... awesome...
– Jay Patel
Nov 11 at 2:18
@JayPatel Glad I could help! If my answer helped to solve your problem, please consider marking it as accepted. That's the customary way of indicating that your question is "resolved" and thanking the person who helped you.
– Daniel Mesejo
Nov 11 at 2:20
add a comment |
up vote
0
down vote
You can access each key in c
and assign its value to the same key in a new dict.
import collections
c = collections.Counter('achdnsdsdsdsdklaffaefhaew')
def subsetter(c, sub):
out = {}
for x in sub:
out[x] = c[x]
return collections.Counter(out)
subsetter(c, ["d","s","l","e","w"])
Yields:
{'d': 5, 'e': 2, 'l': 1, 's': 4, 'w': 1}
OP wants a Counter not a dict.
– Red Cricket
Nov 11 at 2:19
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
You could simply build a dictionary and pass it to Counter:
from collections import Counter
c = Counter({'a': 4,
'c': 1,
'h': 2,
'd': 5,
'n': 1,
's': 4,
'k': 1,
'l': 1,
'f': 3,
'e': 2,
'w': 1})
def do_subset(counter, lst):
return Counter({k: counter.get(k, 0) for k in lst})
result = do_subset(c, ['d', 's', 'l', 'e', 'w'])
print(result)
Output
Counter({'d': 5, 's': 4, 'e': 2, 'l': 1, 'w': 1})
2
If you are lazy, you can also pass'dslew'
because strings are iterable.
– jpp
Nov 11 at 2:14
Thank you @daniel it worked.
– Jay Patel
Nov 11 at 2:17
@jpp you made it shorter... awesome...
– Jay Patel
Nov 11 at 2:18
@JayPatel Glad I could help! If my answer helped to solve your problem, please consider marking it as accepted. That's the customary way of indicating that your question is "resolved" and thanking the person who helped you.
– Daniel Mesejo
Nov 11 at 2:20
add a comment |
up vote
4
down vote
accepted
You could simply build a dictionary and pass it to Counter:
from collections import Counter
c = Counter({'a': 4,
'c': 1,
'h': 2,
'd': 5,
'n': 1,
's': 4,
'k': 1,
'l': 1,
'f': 3,
'e': 2,
'w': 1})
def do_subset(counter, lst):
return Counter({k: counter.get(k, 0) for k in lst})
result = do_subset(c, ['d', 's', 'l', 'e', 'w'])
print(result)
Output
Counter({'d': 5, 's': 4, 'e': 2, 'l': 1, 'w': 1})
2
If you are lazy, you can also pass'dslew'
because strings are iterable.
– jpp
Nov 11 at 2:14
Thank you @daniel it worked.
– Jay Patel
Nov 11 at 2:17
@jpp you made it shorter... awesome...
– Jay Patel
Nov 11 at 2:18
@JayPatel Glad I could help! If my answer helped to solve your problem, please consider marking it as accepted. That's the customary way of indicating that your question is "resolved" and thanking the person who helped you.
– Daniel Mesejo
Nov 11 at 2:20
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
You could simply build a dictionary and pass it to Counter:
from collections import Counter
c = Counter({'a': 4,
'c': 1,
'h': 2,
'd': 5,
'n': 1,
's': 4,
'k': 1,
'l': 1,
'f': 3,
'e': 2,
'w': 1})
def do_subset(counter, lst):
return Counter({k: counter.get(k, 0) for k in lst})
result = do_subset(c, ['d', 's', 'l', 'e', 'w'])
print(result)
Output
Counter({'d': 5, 's': 4, 'e': 2, 'l': 1, 'w': 1})
You could simply build a dictionary and pass it to Counter:
from collections import Counter
c = Counter({'a': 4,
'c': 1,
'h': 2,
'd': 5,
'n': 1,
's': 4,
'k': 1,
'l': 1,
'f': 3,
'e': 2,
'w': 1})
def do_subset(counter, lst):
return Counter({k: counter.get(k, 0) for k in lst})
result = do_subset(c, ['d', 's', 'l', 'e', 'w'])
print(result)
Output
Counter({'d': 5, 's': 4, 'e': 2, 'l': 1, 'w': 1})
answered Nov 11 at 2:12
Daniel Mesejo
8,3791923
8,3791923
2
If you are lazy, you can also pass'dslew'
because strings are iterable.
– jpp
Nov 11 at 2:14
Thank you @daniel it worked.
– Jay Patel
Nov 11 at 2:17
@jpp you made it shorter... awesome...
– Jay Patel
Nov 11 at 2:18
@JayPatel Glad I could help! If my answer helped to solve your problem, please consider marking it as accepted. That's the customary way of indicating that your question is "resolved" and thanking the person who helped you.
– Daniel Mesejo
Nov 11 at 2:20
add a comment |
2
If you are lazy, you can also pass'dslew'
because strings are iterable.
– jpp
Nov 11 at 2:14
Thank you @daniel it worked.
– Jay Patel
Nov 11 at 2:17
@jpp you made it shorter... awesome...
– Jay Patel
Nov 11 at 2:18
@JayPatel Glad I could help! If my answer helped to solve your problem, please consider marking it as accepted. That's the customary way of indicating that your question is "resolved" and thanking the person who helped you.
– Daniel Mesejo
Nov 11 at 2:20
2
2
If you are lazy, you can also pass
'dslew'
because strings are iterable.– jpp
Nov 11 at 2:14
If you are lazy, you can also pass
'dslew'
because strings are iterable.– jpp
Nov 11 at 2:14
Thank you @daniel it worked.
– Jay Patel
Nov 11 at 2:17
Thank you @daniel it worked.
– Jay Patel
Nov 11 at 2:17
@jpp you made it shorter... awesome...
– Jay Patel
Nov 11 at 2:18
@jpp you made it shorter... awesome...
– Jay Patel
Nov 11 at 2:18
@JayPatel Glad I could help! If my answer helped to solve your problem, please consider marking it as accepted. That's the customary way of indicating that your question is "resolved" and thanking the person who helped you.
– Daniel Mesejo
Nov 11 at 2:20
@JayPatel Glad I could help! If my answer helped to solve your problem, please consider marking it as accepted. That's the customary way of indicating that your question is "resolved" and thanking the person who helped you.
– Daniel Mesejo
Nov 11 at 2:20
add a comment |
up vote
0
down vote
You can access each key in c
and assign its value to the same key in a new dict.
import collections
c = collections.Counter('achdnsdsdsdsdklaffaefhaew')
def subsetter(c, sub):
out = {}
for x in sub:
out[x] = c[x]
return collections.Counter(out)
subsetter(c, ["d","s","l","e","w"])
Yields:
{'d': 5, 'e': 2, 'l': 1, 's': 4, 'w': 1}
OP wants a Counter not a dict.
– Red Cricket
Nov 11 at 2:19
add a comment |
up vote
0
down vote
You can access each key in c
and assign its value to the same key in a new dict.
import collections
c = collections.Counter('achdnsdsdsdsdklaffaefhaew')
def subsetter(c, sub):
out = {}
for x in sub:
out[x] = c[x]
return collections.Counter(out)
subsetter(c, ["d","s","l","e","w"])
Yields:
{'d': 5, 'e': 2, 'l': 1, 's': 4, 'w': 1}
OP wants a Counter not a dict.
– Red Cricket
Nov 11 at 2:19
add a comment |
up vote
0
down vote
up vote
0
down vote
You can access each key in c
and assign its value to the same key in a new dict.
import collections
c = collections.Counter('achdnsdsdsdsdklaffaefhaew')
def subsetter(c, sub):
out = {}
for x in sub:
out[x] = c[x]
return collections.Counter(out)
subsetter(c, ["d","s","l","e","w"])
Yields:
{'d': 5, 'e': 2, 'l': 1, 's': 4, 'w': 1}
You can access each key in c
and assign its value to the same key in a new dict.
import collections
c = collections.Counter('achdnsdsdsdsdklaffaefhaew')
def subsetter(c, sub):
out = {}
for x in sub:
out[x] = c[x]
return collections.Counter(out)
subsetter(c, ["d","s","l","e","w"])
Yields:
{'d': 5, 'e': 2, 'l': 1, 's': 4, 'w': 1}
edited Nov 11 at 2:20
answered Nov 11 at 2:12
Charles Landau
1,2461212
1,2461212
OP wants a Counter not a dict.
– Red Cricket
Nov 11 at 2:19
add a comment |
OP wants a Counter not a dict.
– Red Cricket
Nov 11 at 2:19
OP wants a Counter not a dict.
– Red Cricket
Nov 11 at 2:19
OP wants a Counter not a dict.
– Red Cricket
Nov 11 at 2:19
add a comment |
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%2f53245239%2fhow-to-make-subset-of-counter%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