分割字符串后的IndexError

时间:2019-06-15 17:26:05

标签: python python-3.x list indexing

我在使用简单列表时遇到问题。

我想把它们分开。

但是,调试器告诉我index is out of range ...

def __init__(self,num, price):
    self.num = num
    self.price = price

def to_pay(self)   
   y = self.num        # y = 'XY 70842'

   x = y.split(' ') # x = ['XY', '70842']

   letters = list(x[0])  # ['X', 'Y']

   numbers = list(x[1])  # ['7', '0', '8', '4', '2']

预期结果:

   letters = ['X', 'Y']

   numbers = ['7', '0', '8', '4', '2']

实际结果:

   numbers = list(x[1])  # ['7', '0', '8', '4', '2']
IndexError: list index out of range

0 个答案:

没有答案