0

我有两个列表:total_priceunits_b,它们都包含空值,并且我有一个表达式来将 total_price 除以 units_b:

    total_price = [None, None, 10, 20]
    units_b = [None, None, 1, 2]

    average_price = [(x/y) for x, y in zip(total_price, units_b)]

试图运行average_price给我一个错误,说我不能划分两个“NoneTypes”。有什么方法可以排除除法的空值但保持原始列表的完整性?

4

3 回答 3

2
average_price = [(x/y) for x, y in zip(total_price, units_b) if x is not None and y is not None]
于 2012-10-05T00:33:57.093 回答
2
total_price = [None, None, 10, 20]
units_b = [None, None, 1, 2]

average_price = [(x/y) for x, y in zip(total_price, units_b) if x is not None and y is not None]
于 2012-10-05T00:34:01.870 回答
0
average_price = [(x/y) for x, y in zip(total_price, units_b) if not (x==None or y==None)]

虽然你真的应该阅读文档

于 2012-10-05T00:34:04.257 回答