-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDictionary assign keys values.py
54 lines (41 loc) · 1.71 KB
/
Dictionary assign keys values.py
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
# All the ins and outs of dictionaries
# immutable, once assigned can't be change in size
line = 'Converting a text string to list and then converting to dictionary'
print(line)
frequency= { }
txt = line.lower().split()
for item in txt: # assigning dictionary key and values
frequency[item] = txt.count(item)
print(frequency.items())
print()
print('\n1. See code for declaring dictionary && assigning individual elements')
d = { } # declare dictionary && assigning individual elements
d['a'] = 'alpha' #key = a, storage alpha at key a
d['o'] = 'omega'
print('2. Printing items, keys, and values of a dictionary')
print(d.items()) #print(d.keys()) print(d.values())
print(d['a'])
print(d.get('o')) # using get to avoid none existing index
print('Is a in d?' , 'a' in d)
print('\nprint key in alphabetically -> value')
for k in sorted(d.keys()):
print ('key:', k, '->', d[k])
print('\n3. Print all the tuples in the entire dictionary ')
for tuple in d.items():
print(tuple)
mydict = {'q':1, 'w':2, 't':5, 'y':6}
print('\n4. Simple sort of dictionary into type list',sorted(mydict) )
print('Sorting by keys')
for key in sorted(mydict.keys()):
print("%s: %s" % (key, mydict[key]), end=',')
print('\nSorting by values')
for key, value in sorted(mydict.items(), key=lambda item: item[1]):
print("%s: %s" % (key, value), end=',')
print('\n')
print('Sorting twice: first by name then by salary')
employees = [{'name': 'Bill', 'salary': 100},
{'name': 'Bill', 'salary': 76},
{'name': 'Ted', 'salary': 67}]
# employees.sort(key=lambda x: (x['name'], x['salary'])) # this will not work, need to negate the salary to make it work
employees.sort(key=lambda x: (x['name'], -x['salary']))
print(employees)