Practical Programming
An Introduction to Computer Science
Using Python 3
(Pragmatic Programmers)
(Pragmatic Bookshelf)
Paul Gries (著) Jennifer Campbell (著)
Jason Montojo (著) Lynn Beighley (編集)
開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python 3.4 (プログラミング言語)
Practical Programming: An Introduction to Computer Science Using Python 3 (Pragmatic Programmers) (Paul Gries (著)、Jennifer Campbell (著)、Jason Montojo (著)、Lynn Beighley (編集)、Pragmatic Bookshelf)のChapter 11(Storing Data Using Other Collection Types)、11.8(Exercises) 8.を解いてみる。
11.8(Exercises) 8.
コード(BBEdit)
sample8.py
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
def dictIntersect(d1, d2):
result = {}
for k, v in d1.items():
if k in d2 and v == d2[k]:
result[k] = v
return result
if __name__ == '__main__':
d1 = dict(a=1, b=2, c=3, d=4)
d2 = dict(a=2, b=2, e=3, d=4)
print(dictIntersect(d1, d2))
入出力結果(Terminal, IPython)
$ ./sample8.py
{'d': 4, 'b': 2}
$
0 コメント:
コメントを投稿