mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
🏣 Clean up for arxiv
This commit is contained in:
parent
1b969e7db3
commit
41756cb10c
280 changed files with 2 additions and 11 deletions
38
book/ebook_src/python_examples/example_setdefault.py
Normal file
38
book/ebook_src/python_examples/example_setdefault.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
def usual_dict(dict_data):
|
||||
newdata = {}
|
||||
for k, v in dict_data:
|
||||
if k in newdata:
|
||||
newdata[k].append(v)
|
||||
else:
|
||||
newdata[k] = [v]
|
||||
return newdata
|
||||
|
||||
|
||||
def setdefault_dict(dict_data):
|
||||
newdata = {}
|
||||
for k, v in dict_data:
|
||||
newdata.setdefault(k, []).append(v)
|
||||
return newdata
|
||||
|
||||
|
||||
def test_setdef(module_name='this module'):
|
||||
dict_data = (('key1', 'value1'),
|
||||
('key1', 'value2'),
|
||||
('key2', 'value3'),
|
||||
('key2', 'value4'),
|
||||
('key2', 'value5'),)
|
||||
print(usual_dict(dict_data))
|
||||
print(setdefault_dict(dict_data))
|
||||
|
||||
s = 'Tests in {name} have {con}!'
|
||||
print(s.format(name=module_name, con='passed'))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_setdef()
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue