[英]How to skip Index count using enumerate when value is zero?
I have currently one dict with key-value and one list with values.我目前有一个带有键值的字典和一个带有值的列表。 eg
例如
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.我想要做的是,我想删除“总计”中值为 0 的所有值,这意味着它最终只会是 9 个数字,这些数字加起来
item_number
数量。
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
.其中 key 是
item_number
, value 是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.我认为这是由于每个循环的
Index
都在增加。 I wonder how can I skip the counting increase if the value is 0?我想知道如果值为 0 如何跳过计数增加?
If I understand you correctly:如果我理解正确的话:
out = dict(
zip(data["item_number"], (v for v in data["total"].values() if v != 0))
)
print(out)
Prints:印刷:
{
"1": 22,
"2": 35,
"3": 108,
"4": 70,
"5": 24,
"6": 75,
"7": 123,
"8": 224,
"9": 28,
}
Either use a count variable instead of enumerate()
, or filter before enumerating.使用计数变量而不是
enumerate()
,或者在枚举之前进行过滤。
index = 0
for value in data['total'].values():
if value != 0:
print((data['item_number'][index], value))
index += 1
Output: Output:
('1', 22)
('2', 35)
('3', 108)
('4', 70)
('5', 24)
('6', 75)
('7', 123)
('8', 224)
('9', 28)
for index, value in enumerate(v for v in data['total'].values() if v != 0):
print((data['item_number'][index], value))
(Same output) (相同的输出)
And this can be simplified further using zip()
like in Andrej's answer .这可以像Andrej's answer中那样使用
zip()
进一步简化。
Note : I'm not using a dict here for simplicity and because your code as posted doesn't use one.注意:为了简单起见,我在这里没有使用字典,因为您发布的代码没有使用字典。 I think it's obvious how you would incorporate one though.
我认为很明显你会如何合并一个。
There are two tasks at hand here:这里有两个任务:
data["total"]
data["total"]
中过滤掉零值data["item_number"]
, and values are from the filtered collection we created above.data["item_number"]
的元素,值来自我们上面创建的过滤集合。 So, let's do that:那么,让我们这样做:
filtered_values = [val for val in data["total"].values() if val]
# [22, 35, 108, 70, 24, 75, 123, 224, 28]
new_dict = dict(zip(data["item_number"], filtered_values))
Which gives the required new_dict
:这给出了所需的
new_dict
:
{'1': 22,
'2': 35,
'3': 108,
'4': 70,
'5': 24,
'6': 75,
'7': 123,
'8': 224,
'9': 28}
You can save one loop through your data["total"]
if you define filtered_values
as a generator expression, or combine the two lines:如果将
filtered_values
定义为生成器表达式,或者将两行组合起来,则可以通过data["total"]
保存一个循环:
new_dict = dict(zip(data["item_number"], (val for val in data["total"].values() if val)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.