问题:python中是否有将单词拆分为列表的函数?[重复]

python中是否有将单词分解为单个字母列表的函数?例如:

s="Word to Split"

要得到

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' ....]

回答 0

>>> list("Word to Split")
['W', 'o', 'r', 'd', ' ', 't', 'o', ' ', 'S', 'p', 'l', 'i', 't']
>>> list("Word to Split")
['W', 'o', 'r', 'd', ' ', 't', 'o', ' ', 'S', 'p', 'l', 'i', 't']

回答 1

最简单的方法可能只是使用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]

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]

回答 2

列表功能将执行此操作

>>> list('foo')
['f', 'o', 'o']

The list function will do this

>>> list('foo')
['f', 'o', 'o']

回答 3

滥用规则,结果相同:(x表示“要拆分的单词”中的x)

实际上是一个迭代器,而不是列表。但是,您可能不太在意。

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+=1

print(word_list)

['j', 'u', 's', 't', ' ', 't', 'r', 'y', 'i', 'n', 'g', ' ', 'o', 'u', 't']
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']

回答 5

def count():列表=’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)))

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)))

回答 6

最简单的选择是仅使用spit()命令。但是,如果您不想使用它或由于某种市集原因而无法使用它,则可以始终使用此方法。

word = 'foo'
splitWord = []

for letter in word:
    splitWord.append(letter)

print(splitWord) #prints ['f', 'o', 'o']

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']

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。