Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. โปรแกรม Python หาอัตราความซ้ำ (Redundancy Rate) ของแต่ละแถวในแมทริกซ์

    ตัวอย่าง โค้ดด้านล่างคำนวณ redundancy rate ของแต่ละแถว = 1 - (จำนวนค่าที่ไม่ซ้ำ / จำนวนค่าทั้งหมด) โดยใช้ set my_list = [[91, 52, 12, 29, 33], [54, 54, 54, 54, 54], [11, 22, 33, 59, 95]] print(The list is :) print(my_list) my_result = [] for sub in my_list: my_result.append(1 - len(set(sub)) /

  2. โปรแกรม Python หาสมาชิกถัดไปที่ใกล้ที่สุดในแมทริกซ์ (Next Nearest Element in Matrix)

    ตัวอย่าง โค้ดด้านล่างกำหนดฟังก์ชัน get_nearest_elem ค้นหาค่า my_key ในแถวตั้งแต่แถว x ตำแหน่ง y+1 เป็นต้นไป คืนค่าดัชนี (row, col) ที่เจอแรก y: return index + x, j return -1, -1 my_list = [[21, 32, 11, 22, 13], [91, 52, 31, 26, 33], [81, 52, 3, 22, 3], [11, 92, 83, 4, 9]] prin

  3. โปรแกรม Python คืนค่าแถวที่มีสมาชิกเท่ากันในดัชนีที่กำหนด

    ตัวอย่าง โค้ดด้านล่างเปรียบเทียบสองลิสต์ซ้อน หาแถวที่มีค่าเท่ากันในดัชนี my_key (0) แล้วเพิ่มแถวจากลิสต์แรกเข้า result (เพิ่มซ้ำ 2 ครั้งตามโค้ดต้นฉบับ) my_list_1 = [[21, 81, 35], [91, 14, 0], [64, 61, 42]] my_list_2 = [[21, 92, 63], [80, 19, 65], [54, 65, 36]] print(The first list is :) print(my_li

  4. โปรแกรม Python แปลงแมทริกซ์เป็นสตริง (Convert Matrix to String)

    ตัวอย่าง โค้ดด้านล่างแปลงลิสต์ซ้อน (matrix) เป็นสตริงเดียว โดยคั่นสมาชิกในแถวด้วย comma และคั่นแถวด้วย space my_list = [[1, 23, python], [1, is, 24], [fun, 97, 5]] print(The list is :) print(my_list) in_delete, out_delete = ,, my_result = out_delete.join([in_delete.join([str(element) for element

  5. Python - นับจำนวนคู่ที่เท่ากัน (Equal Pairs) ในลิสต์

    ตัวอย่าง โค้ดด้านล่างนับจำนวนคู่ของค่าที่ซ้ำกันในลิสต์ (แต่ละคู่ต้องมี 2 ค่าเท่ากัน) โดยใช้ set และการหารจำนวนเต็ม my_list = [34, 56, 12, 32, 78, 99, 67, 34, 52, 78, 99, 10, 0, 11, 23, 9] print(The list is :) print(my_list) all_elems = set(my_list) my_result = 0 for elements in all_elems: my_re

  6. Python - ตรวจสอบว่าลิสต์ประกอบด้วยเฉพาะหลักที่กำหนด (Check if List Contains Particular Digits)

    ตัวอย่าง โค้ดด้านล่างตรวจสอบว่าทุกหลักของทุกตัวเลขในลิสต์มาจากชุดหลักที่กำหนด (1,4,5,3) โดยแปลงเป็นสตริงและใช้ join my_list = [415, 133, 145, 451, 154] print(The list is :) print(my_list) my_digits = [1, 4, 5, 3] digit_string = .join([str(ele) for ele in my_digits]) all_elems = .join([str(ele) for

  7. Python - หาดัชนีเริ่มต้นของทุกลิสต์ซ้อน (Starting Index of All Nested Lists)

    ตัวอย่าง โค้ดด้านล่างคำนวณดัชนีเริ่มต้นของแต่ละ sublist หากนำมาต่อกันเป็นลิสต์แบน (flattened) โดยสะสมความยาว my_list = [[51], [91, 22, 36, 44], [25, 25], [46, 67, 78, 82, 69, 29], [7, 5]] print(The list is :) print(my_list) my_result = [] my_len = 0 for sub in my_list: my_result.append(my_len)

  8. Python - จำกัด Tuple ตามความถี่ของค่าแรก (Restrict Tuples by First Element Frequency)

    ตัวอย่าง โค้ดด้านล่างกรอง tuple ในลิสต์ ให้เก็บเฉพาะ tuple แรกของแต่ละค่าแรก (first element) โดยใช้ dictionary นับความถี่ my_list = [(21, 24), (13, 42), (11, 23), (32, 43), (25, 56), (73, 84), (91, 40), (40, 83), (13, 27)] print(The list is :) print(my_list) my_key = 1 my_result = [] mems = dict()

  9. โปรแกรม Python หาดัชนีที่เครื่องหมายเปลี่ยน (Sign Change) ในลิสต์

    ตัวอย่าง โค้ดด้านล่างหาดัชนีที่เครื่องหมายของสมาชิกเปลี่ยนจากบวกเป็นลบหรือลบเป็นบวก (หมายเหตุ: โค้ดต้นฉบับมีเงื่อนไขผิดพลาดเล็กน้อย ตรงส่วนที่สองตรวจสอบ my_list[index] < 0 and my_list[index+1] < 0 ซึ่งไม่ใช่การเปลี่ยนเครื่องหมาย แต่นำเสนอตามต้นฉบับ) 0 and my_list[index + 1] < 0 or my_list[index] <

  10. Python - แปลงลิสต์เป็นลิสต์ซ้อนทับซ้อน (Overlapping Nested List) ด้วยขนาดและขั้นตอนกำหนด

    ตัวอย่าง โค้ดด้านล่างแบ่งลิสต์เป็น sublist ขนาด my_size โดยเลื่อนทีละ my_step (อาจทับซ้อนกันได้) my_list = [31, 25, 36, 76, 73, 89, 91, 100] print(The list is :) print(my_list) my_step, my_size = 3, 4 my_result = [] for index in range(0, len(my_list), my_step): my_result.append(my_list[index: i

  11. Python - สร้างดัชนี (Index Directory) ของสมาชิกในลิสต์

    ตัวอย่าง โค้ดด้านล่างสร้าง dictionary ที่ key คือค่าที่ไม่ซ้ำในลิสต์ และ value คือลิสต์ของดัชนีที่ค่านั้นปรากฏ โดยใช้ dictionary comprehension และ set my_list = [81, 36, 42, 57, 68, 12, 26, 26, 38] print(The list is :) print(my_list) my_result = {key: [index for index, value in enumerate(my_list) i

  12. Python - คำนวณจำนวนค่าในคอลัมน์ของ Pandas DataFrame ด้วย count()

    ใช้เมธอด count() เพื่อนับจำนวนค่าในคอลัมน์ (ไม่นับ NaN) เริ่มต้นด้วยการ import pandas import pandas as pd สร้าง DataFrame ตัวอย่าง dataFrame1 = pd.DataFrame({ Car: [BMW, Lexus, Audi, Tesla, Bentley, Jaguar], Units: [100, 150, 110, 80, 110, 90]}) นับค่าในคอลัมน์ Units print(Count of values o

  13. โปรแกรม Python ทำเครื่องหมายสมาชิกซ้ำในลิสต์สตริง

    ตัวอย่าง โค้ดด้านล่างเพิ่มหมายเลขลำดับการปรากฏต่อท้ายสตริงที่ซ้ำกัน โดยใช้ list comprehension และ count() 1 else value for index, value in enumerate(my_list)] print(The result is :) print(my_result) ผลลัพธ์ The list is : [python, is, fun, python, is, fun, python, fun] The result is : [python1, is1

  14. Python - นับจำนวนค่าที่ไม่ซ้ำกัน (Unique Values Count) ของลิสต์

    ตัวอย่าง โค้ดด้านล่างนับจำนวนค่าที่ไม่ซ้ำกันในลิสต์โดยใช้ลิสต์ช่วยเก็บค่าที่เจอแล้ว my_list = [12, 33, 33, 54, 84, 16, 16, 16, 58] print(The list is :) print(my_list) filtered_list = [] elem_count = 0 for item in my_list: if item not in filtered_list: elem_count += 1 filtered_li

  15. โปรแกรม Python หาคู่สั่ง (Pairwise Combinations) ทั้งหมดจากลิสต์

    ตัวอย่าง โค้ดด้านล่างสร้างคู่สั่ง (ordered pairs) ของทุกสมาชิกในลิสต์ โดยที่ดัชนีต่างกัน (i != j) my_list = [15, John, 2, Will, 53, Rob] print(The list is :) print(my_list) my_result = [] for i in range(0, len(my_list)): for j in range(0, len(my_list)): if (i != j): my_resul

  16. วิธีใช้ GroupBy นับค่าใน Pandas DataFrame

    เพื่อนับจำนวนค่า (value counts) แบบจัดกลุ่ม ใช้เมธอด groupby(), size() และ unstack() ของ DataFrame ก่อนอื่นสร้าง DataFrame 3 คอลัมน์ dataFrame = pd.DataFrame({ Product Category: [Computer, Mobile Phone, Electronics, Electronics, Computer, Mobile Phone], Product Name: [Keyboard, Charger, Sma

  17. Python - หาการรวม (Combination) ที่พบบ่อยที่สุดในแมทริกซ์

    ตัวอย่าง โค้ดด้านล่างใช้ Counter และ combinations จาก itertools เพื่อหาการรวมของสมาชิกในแต่ละแถวของแมทริกซ์ที่พบบ่อยที่สุด (ทุกขนาดการรวมตั้งแต่ 2 ถึงความยาวแถว) from collections import Counter from itertools import combinations my_list = [[31, 25, 77, 82], [96, 15, 23, 32]] print(The list is :) pr

  18. Python - กรอง Dictionary ในลิสต์ตามค่าของ Key ที่ K (Kth Key)

    ตัวอย่าง โค้ดด้านล่างกรอง dictionary ในลิสต์ โดยตรวจสอบว่าค่าของ key ที่กำหนด (เช่น is) อยู่ในลิสต์ค่าที่ต้องการหรือไม่ my_list = [{Python: 2, is: 4, cool: 11}, {Python: 5, is: 1, cool: 1}, {Python: 7, is: 3, cool: 7}, {Python: 9, is: 9, cool: 8}, {Python: 4, is: 10, cool: 6}] print(The list is :)

  19. โปรแกรม Python คำนวณยกกำลังของสมาชิกลิสต์ด้วยดัชนี (Index) ของมัน

    ตัวอย่าง โค้ดด้านล่างคำนวณค่าของแต่ละสมาชิกยกกำลังด้วยดัชนีของมัน (index) โดยใช้ enumerate และ operator ** my_list = [62, 18, 12, 63, 44, 75] print(The list is :) print(my_list) my_result = [] for my_index, elem in enumerate(my_list): my_result.append(elem ** my_index) print(The result is :) pr

  20. Python - ลบช่องว่างนำหน้าและท้ายใน Pandas DataFrame ด้วย strip()

    หากต้องการลบช่องว่างนำหน้า (leading) หรือท้าย (trailing) ในคอลัมน์ของ DataFrame ให้ใช้เมธอด str.strip() ก่อนอื่นต้อง import pandas import pandas as pd สร้าง DataFrame ตัวอย่างที่มีคอลัมน์ Product Category มีช่องว่างนำหน้าและท้าย dataFrame = pd.DataFrame({ Product Category: [ Computer, Mobile P

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:339/451  20-คอมพิวเตอร์/Page Goto:1 333 334 335 336 337 338 339 340 341 342 343 344 345