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

Python – K 키에 해당하는 특정 값이 있는지 확인

<시간/>

키 'K'에 해당하는 특정 값이 있는지 확인해야 하는 경우 목록 이해를 사용합니다.

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

예시

my_list = [{"python" : "14", "is" : "great", "fun" : "1`"},{"python" : "cool", "is" : "fun", "best" : "81"},{"python" : "93", "is" : "CS", "amazing" : "16"}]

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

K = "python"
print("The value of K is ")
print(K)

value = "cool"

my_result = value in [index[K] for index in my_list]

print("The result is :")

if(my_result == True):
   print("The value is present in with respect to key ")
else:
   print("The value isn't present with respect to key")

출력

The list is :
[{'python': '14', 'is': 'great', 'fun': '1`'}, {'python': 'cool', 'is': 'fun', 'best': '81'}, {'python': '93', 'is': 'CS', 'amazing': '16'}]
The value of K is
python
The result is :
The value is present in with respect to key

설명

  • 사전 요소 목록이 정의되어 콘솔에 표시됩니다.

  • K 값이 정의되어 콘솔에 표시됩니다.

  • 다른 문자열이 정의되었습니다.

  • 목록은 목록 이해를 사용하여 반복되고 사전 목록에서 K 값의 인덱스가 검색됩니다.

  • 이것은 변수에 할당됩니다.

  • 이 변수가 'True'인지 'False'인지에 따라 콘솔에 해당 메시지가 표시됩니다.