我在php中有一个函数:
function cmp_key($lst){
$itersect_size = count(array_intersect($zset, $lst)); //zset is a list which i have
return $intersect_size,-count($lst)
}
然后在python中使用此代码:
list_with_biggest_intersection = max(iterable,key = cmp_key)
如果我想使用php函数cmp_key
作为max函数的关键字,我怎样才能在php中执行上面的代码行...
答案 0 :(得分:0)
调用函数将返回值作为max函数的参数传递。
list_with_biggest_intersection = max(iterable, cmp_key($lst));
答案 1 :(得分:0)
在Python中复制@ mgilson的答案,这是PHP中的等价物。
function cmp_key($set, $list) {
return count(array_intersect($set, $list));
}
// This iterates over all lists and compares them with some
// original list, here named $set for consistency with the other example.
$largest = NULL;
foreach ($lists as $list) {
if (!isset($largest)) {
$largest = array('list' => $list, 'count' => cmp_key($set, $list));
}
else {
$count = cmp_key($set, $list);
if ($count > $largest['count']) {
$largest = array('list' => $list, 'count' => $count);
}
}
}
$list_with_biggest_intersection = $largest['list'];