5 回答
TA贡献1824条经验 获得超8个赞
您可以将您的字典转换为 DataFrame,这将使您的事情变得非常简单:
from pandas.DataFrame import from_dict
d = {'a':(1,2,3),'b':(4,5,6)}
d = from_dict(d, orient='index')
d[0] # print values of value1
d[1].max() # max of value2
d[2].mean() # mean of value3
TA贡献1835条经验 获得超7个赞
您可以使用列表理解来实现此目的:
persons = {'1': (1,2,3), '2': (4,5,6), '3': (7,8,9)}
# First Value's
first_values = " ".join([str(x[0]) for x in persons.values()])
print(first_values) # prints 1 4 7
# Max of second value's
max_value2 = max([x[1] for x in persons.values()])
print(max_value2) # prints 8
# Average of value3's
third_values = [x[2] for x in persons.values()]
average_of_third_values = sum(third_values) / len(third_values)
# in case avoid zero division :
# average_of_third_values = sum(third_values) / (len(third_values) or 1)
print(average_of_third_values) # prints 6
# to get value1 of values which has max value2
value1_of_max = [x[0] for x in persons.values() if x[1]==max_value2]
print(value1_of_max) # prints [7]
# Its possible to be exist more than 1 person that has value2 which equals to max number, like so
# persons = {'1': (1,2,3), '2': (4,8,6), '3': (7,8,9)}
# so we print them as list
TA贡献1829条经验 获得超9个赞
关于什么?
d = {
"key1": (1,2,3),
"key2": (3,4,5),
"key4": (5,6,8),
}
[ print(val[0]) for _, val in d.items()]
TA贡献1878条经验 获得超4个赞
关于什么?
d = {
"key1": (1,2,3),
"key2": (3,4,5),
"key4": (5,6,8),
}
[ print(val[0]) for _, val in d.items()]
TA贡献2041条经验 获得超4个赞
您可以使用for loop迭代:
d = {'test1': (1, 2, 3), 'test2': (4, 5, 6), 'test3': (7, 8, 9)}
for key in d.values():
print(key[0])
添加回答
举报