How to check if a certain string repeats more than once in a list
In my code, I want to check whether I could check if a certain string e.g
a="."
repeats more than once in a list. e.g
b=[".", ".", "Hello world"]
how would I do that?
python python-3.x
add a comment |
In my code, I want to check whether I could check if a certain string e.g
a="."
repeats more than once in a list. e.g
b=[".", ".", "Hello world"]
how would I do that?
python python-3.x
1
b.count(a) > 1
– sashaaero
Nov 22 '18 at 7:04
add a comment |
In my code, I want to check whether I could check if a certain string e.g
a="."
repeats more than once in a list. e.g
b=[".", ".", "Hello world"]
how would I do that?
python python-3.x
In my code, I want to check whether I could check if a certain string e.g
a="."
repeats more than once in a list. e.g
b=[".", ".", "Hello world"]
how would I do that?
python python-3.x
python python-3.x
asked Nov 22 '18 at 7:03


Coder CodyCoder Cody
408
408
1
b.count(a) > 1
– sashaaero
Nov 22 '18 at 7:04
add a comment |
1
b.count(a) > 1
– sashaaero
Nov 22 '18 at 7:04
1
1
b.count(a) > 1
– sashaaero
Nov 22 '18 at 7:04
b.count(a) > 1
– sashaaero
Nov 22 '18 at 7:04
add a comment |
5 Answers
5
active
oldest
votes
b=[".", ".", "Hello world"]
print(b.count(a))
>>> 2
Use count
function.
add a comment |
You can use the built in count
method
n_occurences = b.count(a)
add a comment |
Use collections.Counter()
or simply list.count()
list.count(x)
will return the count of x
in list
b=[".", ".", "Hello world"]
# Check for "."
print(b.count(".")
collections.Counter
will return a dictionary, which have count
information of every item in a list:
from collections import Counter
b=[".", ".", "Hello world"]
# Check for "."
count = b.Counter()
if count["."] > 1:
print("More than one occurance")
I thinkcollections.Counter
is overkill for this case.
– sashaaero
Nov 22 '18 at 7:05
@sashaaero you are right. I've read OP carelessly and only come with the idea of usingCounter
. I've made an edit,count()
is sufficient for such tasks :D
– enamoria
Nov 22 '18 at 7:09
add a comment |
Use count function.
a="."
b=[".", ".", "Hello world"]
if b.count(a) > 1:
print("more than 1 time")
add a comment |
Using collections.Counter
:
from collections import Counter
a = "."
b=[".", ".", "Hello world"]
print(Counter(b)[a]) # 2
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%2f53425474%2fhow-to-check-if-a-certain-string-repeats-more-than-once-in-a-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
b=[".", ".", "Hello world"]
print(b.count(a))
>>> 2
Use count
function.
add a comment |
b=[".", ".", "Hello world"]
print(b.count(a))
>>> 2
Use count
function.
add a comment |
b=[".", ".", "Hello world"]
print(b.count(a))
>>> 2
Use count
function.
b=[".", ".", "Hello world"]
print(b.count(a))
>>> 2
Use count
function.
answered Nov 22 '18 at 7:04


Sandesh34Sandesh34
254113
254113
add a comment |
add a comment |
You can use the built in count
method
n_occurences = b.count(a)
add a comment |
You can use the built in count
method
n_occurences = b.count(a)
add a comment |
You can use the built in count
method
n_occurences = b.count(a)
You can use the built in count
method
n_occurences = b.count(a)
answered Nov 22 '18 at 7:04


Sven HarrisSven Harris
2,1881515
2,1881515
add a comment |
add a comment |
Use collections.Counter()
or simply list.count()
list.count(x)
will return the count of x
in list
b=[".", ".", "Hello world"]
# Check for "."
print(b.count(".")
collections.Counter
will return a dictionary, which have count
information of every item in a list:
from collections import Counter
b=[".", ".", "Hello world"]
# Check for "."
count = b.Counter()
if count["."] > 1:
print("More than one occurance")
I thinkcollections.Counter
is overkill for this case.
– sashaaero
Nov 22 '18 at 7:05
@sashaaero you are right. I've read OP carelessly and only come with the idea of usingCounter
. I've made an edit,count()
is sufficient for such tasks :D
– enamoria
Nov 22 '18 at 7:09
add a comment |
Use collections.Counter()
or simply list.count()
list.count(x)
will return the count of x
in list
b=[".", ".", "Hello world"]
# Check for "."
print(b.count(".")
collections.Counter
will return a dictionary, which have count
information of every item in a list:
from collections import Counter
b=[".", ".", "Hello world"]
# Check for "."
count = b.Counter()
if count["."] > 1:
print("More than one occurance")
I thinkcollections.Counter
is overkill for this case.
– sashaaero
Nov 22 '18 at 7:05
@sashaaero you are right. I've read OP carelessly and only come with the idea of usingCounter
. I've made an edit,count()
is sufficient for such tasks :D
– enamoria
Nov 22 '18 at 7:09
add a comment |
Use collections.Counter()
or simply list.count()
list.count(x)
will return the count of x
in list
b=[".", ".", "Hello world"]
# Check for "."
print(b.count(".")
collections.Counter
will return a dictionary, which have count
information of every item in a list:
from collections import Counter
b=[".", ".", "Hello world"]
# Check for "."
count = b.Counter()
if count["."] > 1:
print("More than one occurance")
Use collections.Counter()
or simply list.count()
list.count(x)
will return the count of x
in list
b=[".", ".", "Hello world"]
# Check for "."
print(b.count(".")
collections.Counter
will return a dictionary, which have count
information of every item in a list:
from collections import Counter
b=[".", ".", "Hello world"]
# Check for "."
count = b.Counter()
if count["."] > 1:
print("More than one occurance")
edited Nov 22 '18 at 7:07
answered Nov 22 '18 at 7:05
enamoriaenamoria
617621
617621
I thinkcollections.Counter
is overkill for this case.
– sashaaero
Nov 22 '18 at 7:05
@sashaaero you are right. I've read OP carelessly and only come with the idea of usingCounter
. I've made an edit,count()
is sufficient for such tasks :D
– enamoria
Nov 22 '18 at 7:09
add a comment |
I thinkcollections.Counter
is overkill for this case.
– sashaaero
Nov 22 '18 at 7:05
@sashaaero you are right. I've read OP carelessly and only come with the idea of usingCounter
. I've made an edit,count()
is sufficient for such tasks :D
– enamoria
Nov 22 '18 at 7:09
I think
collections.Counter
is overkill for this case.– sashaaero
Nov 22 '18 at 7:05
I think
collections.Counter
is overkill for this case.– sashaaero
Nov 22 '18 at 7:05
@sashaaero you are right. I've read OP carelessly and only come with the idea of using
Counter
. I've made an edit, count()
is sufficient for such tasks :D– enamoria
Nov 22 '18 at 7:09
@sashaaero you are right. I've read OP carelessly and only come with the idea of using
Counter
. I've made an edit, count()
is sufficient for such tasks :D– enamoria
Nov 22 '18 at 7:09
add a comment |
Use count function.
a="."
b=[".", ".", "Hello world"]
if b.count(a) > 1:
print("more than 1 time")
add a comment |
Use count function.
a="."
b=[".", ".", "Hello world"]
if b.count(a) > 1:
print("more than 1 time")
add a comment |
Use count function.
a="."
b=[".", ".", "Hello world"]
if b.count(a) > 1:
print("more than 1 time")
Use count function.
a="."
b=[".", ".", "Hello world"]
if b.count(a) > 1:
print("more than 1 time")
answered Nov 22 '18 at 7:09


UsmanUsman
800614
800614
add a comment |
add a comment |
Using collections.Counter
:
from collections import Counter
a = "."
b=[".", ".", "Hello world"]
print(Counter(b)[a]) # 2
add a comment |
Using collections.Counter
:
from collections import Counter
a = "."
b=[".", ".", "Hello world"]
print(Counter(b)[a]) # 2
add a comment |
Using collections.Counter
:
from collections import Counter
a = "."
b=[".", ".", "Hello world"]
print(Counter(b)[a]) # 2
Using collections.Counter
:
from collections import Counter
a = "."
b=[".", ".", "Hello world"]
print(Counter(b)[a]) # 2
answered Nov 22 '18 at 7:17


b-fgb-fg
1,95911522
1,95911522
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%2f53425474%2fhow-to-check-if-a-certain-string-repeats-more-than-once-in-a-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
1
b.count(a) > 1
– sashaaero
Nov 22 '18 at 7:04