在列表中分割字符串

时间:2018-07-06 16:23:39

标签: python-3.x list

我有一个包含字符串值的列表:

a_list = ['1/one','2/two','3/three']

我想从这样的单词中拆分数字:

number = [1,2,3]
words = [one,two,three]

然后我确定条件是数字等于一还是三,然后将数字添加到新列表中。这是我的代码:

import re
new_list = []

for i in a_list:   

   word =re.findall(r"[\w']+",i)[1]
   number = re.findall(r"[\w']+",i)[0]
   if  word == 'one' or word == 'three' :
      new_list.append(number)

我遇到了错误:

IndexError: list index out of range

有什么帮助吗?...在​​此先感谢

0 个答案:

没有答案
相关问题