เมื่อต้องการนับจำนวนตัวเลขในลิสต์ที่มีค่ามากกว่า K สามารถใช้ลูป iteration ธรรมดาได้
ตัวอย่าง
my_list = [11, 17, 25, 16, 23, 18]
print("The list is :")
print(my_list)
K = 15
print("The value of K is ")
print(K)
my_count = 0
for index in my_list:
if index > K:
my_count = my_count + 1
print("The result is :")
print(my_count)
ผลลัพธ์
The list is : [11, 17, 25, 16, 23, 18] The value of K is 15 The result is : 5
คำอธิบาย
กำหนดลิสต์ตัวเลขและค่า K แสดงผล
สร้างตัวนับ
my_count = 0วนลูปผ่านลิสต์ ถ้าสมาชิกมากกว่า K เพิ่มตัวนับ
แสดงผลจำนวนสมาชิกที่มากกว่า K