-
[Python/Tip] 컬렉션 순환 중 컬렉션 객체 변경코딩/Python 2022. 12. 19. 14:35728x90
컬렉션을 순환시키면서 특정 조건에서 컬렉션의 원소를 제거하면 순환시쿼스에 문제가 생기기 때문에, 원시 컬렉션과 별개로 순환을 위한 컬렉션을 생성시켜 순환하는 경우가 있다.
collection.copy()를 사용해서 새로운 컬렉션을 생성시키지 않고 원시컬렉션의 객체를 제거할 수 있다.users = { 'A': 'active', 'B': 'inactive', 'C': 'active'} for user, status in users.copy().items(): if status == 'inactive': del users[user]728x90'코딩 > Python' 카테고리의 다른 글
[Python/Turtle] 3. Methods/Turtle state & Settings for measurement (0) 2022.12.19 [Python/Turtle] 2. Methods/Turtle motion, Pen control, More drawing control, Using events, Compound shapes (0) 2022.12.19 [Python/Turtle] 1. 개요 (0) 2022.12.19 [Python] Webscrapping (0) 2022.12.18 Python/mac/Exif data 수정 (0) 2022.11.22