Navigation X
ALERT
Click here to register with a few steps and explore all our cool stuff we have to offer!

cracked.io | Best Forum Around | Free Premium Accounts




 2959

[PYTHON] Scan and count for most repeated string

by Nalon - 23 February, 2020 - 08:32 PM
This post is by a banned member (Nalon) - Unhide
Nalon  
Infinity
131
Posts
27
Threads
5 Years of service
#1
Hii, so I am trying to make a bit of code where it will scan a string and count the amount of repeated words. I believe the pseudo code would be something like this?

readme = "Hello hello hello hello I am cool"
repeats = readme.count(repeatedwords)

So it like makes a int variable called repeats, then reads my string and detects a repeated word and counts it. So repeats would = 4 because it says "Hello" four times..

I hope this makes sense kek
DISCORD SERVER : CLICK
This post is by a banned member (whatdiduk) - Unhide
This post is by a banned member (Nalon) - Unhide
Nalon  
Infinity
131
Posts
27
Threads
5 Years of service
#3
Code:
from collections import Counter import re reg = re.compile('\S{4,}') s = "hello this is hello this is baby baby baby baby hello" c = Counter(ma.group() for ma in reg.finditer(s)) print c
 
result
Counter({'baby': 4, 'hello': 3, 'this': 2})

This is printing it? I need to have one answer and I need it in a int variable so the result would be
count = 4

I have literally scoured google for the answer and couldn't find shit
DISCORD SERVER : CLICK
This post is by a banned member (TheRiotDrummer) - Unhide
This post is by a banned member (Nalon) - Unhide
Nalon  
Infinity
131
Posts
27
Threads
5 Years of service
#5
(23 February, 2020 - 08:48 PM)TheRiotDrummer Wrote: Show More
Code:
readMe = "test test gay"
readmeList = readMe.split()
words = set(readmeList)
countList = []
for word in words:
    countList.append((word,":",readMe.count(word)))
print(countList)
This is a really awkward way to do it but im not at my main PC atm but this works, the variable is countList

Oooh ok ill give that a shot. Thank you so much sexy boy
DISCORD SERVER : CLICK

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
or
Sign in
Already have an account? Sign in here.


Forum Jump:


Users browsing this thread: 1 Guest(s)