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:
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.
回答 4
text ="just trying out"
word_list =[]for i in range(0, len(text)):
word_list.append(text[i])
i+=1print(word_list)['j','u','s','t',' ','t','r','y','i','n','g',' ','o','u','t']
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]= tempprint("final count of arrival of each letter is : \n", dict(map(lambda x:(x, word_list.count(x)), word_list)))
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)))
The easiest option is to just use the spit() command. However, if you don’t want to use it or it dose not work for some bazaar reason, you can always use this method.
word = 'foo'
splitWord = []
for letter in word:
splitWord.append(letter)
print(splitWord) #prints ['f', 'o', 'o']