Python 3.7 python,用于循环和列表追加

Python 3.7 python,用于循环和列表追加,python-3.7,Python 3.7,我试图提取字符串的一部分并将其附加到列表(消息)中。 但最后,我看到的只是列表中附加的一项(最后一项)。下面是我的代码。 我做错了什么 for item in all_text: message = [] if len(item) < 2: continue else: m_temp = item.split(']')[1].split(':') if len(m_temp) <= 1:

我试图提取字符串的一部分并将其附加到列表(消息)中。 但最后,我看到的只是列表中附加的一项(最后一项)。下面是我的代码。 我做错了什么

for item in all_text:
    message = []
    if len(item) < 2:
        continue
    else:
        m_temp = item.split(']')[1].split(':')
        if len(m_temp) <= 1:
            continue
        else:
            message.append(m_temp[1])
    print(len(message))
    print(message)
所有文本中的项目的
:
消息=[]
如果长度(项目)<2:
持续
其他:
m_temp=item.split(']')[1]。split(':')

如果len(m_temp)您将
消息重新定义为
for
循环每次迭代的空列表。因此,除了上一次迭代中的附件外,所有以前的附件都将丢失


尝试将
message=[]
移动到
for
循环之前的右侧。

message=[]
移动到for循环上方,将两个打印与for对齐

将message=[]移动到for循环上方,并将两个打印与for对齐