2 回答
TA贡献2041条经验 获得超4个赞
让我们用字典理解来做:
我们将循环字典键和值并按值条件进行过滤。
Sig_List = open('file.txt')
data = Sig_List.readlines()
N, E = [], []
for line in data:
values = [str(s) for s in line.split()]
N.append(values[0])
E.append(values[1])
Sig1 = dict(zip(N, E))
Sig1 = {'Atg4a': '1.0241564267288767', 'Mast2': '-1.0014505579938486'}
def up1(Sig1):
return [key for key, value in Sig1.items() if float(value) > 0]
def down1(Sig1):
return [key for key, value in Sig1.items() if float(value) < 0]
ups = up1(Sig1)
print(f'Ups: {ups}')
downs = down1(Sig1)
print(f'Downs: {downs}')
这些函数可以集成到一个函数中,因此我们保存列表上的第二次迭代:
def diveide_by_directon(Sig1):
ups = []
downs = []
for key, value in Sig1.items():
if 0 < float(value):
ups.append(key)
else:
downs.append(key)
return ups, downs
ups, downs = diveide_by_directon(Sig1)
print(f'Ups: {ups}')
print(f'Downs: {downs}')
TA贡献2003条经验 获得超2个赞
你可以用字典理解来完成这一切:
data = Sig_List.readlines()
Sig1 = {
str(s)[0], str(s)[1] for s in line.split()
for line in data
}
ups = {k, v for k, v in Sig1 if int(v) > 0}
downs = {k, v for k, v in Sig1 if int(v) < 0}
添加回答
举报