Mandie Driskill
Mandie Driskill

Reputation: 83

How to turn all list numbers in a dictionary to zeros

I have the following:

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']}`

I would like the following:

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']}`

How can I do this?

I have tried:

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

and

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

Upvotes: 1

Views: 40

Answers (1)

Adam Smith
Adam Smith

Reputation: 54223

Your first attempt failed because the values are not int objects -- they are strings of only digits. Try instead:

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

Upvotes: 3

Related Questions