[英]How to calculate length of list by prolog
l have a list following 我下面有一个清单
List = [['Dev', 'Jon'], ['Dev', 'Darin'], ['Dev', 'Hans'],
['Test', 'Richard'], ['Test', 'Mike'], ['Test', 'Jon'], ['Test', 'Hans'],
['Chef', 'Bozho'], ['Chef', 'Mike'], ['Chef', 'Jon']] .
I want to calculate how many times every person appear and add his time step by step for example, the result is following: 我想计算每个人出现的次数并逐步添加他的时间,例如,结果如下:
[['Dev', 'Jon', 1], ['Dev', 'Darin',1], ['Dev', 'Hans', 1],
['Test', 'Richard', 1], ['Test', 'Mike', 1], ['Test', 'Jon', 2], ['Test', 'Hans', 2],
['Chef', 'Bozho', 1], ['Chef', 'Mike', 2], ['Chef', 'Jon', 3]]
thanks very much :) 非常感谢 :)
The solution could be something like this: 解决方案可能是这样的:
count([], _, Zs, Zs).
count([[X,Y]|T], Ys, Zs, Us) :- select([Y,C], Ys, Ys1), C1 is C+1, append(Zs, [[X,Y,C1]], Zs1), count(T, [[Y,C1]|Ys1], Zs1, Us), !.
count([[X,Y]|T], Ys, Zs, Us) :- append(Zs, [[X,Y,1]], Zs1), count(T, [[Y,1]|Ys], Zs1, Us).
transform(L1, L2) :- count(L1, [], [], L2).
We use Ys
list to store counters for all people and Zs
list to accumulate every person with corresponding index. 我们使用Ys
列表存储所有人员的计数器,使用Zs
列表累积具有相应索引的每个人。 Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.