Python comparing a list with set to find pangram [duplicate] - python

This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 3 years ago.
I'm trying to find if a string is a Pangram. My approach is to get the string to have unique letters by using the set method. Then using the string.ascii as the base alphabet. I find out after some testing that if I try to compare the 2 with the 'in' operator. Some of the letters get passed over and won't be removed from the alphabet list.
def is_pangram(sentence):
uniqueLetters = set(sentence.lower().replace(" ", ""))
alphabet = list(string.ascii_lowercase)
for letter in alphabet:
if letter in uniqueLetters:
alphabet.remove(letter)
if len(alphabet) <= 0:
return True
return False
print(is_pangram("qwertyuiopasdfghjklzxcvbnm"))
this example will compare 13 letters and the rest will not. Anyone can point me in the right direction? Am I misunderstanding something about set?

Perhaps the following does what you want:
import string
target = set('qwertyuiopasdfghjklzxcvbnm')
all((k in target for k in set(string.ascii_lowercase)))

Related

Checking for 2 different strings in the same list [duplicate]

This question already has answers here:
Check if something is (not) in a list in Python
(4 answers)
Closed 2 months ago.
What I mean by that is, that I want to take a list, let's say"
["i","am","you","pure","fact"],
then check whether this list contains 2 different strings, like "i" and "pure", and then if true, it runs something.
This is what I am looking for, but in PYTHON.
I don't know how to do this sort of check.
Try this out
elements: list = ["i","am","you","pure","fact"]
str1: str = "i"
str2: str = "pure"
if str1 in elements and str2 in elements:
# Execute code
...

Is there any way to count the number of times a string is present in another given string? [duplicate]

This question already has answers here:
String count with overlapping occurrences [closed]
(25 answers)
Closed 1 year ago.
I tried to create a program which returns the number of times a certain string occurs in the main string.
main_string="ABCDCDC"
find_string="CDC"
print(main_string.count(find_string))
Output=1
....
But there are 2 CDC. Is there any other ways to solve?
Try using regex:
print(len(re.findall(fr"(?={find_string})", main_string)))
Or try using this list comprehension:
x = len(find_string)
print(len([main_string[i:x + i] for i in range(len(main_string)) if main_string[i:x + i] == find_string]))
Both codes output:
2

Checking if a number is present in string [duplicate]

This question already has answers here:
Check if a string contains a number
(20 answers)
Closed 2 years ago.
I have a working function that takes an input as string and returns a string. However, I want my function to return an empty string ("") if the input contains any number in whatever position in the string.
For example :
>>> function("hello")
works fine
>>> function("hello1")
should return ""
The main thing you need for that is the method "isdigit()" that returns True if the character is a digit.
For example:
yourstring="hel4lo3"
for char in yourstring:
if char.isdigit():
print(char)
Will output 4 and 3.
I think it is a good exercise for you trying to do your code from that!

What is the faster way to count substrings in string using python [duplicate]

This question already has answers here:
String count with overlapping occurrences [closed]
(25 answers)
Closed 6 years ago.
I'm trying to find a fast algorithm for counting how many times a substring is found in a string using Python. I know there are some builtin functions for doing that but they do no serve my propose. For example, the word "ana" appears 2 times in "banana" but the string method count just returns 1.
The code I have so far is:
s = "banana"
sub = "ana"
count = 0
for i in range(4):
if s.startswith(sub):
count += 1
If some one knows a better way, please let me know.
possible this:
s = "banana"
sub = "ana"
count = len(s.split(sub))-1

Python - change string chars into a list unless recurring [duplicate]

This question already has answers here:
How do I remove duplicates from a list, while preserving order?
(31 answers)
Closed 8 years ago.
I want to print all the characters in a string as a list but for each character to be printed once even if recurring. So far I have:
symbolsx = []
for line in ''.join(word_lines):
for i in line:
symbolsx.append(i)
This prints every character, even if the character is repeated.
symbolsx = list(set(symbolsx))
First pass the list to set function to remove duplicates, then reverted that set back to list by passing it to list function.
How about:
symbolsx = []
for line in ''.join(word_lines):
for i in line:
if i not in symbolsx:
symbolsx.append(i)

Categories