Counting Occurences of a String within elements of a list?












1














I'm trying to count the amount of times "a class" occurs within each "h2 class", so I split the parsed texted by "h2 class" but am having struggles with the second part, this is where I'm at



#splitting parsed text by header
parsed.split("h2 class")

#creating the list for the a value count to be stored
aValCount =

#counting amount of items per header
for i in range (len(parsed)):
aValCount = aValCount + ((parsed[i]).count("a class"))


the error I'm getting is




TypeError: can only concatenate list (not "int") to list




, but I can't figure out how to this without getting some sort of error



Edited: Thought I should add, I want it to be a list of the counts from the strings, so the count from element one in parsed, should be element 1 in aValCount










share|improve this question




















  • 1




    Initialize aValCount=0
    – Julian Peller
    Nov 21 at 1:32










  • Thought I should add, I want it to be a list of the counts from the strings, so the count from element one in parsed, should be element 1 in aValCount
    – SoloTriesToLearn
    Nov 21 at 1:34






  • 1




    I see, you need to use append() as the actual answer suggests then!
    – Julian Peller
    Nov 21 at 1:38


















1














I'm trying to count the amount of times "a class" occurs within each "h2 class", so I split the parsed texted by "h2 class" but am having struggles with the second part, this is where I'm at



#splitting parsed text by header
parsed.split("h2 class")

#creating the list for the a value count to be stored
aValCount =

#counting amount of items per header
for i in range (len(parsed)):
aValCount = aValCount + ((parsed[i]).count("a class"))


the error I'm getting is




TypeError: can only concatenate list (not "int") to list




, but I can't figure out how to this without getting some sort of error



Edited: Thought I should add, I want it to be a list of the counts from the strings, so the count from element one in parsed, should be element 1 in aValCount










share|improve this question




















  • 1




    Initialize aValCount=0
    – Julian Peller
    Nov 21 at 1:32










  • Thought I should add, I want it to be a list of the counts from the strings, so the count from element one in parsed, should be element 1 in aValCount
    – SoloTriesToLearn
    Nov 21 at 1:34






  • 1




    I see, you need to use append() as the actual answer suggests then!
    – Julian Peller
    Nov 21 at 1:38
















1












1








1







I'm trying to count the amount of times "a class" occurs within each "h2 class", so I split the parsed texted by "h2 class" but am having struggles with the second part, this is where I'm at



#splitting parsed text by header
parsed.split("h2 class")

#creating the list for the a value count to be stored
aValCount =

#counting amount of items per header
for i in range (len(parsed)):
aValCount = aValCount + ((parsed[i]).count("a class"))


the error I'm getting is




TypeError: can only concatenate list (not "int") to list




, but I can't figure out how to this without getting some sort of error



Edited: Thought I should add, I want it to be a list of the counts from the strings, so the count from element one in parsed, should be element 1 in aValCount










share|improve this question















I'm trying to count the amount of times "a class" occurs within each "h2 class", so I split the parsed texted by "h2 class" but am having struggles with the second part, this is where I'm at



#splitting parsed text by header
parsed.split("h2 class")

#creating the list for the a value count to be stored
aValCount =

#counting amount of items per header
for i in range (len(parsed)):
aValCount = aValCount + ((parsed[i]).count("a class"))


the error I'm getting is




TypeError: can only concatenate list (not "int") to list




, but I can't figure out how to this without getting some sort of error



Edited: Thought I should add, I want it to be a list of the counts from the strings, so the count from element one in parsed, should be element 1 in aValCount







python for-loop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 6:42









Aqueous Carlos

289213




289213










asked Nov 21 at 1:31









SoloTriesToLearn

466




466








  • 1




    Initialize aValCount=0
    – Julian Peller
    Nov 21 at 1:32










  • Thought I should add, I want it to be a list of the counts from the strings, so the count from element one in parsed, should be element 1 in aValCount
    – SoloTriesToLearn
    Nov 21 at 1:34






  • 1




    I see, you need to use append() as the actual answer suggests then!
    – Julian Peller
    Nov 21 at 1:38
















  • 1




    Initialize aValCount=0
    – Julian Peller
    Nov 21 at 1:32










  • Thought I should add, I want it to be a list of the counts from the strings, so the count from element one in parsed, should be element 1 in aValCount
    – SoloTriesToLearn
    Nov 21 at 1:34






  • 1




    I see, you need to use append() as the actual answer suggests then!
    – Julian Peller
    Nov 21 at 1:38










1




1




Initialize aValCount=0
– Julian Peller
Nov 21 at 1:32




Initialize aValCount=0
– Julian Peller
Nov 21 at 1:32












Thought I should add, I want it to be a list of the counts from the strings, so the count from element one in parsed, should be element 1 in aValCount
– SoloTriesToLearn
Nov 21 at 1:34




Thought I should add, I want it to be a list of the counts from the strings, so the count from element one in parsed, should be element 1 in aValCount
– SoloTriesToLearn
Nov 21 at 1:34




1




1




I see, you need to use append() as the actual answer suggests then!
– Julian Peller
Nov 21 at 1:38






I see, you need to use append() as the actual answer suggests then!
– Julian Peller
Nov 21 at 1:38














