pycharm 如何使用一个列表创建两个/或更多列表?

bogh5gae  于 2023-01-13  发布在  PyCharm
关注(0)|答案(1)|浏览(152)

最小值=["1"、"2"、"、" 3 "、" 4 "、" 5 "、"、"6"]
给定上面的列表,我如何拆分列表并在每次发现""(仅空引号)时创建另一个列表?

lst =["1","2","","3","4","5","","6"]
lst2 = []
for a in lst:
    if a == "":
        continue
    lst2.append(int(a))

print(lst2)
Output:[1, 2, 3, 4, 5, 6]

预期产出:第二次登录=[["1"、"2"]、["3"、"4"、"5"]、["6"]]

y53ybaqx

y53ybaqx1#

lst =["1","2","","3","4","5","","6"]
arr = []
new_lst = []
for i in lst:
  if i == "":
     arr.append(new_lst)
     new_lst = []
  else:
      new_lst.append(i)
 

if len(new_lst) > 0:
    arr.append(new_lst)

print(arr)

相关问题