なし
def print_args(*args):
"当たられた位置指定引数を全て出力する"
for i, val in enumerate(args):
print("args[{:3d}] = {:s}]".format(i, str(val)))
print_args('hello', (1, 2, 3), [], {'key' : 'val'}, None)
args[ 0] = hello] args[ 1] = (1, 2, 3)] args[ 2] = []] args[ 3] = {'key': 'val'}] args[ 4] = None]
def unique(x):
"与えられたlistから重複した要素を除いたlistを返す"
y = []
for xx in x:
if xx not in y: # 既に要素があるかどうかチェック
y.append(xx)
return y
unique(['a', 1, 'b', 2, 'c', 1, 2, 3, 'b', 'd', 'a', 3])
['a', 1, 'b', 2, 'c', 3, 'd']
def process_kwargs(**kwargs):
"キーワード引数で与えられた引数を全て出力する"
# 必須の引数aとbをtupleで指定
required_keys = 'a', 'b'
for key in required_keys:
if key not in kwargs:
kwargs[key] = None
# キーでソートする
keys = list(kwargs.keys())
keys.sort()
for key in keys:
print(key, kwargs[key])
process_kwargs(a='hi', c='chao', x=10, y=20, z=30)
a hi b None c chao x 10 y 20 z 30
# JSON文字列
json_string = \
"""
[
{
"age": 28,
"name": {
"last": "Morales",
"first": "Hickman"
},
"email": "hickman.morales@electonic.cg",
"index": 0,
"company": "Electonic",
"favoriteFruit": "apple"
},
{
"age": 59,
"name": {
"last": "Norris",
"first": "Emerson"
},
"email": "emerson.norris@recrisys.sj",
"index": 1,
"company": "Recrisys",
"favoriteFruit": "strawberry"
},
{
"age": 47,
"name": {
"last": "Garza",
"first": "Shelly"
},
"email": "shelly.garza@signity.mw",
"index": 2,
"company": "Signity",
"favoriteFruit": "orange"
},
{
"age": 30,
"name": {
"last": "Owens",
"first": "Rhoda"
},
"email": "rhoda.owens@kidgrease.cn",
"index": 3,
"company": "Kidgrease",
"favoriteFruit": "strawberry"
},
{
"age": 18,
"name": {
"last": "Dodson",
"first": "Florence"
},
"email": "florence.dodson@musanpoly.cy",
"index": 4,
"company": "Musanpoly",
"favoriteFruit": "banana"
}
]
"""
# jsonモジュールを使う
import json
def process_json1(s):
"JSON文字列から名前とEmailアドレスを抽出して出力"
# 文字列からオブジェクトに変換
json_obj = json.loads(s)
#
for elem in json_obj:
# 以下念のためチェックするコードを色々いれてあるがこの課題には必要ない
if not isinstance(elem, dict):
continue
if 'name' in elem:
first = elem.get('name').get('first', '').capitalize()
last = elem.get('name').get('last', '').upper()
email = elem.get('email', '')
print('{:s} {:s} <{:s}>'.format(first, last, email))
process_json1(json_string)
Hickman MORALES <hickman.morales@electonic.cg> Emerson NORRIS <emerson.norris@recrisys.sj> Shelly GARZA <shelly.garza@signity.mw> Rhoda OWENS <rhoda.owens@kidgrease.cn> Florence DODSON <florence.dodson@musanpoly.cy>
def process_json2(s):
"JSON文字列から個人情報を削除したJSON文字列として返す"
# 削除するキーをtupleで与える
to_be_deleted = 'name', 'email'
x = []
for elem in json.loads(s):
for key in to_be_deleted:
del elem[key]
x.append(elem)
# JSON文字列に変換して返す
return json.dumps(x, indent=2)
# 返された文字列をそのまま出力
print(process_json2(json_string))
[ { "age": 28, "index": 0, "company": "Electonic", "favoriteFruit": "apple" }, { "age": 59, "index": 1, "company": "Recrisys", "favoriteFruit": "strawberry" }, { "age": 47, "index": 2, "company": "Signity", "favoriteFruit": "orange" }, { "age": 30, "index": 3, "company": "Kidgrease", "favoriteFruit": "strawberry" }, { "age": 18, "index": 4, "company": "Musanpoly", "favoriteFruit": "banana" } ]