我对代码的输出有疑问:
d=({'country':'India','population':89},{'country':'USA','population':78},
{'country':'UK','population':65},{'country':'Canada','population':70})
for i in d:
print(i)
[list(i.values())[1] for i in d]
我希望输出为[89,78,65,70]
,但显示为:
['India', 'USA', 'UK', 'Canada']
答案 0 :(得分:2)
您有一个词典列表。如果您使用的Python版本无法保证字典键的顺序,则无法确定哪个键类似:
list(i.values())[1]
之所以会产生,是因为list(i.values())
可能是[population, country]
或[country, population]
最好使用类似的理解来拉出所需的显式键-即使在字典键具有可预测顺序的python版本中,因为代码的含义会更清楚。
d=({'country':'India','population':89},{'country':'USA','population':78},
{'country':'UK','population':65},{'country':'Canada','population':70})
[i['population'] for i in d] # will get population regardless of key order
# [89, 78, 65, 70]
答案 1 :(得分:0)
您的代码显示正确的预期输出,请检查 您可以通过以下方式缩短代码:
d=({'country':'India','population':89},{'country':'USA','population':78},
{'country':'UK','population':65},{'country':'Canada','population':70})
result = [i['population'] for i in d]
print(result)
输出
[89, 78, 65, 70]
答案 2 :(得分:-2)
尝试一下
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
options.addArguments("--disable-web-security");
options.addArguments("--no-proxy-server");
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("credentials_enable_service", false);
prefs.put("profile.password_manager_enabled", false);
options.setExperimentalOption("prefs", prefs);