yield 사용법
# 리스트를 만들어서 던짐 def squareList(nums): result = [] for n in nums: result.append(n * n) return result myNums = squareList([1, 2, 3, 4, 5]) print(myNums) # 결과[1, 4, 9, 16, 25] # yield를 사용해 객체를 던짐 def squareList(nums): for n in nums: yield (n * n) myNums = squareList([1, 2, 3, 4, 5]) for n in myNums: print(n, end = ' ') # 결과1 4 9 16 25 # flatten listnested_ls = [[1, 2], [3, 4], [5]] def flatten(nest..
Python
2024. 12. 1. 20:21