Count how many time a string appears in a longer string [duplicate] - python

This question already has answers here:
String count with overlapping occurrences [closed]
(25 answers)
Closed 7 years ago.
So I have a little problem,
I want to count how many times a string : "aa" is in my longer string "aaatattgg" its looks like a dna sequence.
Here for exemple I expect 2 (overlap is allow)
There is the .count method but overlap is not allowed
PS: excuse my english , I'm french

Through re module. Put your regex inside positive lookarounds in-order to do overlapping match.
>>> import re
>>> s = "aaatattgg"
>>> re.findall(r'(?=(aa))', s)
['aa', 'aa']
>>> len(re.findall(r'(?=(aa))', s))
2

Related

Python regex to match string with specified count of given phrase [duplicate]

This question already has answers here:
Count the number of occurrences of a character in a string
(26 answers)
Closed 25 days ago.
Let's say I have a string:
my_sentence = "I like not only apples, but also bananas."
and I would like to use python regex to match string if for example a letter occurs at least 3 times in it (no matter on what place). How will pattern look like in this case?
You could use
import re
matches = re.findall(r'a', my_sentence) # ['a', 'a', 'a', 'a', 'a']
And then check for the number of occurrences
len(matches) > 3 # True
Try this pattern:
^.*(a.*){3}$
You can easily change it to any number of a's or any other character.

Regular expression to find largest repeating pattern? [duplicate]

This question already has answers here:
Longest consecutive substring of certain character type in python
(2 answers)
Closed 2 years ago.
How can I use regular expressions to find the largest repeating pattern?
For example, in the string "CATchickenchickenCATCATCATCATchickenchickenCATCATchicken"
I need a way to get this string: "CATCATCATCAT" since it is the largest repeating chunk of my substring "CAT"
How can I do this?
Thanks :)
import re
string = "CATchickenchickenCATCATCATCATchickenchickenCATCATchicken"
pattern = "((CAT)+)"
print(max(re.findall(pattern, string), key=lambda tpl: len(tpl[0]))[0])
Output:
CATCATCATCAT
>>>

how do you check if a string has more than one specific character in python. Example The string, 'mood' would clearly have two 'o' characters [duplicate]

This question already has answers here:
Count the number of occurrences of a character in a string
(26 answers)
Closed 3 years ago.
how do you check if a string has more than one specific character in python. Example The string, 'mood' would clearly have two 'o' characters
You can use the str.count method:
>>> 'mood'.count('o') > 1
True
>>>

How to change the characters order in a string in python 3.X? [duplicate]

This question already has answers here:
Finding all possible permutations of a given string in python
(27 answers)
Best way to randomize a list of strings in Python
(6 answers)
Closed 4 years ago.
let's say for example that I got 100 random words (not even real words just words)...
like "ABCD" and I want to make a program that takes a word like the one I mentioned and prints you all the options of this word in random order.
for example the word "ABC" will print: "ABC", "BAC", CAB", "BCA", "CBA".
I could do it manually but if I have 100 words I can't...
so how do I write a code that does it in python?
You can do this by using itertools:
import itertools
import random
words = ['word1', 'word2', 'word3']
for word in words:
permutations_list = [''.join(x) for x in itertools.permutations(word)]
random.shuffle(permutations_list)
print(permutations_list)

Get the last 4 characters of a string [duplicate]

This question already has answers here:
How do I get a substring of a string in Python? [duplicate]
(16 answers)
Closed 7 years ago.
I have the following string: "aaaabbbb"
How can I get the last four characters and store them in a string using Python?
Like this:
>>> mystr = "abcdefghijkl"
>>> mystr[-4:]
'ijkl'
This slices the string's last 4 characters. The -4 starts the range from the string's end. A modified expression with [:-4] removes the same 4 characters from the end of the string:
>>> mystr[:-4]
'abcdefgh'
For more information on slicing see this Stack Overflow answer.
str = "aaaaabbbb"
newstr = str[-4:]
See : http://codepad.org/S3zjnKoD

Categories