- くだすれPython(超初心者用) その27©2ch.net
608 :デフォルトの名無しさん[sage]:2015/10/19(月) 20:33:59.18 ID:CabTOv+a - >>602
いくつか引き算の定義が考えられるけど、fooがあるとする >>> foo = {"a": 1, "b": 2} 1.指定したキーを取り除く >>> bar = {"b"} >>> {key: value for key, value in foo.items() if key not in bar} {'a': 1} 2.指定した値を取り除く >>> bar = {2} >>> {key: value for key, value in foo.items() if value not in bar} {'a': 1} 3.キーと値が一致するものを取り除く >>> bar = {"a": 1000, "b": 2} >>> dict(item for item in foo.items() if item not in bar.items()) {'a': 1}
| - くだすれPython(超初心者用) その27©2ch.net
610 :デフォルトの名無しさん[sage]:2015/10/19(月) 21:12:23.56 ID:CabTOv+a - >>606
高階関数を定義する funcへ渡す関数をlambdaではなくdefで定義すれば複雑な処理もできる def f(func, a, ns): b = a[ns[0]] for i in ns[1:]: b = b[i] return func(b) >>> a = [[[0], [1], [2]], [[3], [4], [5]]] >>> f(lambda x: print("result =", x), a, [0]) result = [[0], [1], [2]] >>> f(lambda x: print("result =", x), a, [1, 2]) result = [5]
|
|