【问题标题】:Is there a function in python to split a word into a list? [duplicate]python中是否有将单词拆分为列表的功能? [复制]
【发布时间】:2010-09-11 22:27:28
【问题描述】:

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

s="Word to Split"

得到

wordlist=['W','o','r','d','','t','o' ....]

【问题讨论】:

  • 只需查看此文档:docs.python.org/library/stdtypes.html
  • 老线程,但值得一提的是:大多数时候你根本不需要这样做。 python字符串的字符可以直接作为列表访问,即。 s[2] 是“r”,s[:4] 是“Word”,len(s) 是 13。您也可以遍历它们:for char in s: print char
  • @domoarrigato 但由于 srting 的不同行为和可变性列表可能是这样做的理由。
  • def show_letters(word): for ch in word: print(ch) show_letters("Hello")

标签: python function split


【解决方案1】:
>>> list("Word to Split")
['W', 'o', 'r', 'd', ' ', 't', 'o', ' ', 'S', 'p', 'l', 'i', 't']

【讨论】:

  • 任何你知道为什么 "Word to Split".split('') 不做同样事情的原因。它没有,但似乎确实应该这样做。
  • @Walter Nissen:尝试这样做时我得到“ValueError:空分隔符”。空的正则表达式的定义不是很好。
  • 使用split()获取字符是否没有分隔符? split() 接受第二个参数 maxsplits,但没有与 list() 等效的参数。当然有变通办法...
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    列表函数会这样做

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

    【讨论】:

      【解决方案4】:

      滥用规则,同样的结果: (x for x in 'Word to split')

      实际上是一个迭代器,而不是一个列表。但很可能你不会真正关心。

      【讨论】:

      • 当然,直接使用的'Word to split'也是自身字符的可迭代,所以生成器表达式只是无意义的包装。
      【解决方案5】:
      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']
      

      【讨论】:

        【解决方案6】:

        定义计数(): 列表 = '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)))
        

        【讨论】:

          【解决方案7】:

          最简单的选择是只使用 list() 命令。但是,如果您不想使用它或由于某些集市原因它无法正常工作,您可以随时使用此方法。

          word = 'foo'
          splitWord = []
          
          for letter in word:
              splitWord.append(letter)
          
          print(splitWord) #prints ['f', 'o', 'o']
          

          【讨论】:

            猜你喜欢
            • 2020-11-16
            • 2023-03-26
            • 1970-01-01
            • 1970-01-01
            • 2018-04-06
            • 2021-10-30
            • 2021-09-09
            • 2018-03-13
            • 1970-01-01
            相关资源
            最近更新 更多