開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- Python 3.6 (プログラミング言語)
Head First Python (Paul Barry (著)、O'Reilly Media)のChapter 3.(Structured Data: Working with Structured Data)、Iterating Over a Dictionary with "items" の FREQUENCY COUNT MAGNETS(No. 2256) を取り組んでみる。
FREQUENCY COUNT MAGNETS(No. 2256)
コード(Emacs)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- vowels = ['a', 'e', 'i', 'o', 'u'] word = input('Provide a word to search for vowels: ') print(f'word: {word}') found = {} found['a'] = 0 found['e'] = 0 found['i'] = 0 found['o'] = 0 found['u'] = 0 for letter in word: if letter in vowels: found[letter] += 1 for k, v in sorted(found.items()): print(f'{k} was found {v} time(s).')
入出力結果(Terminal, IPython)
$ ./sample1.py < sample1.py Provide a word to search for vowels: word: #!/usr/bin/env python3 a was found 0 time(s). e was found 1 time(s). i was found 1 time(s). o was found 1 time(s). u was found 1 time(s). $ ./sample1.py < sample1.py | ./sample1.py Provide a word to search for vowels: word: Provide a word to search for vowels: word: #!/usr/bin/env python3 a was found 2 time(s). e was found 4 time(s). i was found 2 time(s). o was found 7 time(s). u was found 1 time(s). $ ./sample1.py < sample1.py | ./sample1.py | ./sample1.py Provide a word to search for vowels: word: Provide a word to search for vowels: word: Provide a word to search for vowels: word: #!/usr/bin/env python3 a was found 4 time(s). e was found 7 time(s). i was found 3 time(s). o was found 13 time(s). u was found 1 time(s). $
0 コメント:
コメントを投稿