【发布时间】:2020-04-30 17:13:37
【问题描述】:
在 Python 3 中遍历元素列表时,如何“隔离”感兴趣的元素之间的内容?
我有一个清单:
list = ["<h1> question 1", "question 1 content", "question 1 more content", "<h1> answer 1", "answer 1 content", "answer 1 more content", "<h1> question 2", "question 2 content", "<h> answer 2", "answer 2 content"]
在这个列表中,有带有标签
如何将列表中属于 header 的元素连接成两个大小相等的列表:
headers = ["<h1> question 1", "<h1> answer 1", "<h1> question 2", "<h> answer 2"]
content = ["question 1 content question 1 more content", "answer 1 content answer 1 more content", "question 2 content", "answer 2 content"]
如果这两个列表的长度相同,在这种情况下,每个列表有 4 个元素。
我能够将这些部分分开,但您可以使用一些帮助来完成:
list = ["<h1> question 1", "question 1 content", "question 1 more content", "<h1> answer 1", "answer 1 content", "answer 1 more content", "<h1> question 2", "question 2 content", "<h> answer 2", "answer 2 content"]
headers = []
content = []
for i in list:
if "<h1>" in i:
headers.append(i)
if "<h1>" not in i:
tempContent = []
tempContent.append(i)
content.append(tempContent)
对如何组合这些文本以使它们一一对应有什么想法吗?
谢谢!
【问题讨论】:
标签: python python-3.x string list