【问题标题】:Splitting all characters in a string into an array in python将字符串中的所有字符拆分为python中的数组
【发布时间】:2018-01-04 22:27:21
【问题描述】:

有没有办法将'00:00' 等字符串拆分成['0', '0, ':' , '0', '0'] 等数组?我试过了:

time = raw_input()
digital = time.split()

其中时间是输入字符串'00:00'

【问题讨论】:

    标签: python arrays string python-2.7 character


    【解决方案1】:

    无需拆分,直接使用list(..)即可:

    digital = list(time)
    

    字符串是一个可迭代:你可以迭代字符。 list(..) 构造函数将一个可迭代对象作为输入,并构造一个列表,其中可迭代对象的每个元素(在本例中为字符)都是列表的一个元素。

    例如:

    >>> time = raw_input()
    00:00
    >>> list(time)
    ['0', '0', ':', '0', '0']
    

    【讨论】:

      猜你喜欢
      • 2018-08-03
      • 1970-01-01
      • 2022-01-18
      • 2017-11-07
      • 1970-01-01
      • 2012-02-22
      相关资源
      最近更新 更多