我将此列表作为示例
favourite_candy_boys_girls = [['Chips' , 1, 2], ['Chocolate', 3, 4], ['Lollipops', 2, 4]]
第二栏是男生,第三栏是女生。我想写一个函数,我返回第一个女孩最喜欢的糖果(在这种情况下它应该是巧克力)。我知道如果我运行以下内容:
max(l[2] for l in favourite_candy_boys_girls)
我会找到最大值,但我不太确定如何返回糖果
EDIT 我正在考虑使用如下循环将其编写为函数:
>>> def get_candy_with_max_girls(Candy: SystemData):
"""Return the candy type that has the most girls.
If there is a tie of girls for a particular candy type, return the
candy type that appears first in candy.
>>> get_candy_with_max_girls(favourite_candy_boys_girls):
'Chocolate'
"""
答案 0 :(得分:2)
您可以使用max函数的key
参数。它允许您在排序之前提供将在您的项目上使用的功能:
l = [['Chips' , 1, 2], ['Chocolate', 3, 4], ['Lollipops', 2, 4]]
max(l, key=lambda x: x[2])[0]
# returns 'Chocolate'
答案 1 :(得分:2)
如果您使用字典,而密钥是食品,则可以使用以下代码:
<?php
$html = '
<div class="xGh" style="background-image: url(\'name_file0.jpg\');"></div>
<div class="xGh" style="background-image: url(\'name_file1.jpg\');"></div>
<div class="xGh" style="background-image: url(\'name_file2.jpg\');"></div>
<div class="xGh" style="background-image: url(\'name_file3.jpg\');"></div>
';
libxml_use_internal_errors(true);
$dom = new DOMDocument('1.0', 'utf-8');
$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$xpath = new DOMXPath($dom);
$divs = $xpath->query('//div[@class=\'xGh\']/@style');
/* @var DOMAttr $div */
foreach ($divs as $div) {
preg_match("/url\('(.*)'\)/", $div->nodeValue, $matches);
if (!isset($matches[1])) {
continue;
}
$url = $matches[1];
// name_file0.jpg, ...
echo $url . "\n";
}
哪个输出:
testlist = {"Chips":[1,2],"Chocolate":[3,4]}
output = max(testlist, key=lambda x: testlist[x][-1])
print output
答案 2 :(得分:1)
试试这个(不需要图书馆,适合女孩和男孩,也适用于多种糖果价值):
favourite_candy_boys_girls = [['Chips', 1, 2], ['Chocolate', 3, 4], ['Lollipops', 2, 4], ["gumdrop", 3, 4]]
my_dict = {}
# Convert Your List to Dictionary
for candy_box in favourite_candy_boys_girls:
my_dict[candy_box[0]] = {"Girls": candy_box[1], "Boys": candy_box[2]}
def the_most_delicious_candy(get_dict, girl_or_boy):
if girl_or_boy == "girls":
just_girls = {}
for candy, values in get_dict.items():
just_girls[candy] = values["Girls"]
find_max = max(just_girls.values())
last_buffer = []
for candy, values in get_dict.items():
if values['Girls'] == find_max:
make_new_dict = { candy: get_dict[candy] }
last_buffer.append(make_new_dict)
return last_buffer
elif girl_or_boy == "boys":
just_boys = {}
for candy, values in get_dict.items():
just_boys[candy] = values["Boys"]
find_max = max(just_boys.values())
last_buffer = []
for candy, values in get_dict.items():
if values['Boys'] == find_max:
make_new_dict = {candy: get_dict[candy]}
last_buffer.append(make_new_dict)
return last_buffer
else:
return "Just Girls and Boys are available"
呼唤男生:
print(the_most_delicious_candy(my_dict, "boys"))
BOYS OUTPUT:
[{'Chocolate': {'Boys': 4, 'Girls': 3}}, {'Lollipops': {'Boys': 4, 'Girls': 2}}, {'gumdrop': {'Boys': 4, 'Girls': 3}}]
呼唤女孩:
print(the_most_delicious_candy(my_dict, "girls"))
GIRLS OUTPUT:
[{'gumdrop': {'Boys': 4, 'Girls': 3}}, {'Chocolate': {'Boys': 4, 'Girls': 3}}]
祝你好运......
答案 3 :(得分:0)
这将有效:
var logoimg = document.getElementById("logo-cat");
logoimg.src = "https://cdn.vox-cdn.com/uploads/chorus_asset/file/8597673/ph_1_color_black_moon_copy_1000x1000.png";
最大通话通过按键功能为您提供最多女孩的三连音。然后你就抓住那个三胞胎的糖果。
答案 4 :(得分:0)
在执行max
时只需使用密钥def get_candy_with_max_girls(favourite_candy_boys_girls):
return max(favourite_candy_boys_girls, key = lambda k:k[2])[0]