2226460 / s with first_1
1905620 / s with first_2
1994654 / s with first_3
1777946 / s with first_4
3681252 / s with first_5
2829067 / s with first_6
2600622 / s with first_7
以下是我尝试过的所有方法:
def first_1():
return next(iter(biased_dict))
def first_2():
return list(biased_dict)[0]
def first_3():
return next(iter(biased_dict.keys()))
def first_4():
return list(biased_dict.keys())[0]
def first_5():
for key in biased_dict:
return key
def first_6():
for key in biased_dict.keys():
return key
def first_7():
for key, v in biased_dict.items():
return key
prices = {
"banana" : 4,
"apple" : 2,
"orange" : 1.5,
"pear" : 3
}
# you can create a list of keys using list(prices.keys())
prices_keys_list = list(prices.keys())
# now you can access the first key of this list
print(prices_keys_list[0]) # remember 0 is the first index
# you can also do the same for values using list(prices.values())
prices_values_list = list(prices.values())
print(prices_values_list[0])