เมื่อจำเป็นต้องตรวจสอบว่ามีค่าเฉพาะที่สอดคล้องกับคีย์ '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 จะถูกค้นหาในรายการพจนานุกรม
-
สิ่งนี้ถูกกำหนดให้กับตัวแปร
-
ขึ้นอยู่กับว่าตัวแปรนี้เป็น "จริง" หรือ "เท็จ" ข้อความที่เกี่ยวข้องจะแสดงบนคอนโซล