How can I remove multiple occurrences from a string in python? [duplicate] - python

This question already has answers here:
Removing duplicate characters from a string
(15 answers)
Closed 3 years ago.
I have a string like 'AABA'. I want to remove multiple occurances by removing others. The result should be 'AB'.
Sample Input: AABA
Sample Output: AB

If the order doesn't matter, use a set.
word = "AABA"
new_word = "".join(set(word))
If the order DOES matter, use an Ordered Dictionary (from collections library).
from collections import OrderedDict
word = "AABA"
new_word = "".join(OrderedDict.fromkeys(word))
EDIT: Consult the link posted in the comments above - it gives the same advice, but explains it better.

Related

How to get a string after and before a specific substring in Python? [duplicate]

This question already has answers here:
Split a string by a delimiter in python
(5 answers)
Closed 2 years ago.
How can I get a string after and before a specific substring?
For example, I want to get the strings before and after : in
my_string="str1:str2"
(which in this case it is: str1 and str2).
Depending on your use case you may want different things, but this might work best for you:
lst = my_string.split(":")
Then, lst will be: ['str1', 'str2']
You can also find the index of the substring by doing something like:
substring = ":"
index = my_string.find(":")
Then split the string on that index:
first_string = my_string[:index]
second_string = my_string[index+len(substring):]

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)

Formatting The Output of common words [duplicate]

This question already has answers here:
Printing Lists as Tabular Data
(20 answers)
Closed 5 years ago.
import re
from collections import Counter
words = re.findall(r'\w+', open('test01_cc_sharealike.txt').read().lower())
count = Counter(words).most_common(10)
print(count)
How can I change the code so it will format into like this:
Word number
word number
instead of a list
I want the format to be: the word first then 4 whitespace and the number of the word it appears on the text and so on
Just use a for loop, so instead of print(count), you could use:
for p in count:
print(p[0]+" "+str(p[1]))
However, for formatting purposes, you would probably prefer to align the numbers, so you should use:
indent=1+max([len(p[0]) for p in count])
for p in count:
print(p[0].rjust(indent)+str(p[1]))

Splitting two concatenated terms in python [duplicate]

This question already has answers here:
Split a string at uppercase letters
(22 answers)
Closed 6 years ago.
In general I have a string say
temp = "ProgramFields"
Now I want to split strings like these into two terms(I can identify tow strings based on uppercase character)
term1 = "Program"
term2 = "Field"
How to achieve this in python?
I tried regular expression and splitting terms but nothing gave me the result that I expected
Python code -
re.split("[A-Z][a-z]*","ProgramField")
Any suggestions?
You have to include groups:
re.split('([A-Z][a-z]*)', 'ProgramField)

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

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

Categories