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])
Related
This question already has answers here:
Pythonic way to print list items
(13 answers)
Closed 2 years ago.
I am new to python. Are there other ways to print lists for example
li1 = ['This is one sentence. ' 'This is another.']
print(...)
And then the output would be :
This is one sentence. This is another.
You can unpack the list
print(*l)
and choose the separator in between elements.
print(*l, sep=', ')
Or try something like print(''.join(li1))
This question already has answers here:
Find and replace string values in list [duplicate]
(6 answers)
Closed 4 years ago.
I have a list of strings like this:
string_list=["abcdefg","qwrbcdqqq","ophdkjhfkbcd","jdfkvndn"]
Notice that a word exists in some of them (here bcd) but the location of this word is not fixed and changes in each string. Now how do I remove this word from those strings if I know what the word is?
Edit: Target list is:
target_list=["aefg","qwrqqq","ophdkjhfk","jdfkvndn"]
You can iterate over the list and .replace('bcd', ''), e.g.:
In []: [s.replace('bcd', '') for s in string_list]
Out[]: ['aefg', 'qwrqqq', 'ophdkjhfk', 'jdfkvndn']
string_list = [temp.replace('bcd','') for temp in string_list]
Make a new list of those words which do not include the target substring.
target = "bcd"
new_list = [word for word in string_list if not target in word]
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]))
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:
How to get the position of a character in Python?
(11 answers)
Closed 7 years ago.
For example:
my_string = "hi how are you?/nIs everything ok?/nAre you happy?"
I need to make a list containing all the indexes of the newline - (/n).
How can i do it ?
You can use enumerate in a list comprehension to create a list of indices.
>>> [index for index, value in enumerate(my_string) if value == '\n']
[15, 33]
By the way, a new line character is '\n' not '/n' (note the slash)
import re
my_string = "hi how are you?/nIs everything ok?/nAre you happy?"
list = [m.start() for m in re.finditer('/n', my_string)]