附加empyt列表时,一切都会变成空白,为什么?

时间:2019-04-08 03:31:00

标签: python regex

如何使用python3在没有任何更改的情况下将空列表添加到列表中?

import re
l=[]
list1 = ["a","b","c"]
sublist2=[]
for i in sublist2:
   list2 = re.sub("[0-9']+", '',i)
l.append(list1)
l.append(list2)
print(l)
  

预期输出:

 [["a","b","c"],[]]

3 个答案:

答案 0 :(得分:0)

要在python中添加2个列表,有一个不错的简单操作符可以执行您想要的操作(添加2个列表);该运算符为:加+

例如,查看以下代码:

a = list(range(3))
b = list(range(4, 7))
print(a + b) # output: [0, 1, 2, 4, 5, 6]
print(b + a) # output: [4, 5, 6, 0, 1, 2]

答案 1 :(得分:0)

执行此操作:

l=[]
list1 = ["a","b","c"]
sublist2=[]
l.append(list1)
l.append(sublist2)
print(l)  ## [['a', 'b', 'c'], []]  

答案 2 :(得分:0)

这是您要找的吗?

import re
l=[]

list1 = ["a","b","c"]

sublist2=[]
list2=[re.sub("[0-9']+", '',i) for i in sublist2]

l.append(list1)
l.append(list2)
print(l)
#result:[['a', 'b', 'c'], []]
import re
l=[]

list1 = ["a","b","c"]

sublist2=["1","3","f"]
list2=[re.sub("[0-9']+", '',i) for i in sublist2]

l.append(list1)
l.append(list2)
print(l)
#result:[['a', 'b', 'c'], ['', '', 'f']]