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.
問題:
x.replace(".0",".")関数 rem0に追加するにはどうすればよいですか。
x.replace(".0",".")
2 つの yield ステートメントが必要ですが、そのうちの 1 つしか機能しません。
def rem0(data): for x in data: yield x.lstrip('0') lGrid = [] for i in rem0(grid): lGrid.append(i)
次の構成を使用すると、問題が解決します。
def rem0(data): for x in data: yield x.lstrip('0').replace(".0", ".")
とにかく結果がリストであるため、実際にはジェネレーターは必要ありません。リストを直接構築して次のように書く方が効率的です。
grid = [el.lstrip('0').replace('.0', '') for x in some_data]