如何比较列表并获得总匹配项数量 [英] How to compare lists and get total matching items count
本文介绍了如何比较列表并获得总匹配项数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Garage.cars:对象车库具有 FK 的汽车
Person.carwishlst: 对象Person拥有他们想要的汽车的 FK 。
Person.carwishlst: object Person has FK of cars they would like.
在Django中如何实现以下循环?
In Django how to I achieve the following, loop?...
获取车辆总数 x ,该车辆与Garage有所匹配。
Get the total number of cars x the Person has which match the ones the Garage has.
结果:
ie Grange有4辆你想要的车
Outcome: i.e. Grange has 4 cars you want
假设,但让我们说Grange车型有一辆FK cars = models.ManyToManyField(Cars)
Hypothetical, but lets say Grange model has an FK cars = models.ManyToManyField(Cars)
人也有一个FK cars_wishlist = models.ManyToManyField(Cars)
Person also has a FK cars_wishlist = models.ManyToManyField(Cars)
推荐答案
假设 car
和 carwishlst
是列表:
您将执行以下操作
def cars_you_want(cars, carwishlst):
car_set = [val for val in cars if val in carwishlst]
return len(car_set)
应该这样做。
这篇关于如何比较列表并获得总匹配项数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文