首页 > 解决方案 > 通过 char 将字符串拆分为多个列表

问题描述

我有一个从函数返回的字符串,例如:

"& True  True & True  False & False"

我需要编写一个函数,将所有元素&放在一个列表中并将其删除,例如:

[[True, True], [True, False], [False]] 

我该怎么做?提前致谢!

标签: pythonpython-3.xstringlistsplit

解决方案


你可以使用拆分

 l = "& True  True & True  False & False"
 result = [j.split() for j in l.split('&') if j!='']
 print(result)

输出

[['True', 'True'], ['True', 'False'], ['False']]

推荐阅读