-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path32.sort python dictionary by key.py
More file actions
89 lines (70 loc) · 1.87 KB
/
Copy path32.sort python dictionary by key.py
File metadata and controls
89 lines (70 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def dictionairy():
# Declare hash function
key_value ={}
# Initializing value
key_value[2] = 56
key_value[1] = 2
key_value[5] = 12
key_value[4] = 24
key_value[6] = 18
key_value[3] = 323
print ("Task 1:-\n")
print ("Keys are")
# iterkeys() returns an iterator over the
# dictionary’s keys.
for i in sorted (key_value.keys()) :
print(i, end = " ")
def main():
# function calling
dictionairy()
# Main function calling
if __name__=="__main__":
main()
print("\n")
# function calling
def dictionairy():
# Declaring the hash function
key_value ={}
# Initialize value
key_value[2] = 56
key_value[1] = 2
key_value[5] = 12
key_value[4] = 24
key_value[6] = 18
key_value[3] = 323
print ("Task 2:-\nKeys and Values sorted in",
"alphabetical order by the key ")
# sorted(key_value) returns an iterator over the
# Dictionary’s value sorted in keys.
for i in sorted (key_value) :
print ((i, key_value[i]), end =" ")
def main():
# function calling
dictionairy()
# main function calling
if __name__=="__main__":
main()
print("\n")
# Function calling
def dictionairy():
# Declaring hash function
key_value ={}
# Initializing the value
key_value[2] = 56
key_value[1] = 2
key_value[5] = 12
key_value[4] = 24
key_value[6] = 18
key_value[3] = 323
print ("Task 3:-\nKeys and Values sorted",
"in alphabetical order by the value")
# Note that it will sort in lexicographical order
# For mathematical way, change it to float
print(sorted(key_value.items(), key =
lambda kv:(kv[1], kv[0])))
def main():
# function calling
dictionairy()
# main function calling
if __name__=="__main__":
main()