Check if a list of strings contains letters from another list
So I have two lists:
vocabulary = ['a','b','c']
sentences = ['a a b b c c', 'a c b c', 'b c c a b']
I want to calculate how many times the letters in vocabulary appear in the strings in the list sentences.
So I want the output to be:
a = 4
b = 5
c = 6
My program:
counter = Counter()
for word in sentences:
if word in vocabulary:
counter.update(word)
print(counter)
But I keep getting the output:
Counter()
python string python-3.x dictionary counter
add a comment |
So I have two lists:
vocabulary = ['a','b','c']
sentences = ['a a b b c c', 'a c b c', 'b c c a b']
I want to calculate how many times the letters in vocabulary appear in the strings in the list sentences.
So I want the output to be:
a = 4
b = 5
c = 6
My program:
counter = Counter()
for word in sentences:
if word in vocabulary:
counter.update(word)
print(counter)
But I keep getting the output:
Counter()
python string python-3.x dictionary counter
add a comment |
So I have two lists:
vocabulary = ['a','b','c']
sentences = ['a a b b c c', 'a c b c', 'b c c a b']
I want to calculate how many times the letters in vocabulary appear in the strings in the list sentences.
So I want the output to be:
a = 4
b = 5
c = 6
My program:
counter = Counter()
for word in sentences:
if word in vocabulary:
counter.update(word)
print(counter)
But I keep getting the output:
Counter()
python string python-3.x dictionary counter
So I have two lists:
vocabulary = ['a','b','c']
sentences = ['a a b b c c', 'a c b c', 'b c c a b']
I want to calculate how many times the letters in vocabulary appear in the strings in the list sentences.
So I want the output to be:
a = 4
b = 5
c = 6
My program:
counter = Counter()
for word in sentences:
if word in vocabulary:
counter.update(word)
print(counter)
But I keep getting the output:
Counter()
python string python-3.x dictionary counter
python string python-3.x dictionary counter
edited Nov 13 '18 at 13:31
jpp
97.4k2159109
97.4k2159109
asked Nov 13 '18 at 13:24
JameshGongJameshGong
486
486
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Counter
is a subclass of dict
. dict.update
accepts another dictionary or an iterable of pairs. But you're only supplying a single character.
In this case, you can chain your list of strings and pass to Counter
, then filter the result via a dictionary comprehension:
from collections import Counter
from itertools import chain
vocabulary = ['a','b','c']
sentences = ['a a b b c c', 'a c b c', 'b c c a b']
vocab_set = set(vocabulary)
c = Counter(chain.from_iterable(sentences))
res = {k: v for k, v in c.items() if k in vocab_set}
{'a': 4, 'b': 5, 'c': 6}
add a comment |
This will do it, no import
needed:
vocabulary = ['a','b','c']
sentences = ['a a b b c c', 'a c b c', 'b c c a b']
data = ''.join(sentences)
for v in vocabulary:
print('{}: {}'.format(v, data.count(v)))
a: 4
b: 5
c: 6
Worth noting this is at the cost of time complexity O(m * n) vs O(n).
– jpp
Nov 13 '18 at 13:33
add a comment |
An O(n) solution, with no import
:
vocabulary = ['a', 'b', 'c']
sentences = ['a a b b c c', 'a c b c', 'b c c a b']
counts = {}
vocab_set = set(vocabulary)
for sentence in sentences:
for ch in sentence:
if ch in vocab_set:
counts[ch] = counts.get(ch, 0) + 1
print(counts)
Output
{'c': 6, 'a': 4, 'b': 5}
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%2f53282019%2fcheck-if-a-list-of-strings-contains-letters-from-another-list%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
Counter
is a subclass of dict
. dict.update
accepts another dictionary or an iterable of pairs. But you're only supplying a single character.
In this case, you can chain your list of strings and pass to Counter
, then filter the result via a dictionary comprehension:
from collections import Counter
from itertools import chain
vocabulary = ['a','b','c']
sentences = ['a a b b c c', 'a c b c', 'b c c a b']
vocab_set = set(vocabulary)
c = Counter(chain.from_iterable(sentences))
res = {k: v for k, v in c.items() if k in vocab_set}
{'a': 4, 'b': 5, 'c': 6}
add a comment |
Counter
is a subclass of dict
. dict.update
accepts another dictionary or an iterable of pairs. But you're only supplying a single character.
In this case, you can chain your list of strings and pass to Counter
, then filter the result via a dictionary comprehension:
from collections import Counter
from itertools import chain
vocabulary = ['a','b','c']
sentences = ['a a b b c c', 'a c b c', 'b c c a b']
vocab_set = set(vocabulary)
c = Counter(chain.from_iterable(sentences))
res = {k: v for k, v in c.items() if k in vocab_set}
{'a': 4, 'b': 5, 'c': 6}
add a comment |
Counter
is a subclass of dict
. dict.update
accepts another dictionary or an iterable of pairs. But you're only supplying a single character.
In this case, you can chain your list of strings and pass to Counter
, then filter the result via a dictionary comprehension:
from collections import Counter
from itertools import chain
vocabulary = ['a','b','c']
sentences = ['a a b b c c', 'a c b c', 'b c c a b']
vocab_set = set(vocabulary)
c = Counter(chain.from_iterable(sentences))
res = {k: v for k, v in c.items() if k in vocab_set}
{'a': 4, 'b': 5, 'c': 6}
Counter
is a subclass of dict
. dict.update
accepts another dictionary or an iterable of pairs. But you're only supplying a single character.
In this case, you can chain your list of strings and pass to Counter
, then filter the result via a dictionary comprehension:
from collections import Counter
from itertools import chain
vocabulary = ['a','b','c']
sentences = ['a a b b c c', 'a c b c', 'b c c a b']
vocab_set = set(vocabulary)
c = Counter(chain.from_iterable(sentences))
res = {k: v for k, v in c.items() if k in vocab_set}
{'a': 4, 'b': 5, 'c': 6}
answered Nov 13 '18 at 13:30
jppjpp
97.4k2159109
97.4k2159109
add a comment |
add a comment |
This will do it, no import
needed:
vocabulary = ['a','b','c']
sentences = ['a a b b c c', 'a c b c', 'b c c a b']
data = ''.join(sentences)
for v in vocabulary:
print('{}: {}'.format(v, data.count(v)))
a: 4
b: 5
c: 6
Worth noting this is at the cost of time complexity O(m * n) vs O(n).
– jpp
Nov 13 '18 at 13:33
add a comment |
This will do it, no import
needed:
vocabulary = ['a','b','c']
sentences = ['a a b b c c', 'a c b c', 'b c c a b']
data = ''.join(sentences)
for v in vocabulary:
print('{}: {}'.format(v, data.count(v)))
a: 4
b: 5
c: 6
Worth noting this is at the cost of time complexity O(m * n) vs O(n).
– jpp
Nov 13 '18 at 13:33
add a comment |
This will do it, no import
needed:
vocabulary = ['a','b','c']
sentences = ['a a b b c c', 'a c b c', 'b c c a b']
data = ''.join(sentences)
for v in vocabulary:
print('{}: {}'.format(v, data.count(v)))
a: 4
b: 5
c: 6
This will do it, no import
needed:
vocabulary = ['a','b','c']
sentences = ['a a b b c c', 'a c b c', 'b c c a b']
data = ''.join(sentences)
for v in vocabulary:
print('{}: {}'.format(v, data.count(v)))
a: 4
b: 5
c: 6
answered Nov 13 '18 at 13:30
zipazipa
16k31637
16k31637
Worth noting this is at the cost of time complexity O(m * n) vs O(n).
– jpp
Nov 13 '18 at 13:33
add a comment |
Worth noting this is at the cost of time complexity O(m * n) vs O(n).
– jpp
Nov 13 '18 at 13:33
Worth noting this is at the cost of time complexity O(m * n) vs O(n).
– jpp
Nov 13 '18 at 13:33
Worth noting this is at the cost of time complexity O(m * n) vs O(n).
– jpp
Nov 13 '18 at 13:33
add a comment |
An O(n) solution, with no import
:
vocabulary = ['a', 'b', 'c']
sentences = ['a a b b c c', 'a c b c', 'b c c a b']
counts = {}
vocab_set = set(vocabulary)
for sentence in sentences:
for ch in sentence:
if ch in vocab_set:
counts[ch] = counts.get(ch, 0) + 1
print(counts)
Output
{'c': 6, 'a': 4, 'b': 5}
add a comment |
An O(n) solution, with no import
:
vocabulary = ['a', 'b', 'c']
sentences = ['a a b b c c', 'a c b c', 'b c c a b']
counts = {}
vocab_set = set(vocabulary)
for sentence in sentences:
for ch in sentence:
if ch in vocab_set:
counts[ch] = counts.get(ch, 0) + 1
print(counts)
Output
{'c': 6, 'a': 4, 'b': 5}
add a comment |
An O(n) solution, with no import
:
vocabulary = ['a', 'b', 'c']
sentences = ['a a b b c c', 'a c b c', 'b c c a b']
counts = {}
vocab_set = set(vocabulary)
for sentence in sentences:
for ch in sentence:
if ch in vocab_set:
counts[ch] = counts.get(ch, 0) + 1
print(counts)
Output
{'c': 6, 'a': 4, 'b': 5}
An O(n) solution, with no import
:
vocabulary = ['a', 'b', 'c']
sentences = ['a a b b c c', 'a c b c', 'b c c a b']
counts = {}
vocab_set = set(vocabulary)
for sentence in sentences:
for ch in sentence:
if ch in vocab_set:
counts[ch] = counts.get(ch, 0) + 1
print(counts)
Output
{'c': 6, 'a': 4, 'b': 5}
edited Nov 13 '18 at 13:52
answered Nov 13 '18 at 13:43
Daniel MesejoDaniel Mesejo
16.5k21430
16.5k21430
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%2f53282019%2fcheck-if-a-list-of-strings-contains-letters-from-another-list%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