This question already has answers here:
Check if multiple strings exist in another string
(17 answers)
Closed 4 years ago.
I want to when I input a string, if words in string is contain in a list of words, delete words in the strings
Or, you could do the following:
raw_string = input("Enter String:")
useless_list = ["Birds", "Cat"]
print(' '.join([i for i in raw_string.split() if i not in useless_list]))
Related
This question already has answers here:
How to convert comma-delimited string to list in Python?
(7 answers)
Closed 2 years ago.
I am trying to convert a long string to a list of strings for each item separated with comma.
list = ['05-19.scl, 05-22.scl, 05-24.scl, 06-41.scl']
to :
desired_list= ['05-19.scl', '05-22.scl', '05-24.scl', '06-41.scl']
>>> listObj = ['05-19.scl, 05-22.scl, 05-24.scl, 06-41.scl']
>>> a = [x.split(',') for x in listObj][0]
>>> a
['05-19.scl', ' 05-22.scl', ' 05-24.scl', ' 06-41.scl']
This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 3 years ago.
I have a string that consists of a list which made of different product code.
"['B00GHX7H0A', 'B00FRERO7G', 'B00R68QXCS', 'B000Z65AZE', 'B07GFHJRMX', 'B074KGBGL7', 'B00R68QXJG', 'B00025WYZC', 'B07H3W9BM5', 'B00KOBT82G', 'B072N2M1P6', 'B071G8FG2N', 'B00FASVFI8', 'B00GHXE4N8', 'B00EPG2QJI', 'B01MQ4MEFE', 'B01M8ML0SY', 'B074KHCPLH', 'B004XQWY4W', 'B00FASV6UU', 'B01M31HJBJ', 'B00KC8TU7O', 'B00B9TU5T2', 'B00K75EZ04', 'B000Q2Y0FI', 'B00FEGOCCM', 'B00EPFXFBW', 'B00H6SQY3Q', 'B00HZAOWUC', 'B07GFJF1DN', 'B001WBS68E', 'B074KJZCPH']"
How can I extract each product code out of this string?
There is nother option than to replace the unnecessary characters and then splitting it by the commas:
myList = "['123','231','312','123']"
myList = myList.replace("[","").replace("]","").replace("'", "").split(",")
print(myList)
# output: ["123","231","312","123"]
But with this you would get problems if the items inside the list could potentially contain commas.
This question already has answers here:
Convert a list of characters into a string [duplicate]
(9 answers)
Closed 3 years ago.
I have a nested list:
encode= [['t','h','i','s'],['i','s'],['a','w','s','o','m','e']]
and I want to convert the nested list above into a string:
"this is awesome"
the join in the list comprehension joins the letters into words, then the join outside the list joins the words into a sentence
' '.join([''.join(x) for x in encode])
Simply:
words = [''.join(word) for word in encode]
sentence = ' '.join(words)
l =" ".join(["".join(i) for i in encode])
This question already has answers here:
Split string every nth character?
(19 answers)
Closed 4 years ago.
how to split a string into words of 2 letters. Like given string is "HelloThere" now i want to make it ["He","ll","oT","he","re"]. Please help to code that in python.
yourList = []
yourString = "HelloThere"
while yourString:
yourList.append(yourString[:2])
yourString = yourString[2:]
If you print yourList, you will get the result.
This question already has answers here:
Find index of last occurrence of a substring in a string
(12 answers)
Closed 8 years ago.
How would I find the last occurrence of a character in a string?
string = "abcd}def}"
string = string.find('}',last) # Want something like this
You can use rfind:
>>> "abcd}def}".rfind('}')
8