尝试创建一个函数,根据字典中的“整数”值返回“字符串”键

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):

    for key, value in Sig1.items():

        if value >0:

            x=list(key)

    

up1(Sig1)

尝试输入带有 FC 值的基因名称列表,然后生成向上向下基因的子列表,这些子列表可以组合起来在不同的实验中制作维恩图。无法定义一个条件函数,该函数仅根据基因名称是向上还是向下返回基因名称。任何帮助或建议将不胜感激。


哆啦的时光机
浏览 103回答 2
2回答

缥缈止盈

让我们用字典理解来做:我们将循环字典键和值并按值条件进行过滤。Sig_List = open('file.txt')data = Sig_List.readlines()N, E = [], []for line in data:&nbsp; &nbsp; values = [str(s) for s in line.split()]&nbsp; &nbsp; N.append(values[0])&nbsp; &nbsp; E.append(values[1])&nbsp; &nbsp; Sig1 = dict(zip(N, E))&nbsp; &nbsp; Sig1 = {'Atg4a': '1.0241564267288767', 'Mast2': '-1.0014505579938486'}def up1(Sig1):&nbsp; &nbsp; return [key for key, value in Sig1.items() if float(value) > 0]def down1(Sig1):&nbsp; &nbsp; return [key for key, value in Sig1.items() if float(value) < 0]ups&nbsp; &nbsp; &nbsp;= up1(Sig1)print(f'Ups: {ups}')downs&nbsp; &nbsp;= down1(Sig1)print(f'Downs: {downs}')这些函数可以集成到一个函数中,因此我们保存列表上的第二次迭代:def diveide_by_directon(Sig1):&nbsp; &nbsp; ups&nbsp; &nbsp; &nbsp;= []&nbsp; &nbsp; downs&nbsp; &nbsp;= []&nbsp; &nbsp; for key, value in Sig1.items():&nbsp; &nbsp; &nbsp; &nbsp; if 0 < float(value):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ups.append(key)&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; downs.append(key)&nbsp; &nbsp; return ups, downsups, downs&nbsp; &nbsp; &nbsp;= diveide_by_directon(Sig1)print(f'Ups: {ups}')print(f'Downs: {downs}')

湖上湖

你可以用字典理解来完成这一切:data = Sig_List.readlines()Sig1 = {&nbsp; &nbsp; str(s)[0], str(s)[1] for s in line.split()&nbsp; &nbsp; 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}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python