Is there a function in python to split a word into a list of single letters? e.g:
s="Word to Split"
to get
wordlist=['W','o','r','d','','t','o' ....]
Is there a function in python to split a word into a list of single letters? e.g:
s="Word to Split"
to get
wordlist=['W','o','r','d','','t','o' ....]
>>> list("Word to Split")
['W', 'o', 'r', 'd', ' ', 't', 'o', ' ', 'S', 'p', 'l', 'i', 't']
The easiest way is probably just to use list()
, but there is at least one other option as well:
s = "Word to Split"
wordlist = list(s) # option 1,
wordlist = [ch for ch in s] # option 2, list comprehension.
They should both give you what you need:
['W','o','r','d',' ','t','o',' ','S','p','l','i','t']
As stated, the first is likely the most preferable for your example but there are use cases that may make the latter quite handy for more complex stuff, such as if you want to apply some arbitrary function to the items, such as with:
[doSomethingWith(ch) for ch in s]
The list function will do this
>>> list('foo')
['f', 'o', 'o']
Abuse of the rules, same result: (x for x in 'Word to split')
Actually an iterator, not a list. But it's likely you won't really care.
text = "just trying out"
word_list = []
for i in range(0, len(text)):
word_list.append(text[i])
i+=1
print(word_list)
['j', 'u', 's', 't', ' ', 't', 'r', 'y', 'i', 'n', 'g', ' ', 'o', 'u', 't']
def count(): list = 'oixfjhibokxnjfklmhjpxesriktglanwekgfvnk'
word_list = []
# dict = {}
for i in range(len(list)):
word_list.append(list[i])
# word_list1 = sorted(word_list)
for i in range(len(word_list) - 1, 0, -1):
for j in range(i):
if word_list[j] > word_list[j + 1]:
temp = word_list[j]
word_list[j] = word_list[j + 1]
word_list[j + 1] = temp
print("final count of arrival of each letter is : \n", dict(map(lambda x: (x, word_list.count(x)), word_list)))