This question already has answers here:
How to create a list with the characters of a string? [duplicate]
(5 answers)
Closed 7 years ago.
If I have a string:
a = 'hello'
How do I convert it into a list?
a = ['h', 'e', 'l', 'l', 'o']
A string is an iterable, and a list can be constructed from an iterable. So all you need is
a = list(a)
print(a)
Output:
['h', 'e', 'l', 'l', 'o']
Tried to do it differently
Code:
map(None,"sart")#for python 2.x
#list(map(None,"sart")) for python 3.x
Output:
['s', 'a', 'r', 't']
Related
This question already has answers here:
Alphabet range in Python
(8 answers)
Closed 7 months ago.
I need to create random word/names with random.choice(alphabet) for many of my games in repl,
but it is a pain to type it out, and make uppercase versions, consonants/vowels only, etc.
Is there a built-in or importable way to get a pre-made one in python?
The string module provides several (English-centric) values:
>>> import string
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.digits
'0123456789'
You'll have to create your own vowel/consonant lists, as well as lists for other languages.
Given how short the list of vowels is, vowels and consonants aren't too painful:
>>> vowels = set("aeiou")
>>> set(string.ascii_lowercase).difference(vowels)
{'b', 'f', 'v', 'q', 's', 'w', 'y', 'l', 'g', 'j', 'z', 'c', 'h', 'p', 'x', 'd', 'm', 'n', 't', 'k', 'r'}
This question already has answers here:
How to create a list with the characters of a string? [duplicate]
(5 answers)
Closed 4 years ago.
I have a string in the file, for example, 'HELLO', and I need to convert the string in list by characters, 'H', 'E', 'L', 'L', 'O'. How to implement it?
Assuming your file is called myfile.txt:
In [1]: with open('myfile.txt', 'r') as myfile:
...: mystring = myfile.read()
...: print(list(mystring))
...:
['H', 'E', 'L', 'L', 'O']
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Flatten an irregular (arbitrarily nested) list of lists
(51 answers)
Closed 7 years ago.
For example:
lst = [('ABC','DEF'),('HIJ','KLM')]
To get:
>>> ['A','B','C','D','E','F','G','H','I','J','K','L','M']
You can use a nested list comprehension :
>>> [t for i in lst for word in i for t in word]
['A', 'B', 'C', 'D', 'E', 'F', 'H', 'I', 'J', 'K', 'L', 'M']
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I optimally concat a list of chars to a string?
I have a list of chars:
['h', 'e', 'l', 'l', 'o']
Is there a way to concatenate the elements of such list in a string 'hello' that does not require c-like 'for' loop? Thanks.
This is the usual way of concatenating strings in Python:
''.join(list_of_chars)
In fact, that's the recommended way - for readability and efficiency reasons. For example:
''.join(['h', 'e', 'l', 'l', 'o'])
=> 'hello'
str.join
>>> list('hello')
['h', 'e', 'l', 'l', 'o']
>>> ''.join(_)
'hello'
It's effectively:
from operator import add
reduce(add, ['h', 'e', 'l', 'l', 'o'])
But optimised for strings, it also only allows strings, otherwise it raises a TypeError
Yes. Use str.join
>>> chars = ['h', 'e', 'l', 'l', 'o']
>>> ''.join(chars)
'hello'
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
split a string in python
I want to change this:
str = 'blue\norange\nyellow\npink\nblack'
to this:
list = ['blue','orange', 'yellow', 'pink', 'black']
I tried some for and while loops and have not been able to do it. I just want the newline character to be removed while triggering to make the next element. I was told to use:
list(str)
which gives
['b', 'l', 'u', 'e', '\n', 'o', 'r', 'a', 'n', 'g', 'e', '\n', 'y', 'e', 'l', 'l', 'o', 'w', '\n', 'p', 'i', 'n', 'k', '\n', 'b', 'l', 'a', 'c', 'k']
After this I use .remove() but only one '\n' is removed and the code gets more complicated to spell the colors.
You want your_str.splitlines(), or possibly just your_str.split('\n')
Using a for loop -- for instructional use only:
out = []
buff = []
for c in your_str:
if c == '\n':
out.append(''.join(buff))
buff = []
else:
buff.append(c)
else:
if buff:
out.append(''.join(buff))
print out