Given two strings suppose stringA and stringB (len(stringA)>len(stringB)), how do i remove all characters from stringA which are present in stringB? Assume that all characters in stringB are present in stringA
Here is what i tried:
a=input()
b=input()
for i in range(len(b)):
if b[i] not in a:
a.remove(b[i])
I expected it to remove characters from A, but resulting in an error, I am a beginner in python and i havent a clue which other method or approach i should use here
Use a set of B for efficiency and loop over all characters of A with a list comprehension to filter and join them:
s = set(B)
out = ''.join([c for c in A if not c in s])
stringA = input()
stringB = input()
for char in stringB:
stringA = stringA.replace(char, "")
print(stringA)
s1 = input()
s2 = input()
new_s1 = ""
new_s2 = ""
for char in s1:
if char not in s2:
new_s1 += char
for char in s2:
if char not in s1:
new_s2 += char
print(new_s1)
print(new_s2)
str object has no method called remove. You have to use sth like replace or use set for efficiency:
out = "".join(set(a).difference(set(b)))
Note that this will not necessarily maintain order of the characters of A.
Related
how do I do this with sliced notation?
ex. "Hello" to "HHeelloo"
You can iterate through the input string, then use the multiplication operator to duplicate the string and then concatenate again using join():
"".join(2 * s for s in "Hello")
You could simply loop each letter in your string and add it twice to a new string. For example
word = "Hello"
doubledWord = ""
for letter in word:
doubledWord += letter * 2
print(doubledWord)
Output
HHeelllloo
The term slicing in programming usually refers to obtaining a substring, sub-tuple, or sublist from a string, tuple, or list respectively. You cannot slice a string to double every character. Though you can use sliced notation- method 6. Here are some methods to get the desirable output.
Method 1:
input = Hello
output=""
for i in input:
output = output + i*2
print(output)
Method 2:
"".join(2 * i for i in "Hello")
Method 3:
s='Hello'
from itertools import chain,izip
''.join(chain(*izip(s,s)))
Method 4:
s.translate({ord(x):2*x for x in set(s)})
Method 5:
s = "Hello"
import re
re.sub('(.)', r'\1' * 2, s)
Method 6 (using sliced notation):
s = "Hello"
s1 = 2 * s
s2 = ""
for i in range(len(s)):
s2 += s1[i::len(s)]
print(s2)
All would result in:
HHeelllloo
You can use join function and for loop to an empty string. The letters will be double of all letters.
s="Hello"
"".join([x*2 for x in s])
Word = 'Hello'
number_of_time = 2
''.join([char*number_of_time for char in Word])
Output:
'HHeelllloo'
you can achieve it by iterating the whole string and at each iteration multiply the string by 2
str1 = 'Hello'
str2= ''
for i in range(len(str1)):
str2 += str1[i]*2
print(str2)
Using sliced notation in a non-trivial manner:
s = "Sajad"
s2 = 2 * "Sajad"
ss = ""
for i in range(len(s)):
ss += s2[i::len(s)]
print(ss)
I'm trying to write a function that returns a list of the three most frequent words in a string but I get the following error when I try to remove unwanted punctuation from the string "list.remove(x): x not in list"
As an example, "//wont won't won't " should return ["won't", "wont"] or "a a a b c c d d d d e e e e e" should return ["e", "d", "a"] Any ideas?
def top_3_words(text):
new_list = []
new_text = text.lower().split()
for word in new_text:
for char in word:
if char.isalpha():
pass
else:
new_text.remove(char)
Count = Counter(new_text)
most_occur = Count.most_common(3)
for l in most_occur:
new_list.append(l[0])
return new_list
There are a couple of problems. 1. You need to replace a character in a string with '' if you want to remove it. 2. Your Counter is still pointed to your new_text list and not your stripped words. Finally, calling isalpha on each character will get rid of the apostrophe in "won't", which I assume you don't want. Here is a different approach. You can chain as many .replace statements as you need to get rid of all unwanted punctuation.
from collections import Counter
def top_3_words(text):
new_list = []
new_text = text.lower().split()
stripped_words = []
for word in new_text:
word = word.replace('/','')
stripped_words.append(word)
Count = Counter(stripped_words)
most_occur = Count.most_common(3)
for l in most_occur:
new_list.append(l[0])
return new_list
print(top_3_words("//wont won't won't " ))
print(top_3_words("a a a b c c d d d d e e e e e"))
#output:
["won't", 'wont']
['e', 'd', 'a']
More generally, you can create a list of unwanted punctuation and iterate through it as follows:
unwanted = ['.','/',';',':',',']
stripped_words = []
for word in new_text:
for punc in unwanted:
word = word.replace(punc, '')
stripped_words.append(word)
...
print(top_3_words("//wo;nt wo:n't won't... " ))
#output: ["won't", 'wont']
Be careful because you are trying to remove char from new_text but char is a character from the string word.
If you want to remove the unwanted character you can do the following:
Instead of using
new_text.remove(char)
You can use
word = word.replace(char, '')
What this code does is replacing you unwanted character for an empty string.
Another problem you have is that the The ' will return False in char.isalpha() so "won't" will be converted to "wont" and from
"//wont won't won't " you will only get ["wont"]
If what you want is remove the word with an unwanted character, you have to use:
new_text.remove(word)
break
"ABCDE" has no empty character. But when I type
"" in "ABCDE"
Python interpreter returns True.
Why?
is there any empty character in "ABCDE"? I don't think so.
And I also found when I use these code:
target = ''
src = 'ABCDE'
src.find(target)
it returns 0 instead of -1
Why is this?
For people with background in languages where string objects represented as arrays of characters it may be surprising, but if we try to follow such approach like
string = 'ABCDE'
characters_list = list(string)
then
'' in characters_list
will be False statement.
Empty string probably came from mathematics, where it is a neutral element for binary operation of string concatenation, i. e. for every string a
a + empty_string == empty_string + a == a
where + is a string concatenation symbol. Then "substringing" can be defined as follows:
for every strings a, b we say a is substring of b iff exists strings c, d such that
b == c + a + d
Let's denote a is substring of b as a in b.
With these definitions of empty string and substringing relation can be proved lemma
empty_string is a substring of any string a:
a == (definition of empty_string) == empty_string + a ==
== (definition of empty_string) == empty_string + empty_string + a
then if we define c = empty_string and d = a:
a == c + empty_string + d
and by definition empty_string in a.
Here's a qualitative way to think about it. Consider the following:
>>> "foo"[0:0]
''
Doing a zero-width slice of a string returns ''. So, if you can get '' out of a string, it must be in the string and therefore '' in "foo" must be true.
Given some string say 'aabaaab', how would I go about finding the largest substring of a. So it should return 'aaa'. Any help would be greatly appreciated.
def sub_string(s):
best_run = 0
current_run = 0
for char in s:
if char == 'a'
current_run += 1
else:
current_letter = char
return(best_run)
I have something like the one above. Not sure where I can fix it up.
not the most efficient, but a straightforward solution:
word = "aasfgaaassaasdsddaaaaaafff"
substr_count = 0
substr_counts = []
character = "f"
for i, letter in enumerate(word):
if (letter == character):
substr_count += 1
else:
substr_counts.append(substr_count)
substr_count = 0
if (i == len(word) - 1):
substr_counts.append(substr_count)
print(max(substr_counts))
If you want a short method using standard python tools (and avoid writing loops to reconstruct the string as you iterate), you can use regex to split the string by any non-a characters than get the max() according to len:
import re
test_string = 'aabaaab'
split_string_list = re.split( '[^a]', test_string )
longest_string_subset = max( split_string_list, key=len )
print( longest_string_subset )
The re library is for regex, the '[^a]' is a regex statement for any non-a character. Basically, the 'aabaaab' is being split into a list according to any matches on the regex statement, so that it becomes [ 'aa' 'aaa' '' ]. Then, the max() statement looks for the longest string based on len (aka length).
You can read more about functions like re.split() in the docs: https://docs.python.org/2/library/re.html
I need to know if there is a function that detects the lowercase letters in a string. Say I started writing this program:
s = input('Type a word')
Would there be a function that lets me detect a lowercase letter within the string s? Possibly ending up with assigning those letters to a different variable, or just printing the lowercase letters or number of lowercase letters.
While those would be what I would like to do with it I'm most interested in how to detect the presence of lowercase letters. The simplest methods would be welcome.
To check if a character is lower case, use the islower method of str. This simple imperative program prints all the lowercase letters in your string:
for c in s:
if c.islower():
print c
Note that in Python 3 you should use print(c) instead of print c.
Possibly ending up with assigning those letters to a different variable.
To do this I would suggest using a list comprehension, though you may not have covered this yet in your course:
>>> s = 'abCd'
>>> lowercase_letters = [c for c in s if c.islower()]
>>> print lowercase_letters
['a', 'b', 'd']
Or to get a string you can use ''.join with a generator:
>>> lowercase_letters = ''.join(c for c in s if c.islower())
>>> print lowercase_letters
'abd'
There are 2 different ways you can look for lowercase characters:
Use str.islower() to find lowercase characters. Combined with a list comprehension, you can gather all lowercase letters:
lowercase = [c for c in s if c.islower()]
You could use a regular expression:
import re
lc = re.compile('[a-z]+')
lowercase = lc.findall(s)
The first method returns a list of individual characters, the second returns a list of character groups:
>>> import re
>>> lc = re.compile('[a-z]+')
>>> lc.findall('AbcDeif')
['bc', 'eif']
There are many methods to this, here are some of them:
Using the predefined str method islower():
>>> c = 'a'
>>> c.islower()
True
Using the ord() function to check whether the ASCII code of the letter is in the range of the ASCII codes of the lowercase characters:
>>> c = 'a'
>>> ord(c) in range(97, 123)
True
Checking if the letter is equal to it's lowercase form:
>>> c = 'a'
>>> c.lower() == c
True
Checking if the letter is in the list ascii_lowercase of the string module:
>>> from string import ascii_lowercase
>>> c = 'a'
>>> c in ascii_lowercase
True
But that may not be all, you can find your own ways if you don't like these ones: D.
Finally, let's start detecting:
d = str(input('enter a string : '))
lowers = [c for c in d if c.islower()]
# here i used islower() because it's the shortest and most-reliable
# one (being a predefined function), using this list comprehension
# is (probably) the most efficient way of doing this
You should use raw_input to take a string input. then use islower method of str object.
s = raw_input('Type a word')
l = []
for c in s.strip():
if c.islower():
print c
l.append(c)
print 'Total number of lowercase letters: %d'%(len(l) + 1)
Just do -
dir(s)
and you will find islower and other attributes of str
import re
s = raw_input('Type a word: ')
slower=''.join(re.findall(r'[a-z]',s))
supper=''.join(re.findall(r'[A-Z]',s))
print slower, supper
Prints:
Type a word: A Title of a Book
itleofaook ATB
Or you can use a list comprehension / generator expression:
slower=''.join(c for c in s if c.islower())
supper=''.join(c for c in s if c.isupper())
print slower, supper
Prints:
Type a word: A Title of a Book
itleofaook ATB
If you don't want to use the libraries and want simple answer then the code is given below:
def swap_alpha(test_string):
new_string = ""
for i in test_string:
if i.upper() in test_string:
new_string += i.lower()
elif i.lower():
new_string += i.upper()
else:
return "invalid "
return new_string
user_string = input("enter the string:")
updated = swap_alpha(user_string)
print(updated)