1 Answer
1






active

oldest

votes


















1














The issue is that aValCount is an array and ((parsed[i]).count("a class")) is an int.



What you want is to add the count to aValCount so you need to pass another array.



aValCount = aValCount + [((parsed[i]).count("a class"))]



If you add [...] that should do it.
Or you can also do



aValCount.append(((parsed[i]).count("a class"))])



Hope that help.



results = parsed.split("h2 class")
aValCountList =

for i in range (len(results)):
aValCountList.append((results[i]).count("a class"))





share|improve this answer























  • When I try to print this, I get a significant amount of zeros, would that be a zero for every occurrence? I'm trying to have it to be a list of the counts from the strings, so the count from element one in parsed, should be element 1 in aValCount
    – SoloTriesToLearn
    Nov 21 at 1:40










  • You need to assign the value of the split to another variable.
    – Antoine
    Nov 21 at 1:42











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404080%2fcounting-occurences-of-a-string-within-elements-of-a-list%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









1














The issue is that aValCount is an array and ((parsed[i]).count("a class")) is an int.



What you want is to add the count to aValCount so you need to pass another array.



aValCount = aValCount + [((parsed[i]).count("a class"))]



If you add [...] that should do it.
Or you can also do



aValCount.append(((parsed[i]).count("a class"))])



Hope that help.



results = parsed.split("h2 class")
aValCountList =

for i in range (len(results)):
aValCountList.append((results[i]).count("a class"))





share|improve this answer























  • When I try to print this, I get a significant amount of zeros, would that be a zero for every occurrence? I'm trying to have it to be a list of the counts from the strings, so the count from element one in parsed, should be element 1 in aValCount
    – SoloTriesToLearn
    Nov 21 at 1:40










  • You need to assign the value of the split to another variable.
    – Antoine
    Nov 21 at 1:42
















1














The issue is that aValCount is an array and ((parsed[i]).count("a class")) is an int.



What you want is to add the count to aValCount so you need to pass another array.



aValCount = aValCount + [((parsed[i]).count("a class"))]



If you add [...] that should do it.
Or you can also do



aValCount.append(((parsed[i]).count("a class"))])



Hope that help.



results = parsed.split("h2 class")
aValCountList =

for i in range (len(results)):
aValCountList.append((results[i]).count("a class"))





share|improve this answer























  • When I try to print this, I get a significant amount of zeros, would that be a zero for every occurrence? I'm trying to have it to be a list of the counts from the strings, so the count from element one in parsed, should be element 1 in aValCount
    – SoloTriesToLearn
    Nov 21 at 1:40










  • You need to assign the value of the split to another variable.
    – Antoine
    Nov 21 at 1:42














1












1








1






The issue is that aValCount is an array and ((parsed[i]).count("a class")) is an int.



What you want is to add the count to aValCount so you need to pass another array.



aValCount = aValCount + [((parsed[i]).count("a class"))]



If you add [...] that should do it.
Or you can also do



aValCount.append(((parsed[i]).count("a class"))])



Hope that help.



results = parsed.split("h2 class")
aValCountList =

for i in range (len(results)):
aValCountList.append((results[i]).count("a class"))





share|improve this answer














The issue is that aValCount is an array and ((parsed[i]).count("a class")) is an int.



What you want is to add the count to aValCount so you need to pass another array.



aValCount = aValCount + [((parsed[i]).count("a class"))]



If you add [...] that should do it.
Or you can also do



aValCount.append(((parsed[i]).count("a class"))])



Hope that help.



results = parsed.split("h2 class")
aValCountList =

for i in range (len(results)):
aValCountList.append((results[i]).count("a class"))






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 at 1:44

























answered Nov 21 at 1:37









Antoine

793624




793624












  • When I try to print this, I get a significant amount of zeros, would that be a zero for every occurrence? I'm trying to have it to be a list of the counts from the strings, so the count from element one in parsed, should be element 1 in aValCount
    – SoloTriesToLearn
    Nov 21 at 1:40










  • You need to assign the value of the split to another variable.
    – Antoine
    Nov 21 at 1:42


















  • When I try to print this, I get a significant amount of zeros, would that be a zero for every occurrence? I'm trying to have it to be a list of the counts from the strings, so the count from element one in parsed, should be element 1 in aValCount
    – SoloTriesToLearn
    Nov 21 at 1:40










  • You need to assign the value of the split to another variable.
    – Antoine
    Nov 21 at 1:42
















When I try to print this, I get a significant amount of zeros, would that be a zero for every occurrence? I'm trying to have it to be a list of the counts from the strings, so the count from element one in parsed, should be element 1 in aValCount
– SoloTriesToLearn
Nov 21 at 1:40




When I try to print this, I get a significant amount of zeros, would that be a zero for every occurrence? I'm trying to have it to be a list of the counts from the strings, so the count from element one in parsed, should be element 1 in aValCount
– SoloTriesToLearn
Nov 21 at 1:40












You need to assign the value of the split to another variable.
– Antoine
Nov 21 at 1:42




You need to assign the value of the split to another variable.
– Antoine
Nov 21 at 1:42


















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404080%2fcounting-occurences-of-a-string-within-elements-of-a-list%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python