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

    เมื่อต้องการหาค่าเฉลี่ยจำนวนหลักตัวเลข (average digit count) ในลิสต์ สามารถใช้ลูป iteration, str() และตัวดำเนินการ / ได้ ตัวอย่าง my_list = [324, 5345, 243, 746, 432, 463, 946787] print(The list is :) print(my_list) sum_digits = 0 for ele in my_list: sum_digits += len(str(ele)) my_result = sum_d

  2. Python – การจัดหมู่ทั้งหมดของลิสต์ Dictionary

    เมื่อต้องการสร้างการจัดหมู่ (combinations) ทั้งหมดของลิสต์ dictionary จากสองลิสต์ สามารถใช้ list comprehension, zip() และ itertools.product() ได้ ตัวอย่าง from itertools import product my_list_1 = [python, is, fun] my_list_2 = [24, 15] print(The first list is :) print(my_list_1) print(The second lis

  3. Python – เรียงลำดับเมทริกซ์ตามค่ามัธยฐานแถว

    เมื่อต้องการเรียงลำดับเมทริกซ์ตามค่ามัธยฐาน (median) ของแต่ละแถว สามารถใช้ฟังก์ชันช่วยที่มี statistics.median() ได้ ตัวอย่าง from statistics import median def median_row(row): return median(row) my_list = [[43, 14, 27], [13, 27, 24], [32, 56, 18], [34, 62, 55]] print(The list is :) print(my_lis

  4. Python – เรียงลำดับสตริงตามจำนวนเครื่องหมายวรรคตอน

    เมื่อต้องการเรียงลำดับสตริงตามจำนวนเครื่องหมายวรรคตอน (punctuation) สามารถใช้ฟังก์ชันช่วยที่มี list comprehension และ string.punctuation ได้ ตัวอย่าง from string import punctuation def get_punctuation_count(my_str): return len([element for element in my_str if element in punctuation]) my_list =

  5. Python – กรองแถวที่มีสมาชิกครบทุกค่าในช่วง

    เมื่อต้องการกรองแถว (สับลิสต์) ที่มีสมาชิกครอบคลุมทุกค่าในช่วงที่กำหนด สามารถใช้ list comprehension และ all() ได้ ตัวอย่าง my_list = [[3, 2, 4, 5, 10], [32, 12, 4, 51, 10], [12, 53, 11], [2, 3, 31, 5, 8, 7]] print(The list is :) print(my_list) i, j = 2, 5 my_result = [index for index in my_list if a

  6. Python – คำนวณเปอร์เซ็นต์สมาชิกบวกในลิสต์

    เมื่อต้องการคำนวณเปอร์เซ็นต์ของสมาชิกที่เป็นบวกในลิสต์ สามารถใช้ list comprehension และ len() ได้ ตัวอย่าง 0]) / len(my_list)) * 100 print(The result is :) print(my_result) ผลลัพธ์ The list is : [14, 62, -22, 13, -87, 0, -21, 81, 29, 31] The result is : 60.0 คำอธิบาย กำหนดลิสต์ตัวเลขที่มีทั้งบวก ล

  7. Python – เรียงลำดับตามหลักตัวเลขสูงสุดในสมาชิก

    เมื่อต้องการเรียงลำดับลิสต์ตัวเลขตามหลักตัวเลขสูงสุด (maximum digit) ในแต่ละตัวเลข สามารถใช้ฟังก์ชันช่วยที่มี str() และ max() ได้ ตัวอย่าง def max_digits(element): return max(str(element)) my_list = [224, 192, 145, 18, 3721] print(The list is :) print(my_list) my_list.sort(key=max_digits) print

  8. Python – เรียงลำดับเมทริกซ์ตามผลรวมของแถว

    เมื่อต้องการเรียงลำดับเมทริกซ์ (ลิสต์ของลิสต์) ตามผลรวมของแต่ละแถว สามารถกำหนดฟังก์ชันช่วยที่ใช้ sum() ได้ ตัวอย่าง def sort_sum(row): return sum(row) my_list = [[34, 51], [32, 15, 67], [12, 41], [54, 36, 22]] print(The list is :) print(my_list) my_list.sort(key=sort_sum) print(The result is :)

  9. Python – ลบ Tuple ที่มีสมาชิกเป็น None ทั้งหมด

    เมื่อต้องการลบ tuple ที่มีสมาชิกเป็น None ทุกตัวออกจากลิสต์ สามารถใช้ list comprehension และ all() ได้ ตัวอย่าง my_tuple = [(None, 12), (None, None), (33, 54), (32, 13), (None,)] print(The tuple is :) print(my_tuple) my_result = [index for index in my_tuple if not all(element == None for element in

  10. Python – รวมลิสต์กับสมาชิกของอีกลิสต์หนึ่ง

    เมื่อต้องการรวมลิสต์หนึ่งกับสมาชิกของอีกลิสต์หนึ่ง (ต่อท้าย) สามารถใช้ลูป iteration และ append() ได้ ตัวอย่าง my_list_1 = [12, 14, 25, 36, 15] print(The first list is :) print(my_list_1) my_list_2 = [23, 15, 47, 12, 25] print(The second list is :) print(my_list_2) for element in my_list_2: my_li

  11. Python – ดึง K หลักตัวเลขท้ายจากตัวเลขในลิสต์

    เมื่อต้องการดึง K หลักตัวเลขท้าย (rear K digits) จากตัวเลขทุกตัวในลิสต์ สามารถใช้ list comprehension, modulo และยกกำลัง ** ได้ ตัวอย่าง my_list = [51645, 24567, 36743, 89452, 2122] print(The list is :) print(my_list) K = 3 print(The value of K is ) print(K) my_result = [element % (10 ** K) for eleme

  12. Python – ผลรวมสลับคู่ของ Dual Tuple

    เมื่อต้องการหาผลรวมแบบสลับคู่ (alternate summation) ของลิสต์ tuple คู่ สามารถใช้ลูป iteration และตัวดำเนินการ modulus ได้ ตัวอย่าง my_list = [(24, 11), (45, 66), (53, 52), (77, 51), (31, 10)] print(The list is :) print(my_list) my_result = 0 for index in range(len(my_list)): if index % 2 == 0:

  13. Python – ดึงสมาชิกที่หลักตัวเลขเรียงเพิ่มขึ้น

    เมื่อต้องการดึงสมาชิกจากลิสต์ที่หลักตัวเลขเรียงลำดับเพิ่มขึ้น (strictly increasing) สามารถใช้ลูป iteration, ค่า flag และ str() ได้ ตัวอย่าง my_list = [4578, 7327, 113, 3467, 1858] print(The list is :) print(my_list) my_result = [] for element in my_list: my_flag = True for index in range(len

  14. Pandas GroupBy – นับจำนวนการเกิดของแต่ละการรวมกัน

    เพื่อจัดกลุ่มคอลัมน์และนับจำนวนการเกิดของแต่ละการรวมกัน (combination) ใน Pandas ใช้ DataFrame.groupby() ร่วมกับ size() เมธอด groupby() แยก DataFrame ออกเป็นกลุ่มๆ ขั้นตอน สร้างข้อมูลสองมิติ df1 พิมพ์ DataFrame ต้นทาง df1 สร้างข้อมูลอีกชุด df2 พิมพ์ DataFrame ต้นทาง df2 หาค่าที่ซ้ำกันด้วยเมธอด merge

  15. Python – เรียงลำดับลิสต์สตริงตามความถี่ของตัวอักษร K

    เมื่อต้องการเรียงลำดับลิสต์สตริงตามความถี่ของตัวอักษร K ที่กำหนด สามารถใช้ sorted() และ lambda function ได้ ตัวอย่าง my_list = [Hi, Will, Jack, Python, Bill, Mills, goodwill] print(The list is : ) print(my_list) my_list.sort() print(The list after sorting is ) print(my_list) K = l print(The value of

  16. Python – กลับค่า Dictionary เป็นลิสต์ของ Key (Inverse Dictionary)

    เมื่อต้องการกลับค่า (inverse) ของ dictionary ที่มี value เป็นลิสต์ ให้กลายเป็น dictionary ใหม่ที่ key คือค่าเดิม และ value เป็นลิสต์ของ key เดิม สามารถใช้ defaultdict ได้ ตัวอย่าง from collections import defaultdict my_dict = {13: [12, 23], 22: [31], 34: [21], 44: [52, 31]} print(The dictionary is :

  17. Python – หาลำดับฟีบอนัชชีด้วยการเรียกซ้ำ (Recursion)

    เมื่อต้องการหาลำดับฟีบอนัชชี (Fibonacci sequence) โดยใช้วิธีการเรียกซ้ำ (recursion) สามารถกำหนดฟังก์ชันที่เรียกตัวเองได้ ตัวอย่าง def fibonacci_recursion(my_val): if my_val <= 1: return my_val else: return fibonacci_recursion(my_val - 1) + fibonacci_recursion(my_val - 2) num

  18. Python – ทำซ้ำสมาชิกที่ดัชนีที่กำหนด

    เมื่อต้องการทำซ้ำสมาชิกที่ดัชนีที่กำหนดในลิสต์ (เพิ่มเข้าไปอีกหนึ่งตัว) สามารถใช้ enumerate(), extend() และ append() ได้ ตัวอย่าง my_list = [34, 56, 77, 23, 31, 29, 62, 99] print(The list is :) print(my_list) index_list = [3, 1, 4, 6] my_result = [] for index, element in enumerate(my_list): if i

  19. Python – พิมพ์แถวที่ความถี่ทุกสมาชิกมากกว่า K

    เมื่อต้องการพิมพ์แถว (สับลิสต์) ที่ความถี่ของทุกสมาชิกในแถวนั้นมากกว่า K สามารถใช้ฟังก์ชันช่วยที่มี all() และ count() ได้ ตัวอย่าง K for element in row) my_list = [[11, 11, 32, 43, 12, 23], [42, 14, 55, 62, 16], [11, 11, 11, 11], [42, 54, 61, 18]] print(The tuple is :) print(my_list) K = 1 print(T

  20. Python – ดึงคำที่ขึ้นต้นด้วยสระจากลิสต์

    เมื่อต้องการดึงคำที่ขึ้นต้นด้วยสระ (a, e, i, o, u) จากลิสต์ สามารถใช้ลูป iteration, ค่า flag และ startswith() ได้ ตัวอย่าง my_list = [abc, phy, and, okay, educate, learn, code] print(The list is :) print(my_list) my_result = [] my_vowel = aeiou print(The vowels are ) print(my_vowel) for index in my_

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