如何将字典中的所有列表数字都设为零

时间:2019-03-15 23:09:11

标签: python python-3.x dictionary

我有以下内容:

d_out={'000000F|arrow_887067': ['G/G/G/G', '2', '3', '4', '5', '6', 'T/T/G/G', '8', '9', '10', '11', '12', '13', '14', 'G/G/G/G', 'T/T/T/G', '17', '18', 'T/G/G/G', '20', '21', '22', 'T/G/G/G', '24', '25', '26', '27', '28', 'T/G/G/G', '30', '31', '32', '33', 'G/G/G/G', '35', '36', '37', '38', '39', '40'],
       '000000F|arrow_887188': ['CTTTTATTC/CTTTTATTC/CTTTTATTC/CTTTTATTC', '2', '3', '4', '5', '6', 'A/A/A/A', '8', '9', '10', '11', '12', 'G/G/A/A', '14', 'A/A/A/A', '16', '17', '18', '19', '20', '21', '22', 'A/A/A/A', '24', '25', '26', '27', '28', '29', 'A/A/A/A', '31', '32', 'A/A/A/A', '34', '35', '36', '37', '38', '39', '40']}`

我想要以下内容:

d2_out={'000000F|arrow_887067': ['G/G/G/G', '0', '0', '0', '0', '0', 'T/T/G/G', '0', '0', '0', '0', '0', '0', '0', 'G/G/G/G', 'T/T/T/G', '0', '0', 'T/G/G/G', '0', '0', '0', 'T/G/G/G', '0', '0', '0', '0', '0', 'T/G/G/G', '0', '0', '0', '0', 'G/G/G/G', '0', '0', '0', '0', '0', '0'], 
        '000000F|arrow_887188': ['CTTTTATTC/CTTTTATTC/CTTTTATTC/CTTTTATTC', '0', '0', '0', '0', '0', 'A/A/A/A', '0', '0', '0', '0', '0', 'G/G/A/A', '0', 'A/A/A/A', '0', '0', '0', '0', '0', '0', '0', 'A/A/A/A', '0', '0', '0', '0', '0', '0', 'A/A/A/A', '0', '0', 'A/A/A/A', '0', '0', '0', '0', '0', '0', '0']}`

我该怎么做?

我尝试过:

{key: [value if not isinstance(value, int) else 0 for value in lst] 
           for key, lst in d_out.items()}

{i:[x if type(x)!=int else 0 for x in d_out[i]] for i in d_out}

1 个答案:

答案 0 :(得分:3)

您的第一次尝试失败,因为这些值不是int对象-它们是只有数字的字符串。请尝试:

{key: [value if not value.isdigit() else '0' for value in lst]
     for key, lst in d1.items()}