python - 通过 char 将字符串拆分为多个列表
问题描述
我有一个从函数返回的字符串,例如:
"& True True & True False & False"
我需要编写一个函数,将所有元素&
放在一个列表中并将其删除,例如:
[[True, True], [True, False], [False]]
我该怎么做?提前致谢!
解决方案
你可以使用拆分
l = "& True True & True False & False"
result = [j.split() for j in l.split('&') if j!='']
print(result)
输出
[['True', 'True'], ['True', 'False'], ['False']]
推荐阅读
- python - 理解 Python 解释器内部的问题
- android - android MotionLayout中的RecyclerView滚动问题
- performance - 避免循环中的条件语句
- c++ - 为什么MingW编译错误?
- r - 合并到最近的索引日期
- python - tensorflow 1.10 中的`tf.group(tf.local_variables_initializer(), tf.global_variables_initializer())` 有替代方法吗?
- java - 当从 Java Repository 发送“关键字”时,如何让 PSQL 认为它是一个变量
- python - 如何减少数百万张图片的文件大小?
- javascript - 存储到MYSQL数据库时如何删除时区?
- java - 从对话框更新 TableLayout Tablerows