【发布时间】:2022-12-02 05:41:10
【问题描述】:
I have currently one dict with key-value and one list with values. e.g.
data = {
\'total\': {
\'06724\': 0,
\'06725\': 0,
\'06726\': 0,
\'06727\': 0,
\'06712\': 22,
\'06713\': 35,
\'06714\': 108,
\'06715\': 70,
\'06716\': 0,
\'06717\': 24,
\'06718\': 0,
\'06719\': 0,
\'06720\': 0,
\'06709\': 75,
\'06710\': 123,
\'06711\': 224,
\'06708\': 28,
\'06723\': 0,
\'06721\': 0,
\'06722\': 0
},
\'item_number\': [\'1\', \'2\', \'3\', \'4\', \'5\', \'6\', \'7\', \'8\', \'9\']
}
for Index, value in enumerate(data[\'total\'].values()):
if value and value != \'0\':
print(data[\'item_number\'][Index], value)
What I am trying to do is that I want to remove all values in \'total\' that has the value 0 meaning that it would only end up being 9 numbers which adds up to the item_number amount.
What I am trying to achieve is that I want print out:
Expected:
{
\'1\': 22,
\'2\': 35,
\'3\': 108,
\'4\': 70,
\'5\': 24,
\'6\': 75,
\'7\': 123,
\'8\': 224,
\'9\': 28
}
where key is the item_number and the value is total.
However the code I am currently trying gives me the error:
print(data[\'item_number\'][Index], value)
IndexError: list index out of range
which I believe is due to the Index increasing for each loop. I wonder how can I skip the counting increase if the value is 0?
标签: python