python 不能写new_loss=old_loss=[]
这样 两个变量实际上是同一个list
要分开写new_loss=[]
Old_loss=[]
以下列数据文件为例:
def analyze_mmr(fn): #初始化方法一 new_loss = [] old_loss = [] #初始化方法二 new_loss = old_loss =[] with open(fn, encoding='utf8') as f: for line in f: parts = line.split('=') if parts[0] == 'new_loss': print(parts[0]) print('add_new_loss') new_loss.append(parts[1].strip()) elif parts[0] == 'old_loss': old_loss.append(parts[1].strip()) else: pass return new_loss, old_loss
运行
n, o = analyze('../example.txt')
初始化方法一输出 n=[1,2],o=[3,4]
初始化方法二输出 n=[1,2,3,4],o=[1,2,3,4]
测试文档:
example.txt
new_loss=1
old_loss=3
new_loss=2
old_loss=4