【问题标题】:convert string to list [duplicate]将字符串转换为列表[重复]
【发布时间】:2017-06-28 16:37:57
【问题描述】:

我有一个这样的字符串:

st = "The most basic data structure in Python is the sequence * Each element of a sequence is assigned a number * its position or index * The first index is zero * the second index is one * and so forth *"

我想像这样拆分到列表中:

ls =["The most basic data structure in Python is the sequence","Each element of a sequence is assigned a number","its position or index",.....]

我刚开始python,请帮帮我

【问题讨论】:

    标签: python list


    【解决方案1】:

    您可以使用函数str.split 将字符串拆分为特定字符的数组:

    >>> str.split(st,"*")
    ['The most basic data structure in Python is the sequence ',
     ' Each element of a sequence is assigned a number ',
     ' its position or index ',
     ' The first index is zero ',
     ' the second index is one ',
     ' and so forth ',
     '']
    

    【讨论】:

      【解决方案2】:

      你可以用一个字符来分割一个字符串:

      yourString = "Hello * my name * is * Alex"
      yourStringSplitted = yourString.split('*')
      

      【讨论】:

        猜你喜欢
        • 2011-05-27
        • 2011-02-23
        • 1970-01-01
        • 2018-01-27
        • 2020-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-07
        相关资源
        最近更新 更多