파이썬에 단어를 단일 문자 목록으로 분할하는 기능이 있습니까? 예 :
s="Word to Split"
얻기 위해
wordlist=['W','o','r','d','','t','o' ....]
답변
>>> list("Word to Split")
['W', 'o', 'r', 'd', ' ', 't', 'o', ' ', 'S', 'p', 'l', 'i', 't']
답변
가장 쉬운 방법은 아마도를 사용하는 것입니다 list()
. 그러나 적어도 하나의 다른 옵션도 있습니다.
s = "Word to Split"
wordlist = list(s) # option 1,
wordlist = [ch for ch in s] # option 2, list comprehension.
그들은해야 모두 당신이 필요 당신을 줄 :
['W','o','r','d',' ','t','o',' ','S','p','l','i','t']
앞서 언급했듯이 첫 번째가 예제에 가장 바람직 할 수 있지만 다음과 같이 항목에 임의의 함수를 적용하려는 경우와 같이 더 복잡한 작업에 후자를 매우 편리하게 만들 수있는 사용 사례가 있습니다.
[doSomethingWith(ch) for ch in s]
답변
목록 기능이이를 수행합니다.
>>> list('foo')
['f', 'o', 'o']
답변
규칙의 남용, 동일한 결과 : ( ‘분할 단어’에서 x의 경우 x)
실제로는 목록이 아니라 반복자입니다. 그러나 당신은 정말로 신경 쓰지 않을 것입니다.
답변
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)))
답변
가장 쉬운 옵션은 spit () 명령을 사용하는 것입니다. 그러나 사용하고 싶지 않거나 바자회 이유로 작동하지 않는 경우 항상이 방법을 사용할 수 있습니다.
word = 'foo'
splitWord = []
for letter in word:
splitWord.append(letter)
print(splitWord) #prints ['f', 'o', 'o']
