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",".")
我需要有两个 yield 语句,但只能让其中一个工作。
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]