Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. Python – เรียงลำดับตามการรวมช่วง

    เมื่อต้องการเรียงลำดับรายการตามช่วง จะใช้เมธอด abs(), sum() และ List Comprehension ภายในฟังก์ชัน ตัวอย่าง i and element[0] i and element[1] < j]) my_list = [[(12, 4), (55, 10), (11, 16)], [(42, 14)], [(2, 5), (2, 28), (9, 16)], [(12, 6), (6, 13)]] print(The list is :) print(my_list) i, j = 2, 15

  2. Python – ตัดทอน Tuple ด้วยค่า K

    เมื่อต้องการตัดทอน Tuple ตามค่า K จะใช้การวนลูปแบบง่ายและเมธอด append() ตัวอย่าง my_list = [(44, 3, 68, 11, 5), (68, 44, 9, 5, 8), (8, 11, 2, 68, 5), (44, 68, 2, 5, 7)] print(The list is :) print(my_list) K = 1 print(The value for K is ) print(K) my_result = [] for element in my_list: list_leng

  3. Python – เรียงลำดับแถวตามตัวคูณของ K

    เมื่อต้องการเรียงลำดับแถวตามตัวคูณของ K จะนิยามฟังก์ชันที่ใช้ List Comprehension และตัวดำเนินการ modulus (เศษ) ตัวอย่าง def multiple_sort_val(row): return len([ele for ele in row if ele % K == 0]) my_list = [[11, 44, 7, 11], [7, 5, 44, 11], [11, 6, 35, 44], [92, 92, 5]] print(The list is :) pr

  4. Python – กรองแถวที่จัดเรียงแล้ว

    เมื่อต้องการกรองแถวที่จัดเรียงแล้ว จะใช้ List Comprehension ร่วมกับเมธอด sorted() และ list() ตัวอย่าง my_list = [[99, 6, 75, 10], [1, 75, 2, 4, 99], [75, 15, 99, 2], [1, 4, 15, 99]] print(The list is :) print(my_list) my_result = [sub for sub in my_list if sub == list(sorted(sub)) or sub == list(so

  5. Python – หา K สมาชิกกลางของลิสต์

    เมื่อต้องการหา K สมาชิกกลาง (middle elements) ของลิสต์ สามารถใช้ตัวดำเนินการหารจำนวนเต็ม // และ list slicing ได้ ตัวอย่าง my_list = [34, 56, 12, 67, 88, 99, 0, 1, 21, 11] print(The list is : ) print(my_list) K = 5 print(The value of K is ) print(K) beg_indx = (len(my_list) // 2) - (K // 2) end_indx

  6. Python – จัดกลุ่มสมาชิกติดกันตามเครื่องหมาย (Sign)

    เมื่อต้องการจัดกลุ่มสมาชิกติดกันในลิสต์ตามเครื่องหมาย (บวก/ลบ) สามารถใช้ตัวดำเนินการ XOR ^ ร่วมกับ enumerate() ได้ ตัวอย่าง my_list = [15, -33, 12, 64, 36, -12, -31, -17, -49, 12, 43, 30, -23, -35, 53] print(The list is :) print(my_list) my_result = [[]] for (index, element) in enumerate(my_list):

  7. Python – ดึงแถวที่มีชนิดข้อมูลเฉพาะ

    เมื่อต้องการดึงแถว (สับลิสต์) ที่สมาชิกทุกตัวเป็นชนิดข้อมูลที่กำหนด (เช่น int) สามารถใช้ list comprehension, isinstance() และ all() ได้ ตัวอย่าง my_list = [[14, 35, Will], [12, 26, 17], [p, y, t], [29, 40, 21]] print(The list is :) print(my_list) my_data_type = int my_result = [row for row in my_lis

  8. Python – กรองแถวที่เป็น Immutable เพื่อเป็น Dictionary Keys จากเมทริกซ์

    เมื่อต้องการกรองแถวในเมทริกซ์ที่สมาชิกทุกตัวเป็น immutable type (เหมาะเป็น dictionary keys) สามารถใช้ list comprehension และ isinstance() ได้ ตัวอย่าง my_list = [[24, 15, [32, 33, 12]], [pyt, 8, (14, 54)], [{15: 24}, 13, fun], [True, cool]] print(The list is :) print(my_list) my_result = [row for ro

  9. Python – การหารติดต่อกันในลิสต์ (Consecutive Division)

    เมื่อต้องการทำการหารติดต่อกัน (consecutive division) ของสมาชิกในลิสต์ คือ สมาชิกแรกหารด้วยสมาชิกที่สอง แล้วผลลัพธ์หารด้วยตัวที่สาม ต่อเนื่องไป สามารถใช้ฟังก์ชันที่มีลูปและตัวดำเนินการ / ได้ ตัวอย่าง def consec_division(my_list): my_result = my_list[0] for idx in range(1, len(my_list)):

  10. Python – นับจำนวนยอด (Peaks) และหุบเขา (Valleys) ในลิสต์

    เมื่อต้องการนับจำนวนยอด (peak: ตัวกลางน้อยกว่าทั้งสองข้าง) และหุบเขา (valley: ตัวกลางมากกว่าทั้งสองข้าง) ในลิสต์ สามารถใช้ลูป iteration และเงื่อนไขเปรียบเทียบได้ ตัวอย่าง my_list[index] < my_list[index - 1] or my_list[index + 1] my_list[index - 1]: my_result += 1 print(The result is :) pri

  11. Python – พิมพ์แถวที่มีสมาชิกตัวเดียวกันที่ดัชนีที่กำหนด

    เมื่อต้องการพิมพ์แถวจากเมทริกซ์ที่มีตัวเลขตัวเดียวกันที่ดัชนีที่กำหนด (ของตัวเลขแรกในแถว) สามารถใช้ list comprehension และ all() ได้ ตัวอย่าง my_list = [[7745, 6755, 87, 978], [727, 927, 845], [192, 997, 49], [98, 74, 27]] print(The list is :) print(my_list) my_key = 1 print(The key is ) print(my_ke

  12. Python – ดึงสมาชิกจากลิสต์สตริงเฉพาะตัวอักษรที่กำหนด

    เมื่อต้องการดึงเฉพาะตัวอักษรที่กำหนดจากแต่ละสตริงในลิสต์ สามารถใช้ list comprehension และ join() ได้ ตัวอย่าง my_list = [hi, is, great, pyn, pyt] print(The list is :) print(my_list) my_key = n print(The value for key is ) print(my_key) my_result = [] for sub in my_list: my_result.append(.join([e

  13. Python – ทดสอบการสร้างคำจากลิสต์ตัวอักษร

    เมื่อต้องการทดสอบว่าสามารถสร้างคำ (word) จากลิสต์ตัวอักษรที่มีอยู่ได้หรือไม่ (พิจารณาจำนวนตัวอักษร) สามารถใช้ all() และ count() ได้ ตัวอย่าง my_list = [p, p, y, t, h, p, p, y, n, y, y, t] print(The list is :) print(my_list) key = pyt print(The key is :) print(key) my_result = all(key.count(chr) <= m

  14. Python – หาผลรวมหลักคู่และคี่ของตัวเลขในลิสต์

    เมื่อต้องการหาผลรวมของหลักตัวเลขคู่และคี่แยกกันจากทุกตัวเลขในลิสต์ สามารถใช้ลูป iteration และตัวดำเนินการ modulus ได้ ตัวอย่าง my_list = [369, 793, 2848, 4314, 57467] print(The list is :) print(my_list) sum_odd = 0 sum_even = 0 for index in my_list: for element in str(index): if int(elem

  15. Python – ทดสอบว่าความยาวแถวเพิ่มขึ้นตามลำดับ

    เมื่อต้องการทดสอบว่าความยาวของแถว (สับลิสต์) ในเมทริกซ์เพิ่มขึ้นตามลำดับหรือไม่ สามารถใช้ลูป iteration และค่า Boolean ได้ ตัวอย่าง my_list = [[55], [12, 17], [25, 32, 24], [58, 36, 57, 19, 14]] print(The list is :) print(my_list) my_result = True for index in range(len(my_list) - 1): if len(my_l

  16. Python – แปลงสมาชิกในลิสต์ Tuple ให้เป็น Float

    เมื่อต้องการแปลงสมาชิกในลิสต์ของ tuple ให้เป็น float ถ้าเป็นตัวเลข คงไว้ถ้าเป็นสตริงอักษร สามารถใช้ isalpha(), float() และลูป iteration ได้ ตัวอย่าง my_list = [(31, py), (22, 226.65), (18.12, 17), (pyt, 12)] print(The list is :) print(my_list) my_result = [] for index in my_list: my_temp = []

  17. Python – หาความถี่ของตัวเลขที่มากกว่าหรือเท่ากับแต่ละสมาชิก

    เมื่อต้องการหาความถี่ของตัวเลขในลิสต์ที่มากกว่าหรือเท่ากับแต่ละสมาชิก สามารถใช้ list comprehension และ sum() ได้ ตัวอย่าง my_list = [24, 13, 72, 22, 12, 47] print(The list is :) print(my_list) my_result = [sum(1 for element in my_list if index <= element) for index in my_list] print(The result is :)

  18. Python – ลบ Dictionary ที่มีค่า Key ตรงกัน

    เมื่อต้องการลบ dictionary ออกจากลิสต์ถ้าค่าของ key ที่กำหนด (K) ตรงกับ dictionary ในลิสต์อีกอันหนึ่ง สามารถใช้ dictionary comprehension ได้ ตัวอย่าง my_dict_1 = [{Hi: 32, there: 32, Will: 19}, {Hi: 19, there: 100, Will: 13}, {Hi: 72, there: 19, Will: 72}] print(The first dictionary is : ) print(my_d

  19. Python – เรียงลำดับลิสต์ Dictionary ตามผลรวมค่า Value

    เมื่อต้องการเรียงลำดับลิสต์ของ dictionary ตามผลรวมของค่า (values) ในแต่ละ dictionary สามารถใช้ฟังก์ชันช่วยที่มี sum() และ .values() ได้ ตัวอย่าง def sum_value(row): return sum(list(row.values())) my_dict = [{21: 13, 44: 35, 34: 56}, {11: 75, 70: 19, 39: 70}, {1: 155}, {48: 29, 17: 53}] print(Th

  20. Python – ดึงตัวเลขที่มีหลักเฉพาะตามที่กำหนดจากลิสต์

    เมื่อต้องการดึงตัวเลขจากลิสต์ที่หลักตัวเลขทุกหลักอยู่ในลิสต์ที่กำหนด (digit_list) สามารถใช้ list comprehension และ all() ได้ ตัวอย่าง my_list = [3345, 2345, 1698, 2475, 1932] print(The list is :) print(my_list) digit_list = [2, 3, 5, 4] my_result = [index for index in my_list if all(int(element) in

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:325/451  20-คอมพิวเตอร์/Page Goto:1 319 320 321 322 323 324 325 326 327 328 329 330 331