Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
値のリストがあるとします:
l = [1, -1]
0など、他の値に近いリスト値を取得したい場合は、次のようにします。
min(l, key=lambda v: abs(v - 0)) # prints 1
しかし、次のようにリスト内の値の順序を変更すると:
l = [-1, 1]
-1の代わりに上記の同じ関数を呼び出すと1、正の値と負の値の両方が同じ距離にある場合に正の値を常に取得するにはどうすればよいですか?
-1
1