[Python]병렬 반복문 수행 예제
파이썬을 이용하여 병렬 반복문(Parallel Iteration)을 사용하는 예제를 알아보자
a = ["one", "two", "three", "four"] b = [30, 20, 15, 75] c = [5.2, 7.4, 3.6, 4.2] # 방법1 result1 = {} for x, y, z in zip(a,b,c): result1[x] = y * z print('ex1 결과 : ', result1) # 방법2 print('ex2 결과 : ', {x : y * z for x, y, z in zip(a,b,c)}) # 방법3 print('ex3 결과 : ', dict(((x, y * z) for x, y, z in zip(a,b,c)))) # 방법4 result = {} for i in range(4): result[a[i]] = b[i] * c[i] print(result) # 참고(strict args >= python 3.10) # print('참고1 : ', list(zip(range(5), range(100)))) # 길이가 다를 경우 짧은 길이에 맞게 선언 # print('참고2 : ', list(zip(range(5), range(100), strict=True))) # 예외 발생 ## 결과 ex1 결과 : {'one': 156.0, 'two': 148.0, 'three': 54.0, 'four': 315.0} ex2 결과 : {'one': 156.0, 'two': 148.0, 'three': 54.0, 'four': 315.0} ex3 결과 : {'one': 156.0, 'two': 148.0, 'three': 54.0, 'four': 315.0} {'one': 156.0, 'two': 148.0, 'three': 54.0, 'four': 315.0}
최신 댓글