Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python 프로그램 – 특정 값이 없으면 사전 목록에서 사전 제거

<시간/>

특정 값이 없을 때 사전 목록에서 사전을 제거해야 하는 경우 단순 반복, 'del'연산자 및 'break'문이 사용됩니다.

예시

아래는 동일한 데모입니다 -

my_list = [{"code" : 1, "fun" : "learn"},
   {"code" : 2, "fun" : "code"},
   {"code" : 3, "fun" : "test"},
   {"code" : 4, "fun" : "teach"},
   {"code" : 4, "fun" : "make"},
   {"code" : 4, "fun" : "object"}]

print("The list of dictionary is : " )
print(my_list)

for index in range(len(my_list)):
   if my_list[index]['code'] == 3:
      del my_list[index]
      break

print("The resultant list is : ")
print(my_list)
입니다.

출력

The list of dictionary is :
[{'code': 1, 'fun': 'learn'}, {'code': 2, 'fun': 'code'}, {'code': 3, 'fun': 'test'}, {'code': 4, 'fun': 'teach'},
{'code': 4, 'fun': 'make'}, {'code': 4, 'fun': 'object'}]
The resultant list is :
[{'code': 1, 'fun': 'learn'}, {'code': 2, 'fun': 'code'}, {'code': 4, 'fun': 'teach'}, {'code': 4, 'fun': 'make'},
{'code': 4, 'fun': 'object'}]

설명

  • 문자열 목록이 정의되고 콘솔에 표시됩니다.

  • 목록이 반복되고 특정 인덱스가 정수와 같은지 확인됩니다.

  • 결과에 할당됩니다.

  • 이것은 콘솔에 출력으로 표시됩니다.