Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. Python – นับคู่สตริงที่เป็น Reverse ของกัน (Count Reverse String Pairs)

    ภาพรวม หมายเหตุ: โค้ดตัวอย่างเดิมมีปัญหา** – คำนวณ max per key ไม่ใช่ count reverse pairs** โจทย์จริง: นับคู่สตริงที่กลับกัน (เช่น abc และ cba) def count_reverse_pairs(strings): seen = set() count = 0 for s in strings: rev = s[::-1] if rev in seen: count +=

  2. Python – หาคำที่พบบ่อยที่สุด (Most Frequent Word) ใน List of Strings

    ภาพรวม จาก list ของประโยค ให้หาคำที่ออก现บ่อยที่สุดรวมทุกประโยค ใช้ defaultdict นับความถี่ ตัวอย่างโค้ด from collections import defaultdict my_list = [ python is best for coders, python is fun, python is easy to learn ] print(The list is:, my_list) word_count = defaultdict(int) for s

  3. Python – ค้นหาสตริงใน List ด้วย Operator in

    ภาพรวม ตรวจสอบว่าสตริง (key) อยู่ใน list หรือไม่ ใช้ in operator ตัวอย่างโค้ด my_list = [4, 3.0, python, is, fun] key = fun print(The list is:, my_list) print(The key is:, key) if key in my_list: print(The key is present in the list) else: print(The key is not present in the list) ผลลั

  4. Python – หาสตริงที่มีสระ (Vowels) มากที่สุดใน List

    ภาพรวม วนลูปหาสตริงที่มีจำนวนสระ (a, e, i, o, u) มากที่สุด ตัวอย่างโค้ด (แก้ไขจากต้นฉบับที่มี bug) max_vowel_count: max_vowel_count = vowel_count max_vowel_word = word print(The result is:, max_vowel_word) print(fVowel count: {max_vowel_count}) ผลลัพธ์ The list is: [this, week,

  5. Python – หาตัวอักษรที่เหมือนกันในตำแหน่งเดียวกัน (Common Characters at Same Index) ของ List of Strings

    ภาพรวม จาก list ของสตริง ให้หาตัวอักษรที่ตรงกัน ทุกสตริง ใน ตำแหน่งเดียวกัน (index เดียวกัน) ตัวอย่างโค้ด my_list = [week, seek, beek, reek, meek, peek] print(The list is:, my_list) # หาความยาวสตริงสั้นที่สุด min_length = min(len(s) for s in my_list) my_result = [] for i in range(min_length):

  6. Python – จัดกึ่งกลาง Column Headers ของ Pandas DataFrame

    ภาพรวม ปรับการจัดตำแหน่งหัวคอลัมน์ (column headers) ให้อยู่กึ่งกลาง ใช้ option display.colheader_justify ตัวอย่างโค้ด import pandas as pd dataFrame = pd.DataFrame({ Car: [BMW, Lexus, Tesla, Mustang, Mercedes, Jaguar], Reg_Price: [7000.5057, 1500, 5000.9578, 8000, 9000.75768, 6000] }) pri

  7. Python – Selective Consecutive Suffix Join (รวมสตริงต่อเนื่องที่ลงท้ายด้วย Suffix)

    ภาพรวม รวมสตริงใน list ต่อเนื่องกันจนกว่าจะเจอสตริงที่ ไม่ลงท้าย ด้วย suffix ที่กำหนด (เช่น -) ตัวอย่างโค้ด my_list = [Python-, fun, to-, code] suffix = - print(The list is:, my_list) print(The suffix is:, suffix) result = [] temp = [] for element in my_list: temp.append(element) if not

  8. Python – เรียงลำดับ List ตามความถี่ของค่า K (Sort by Frequency)

    ภาพรวม เรียงลำดับ element ใน list ตามความถี่ (frequency) จากมากไปน้อย ใช้ Counter.most_common() ตัวอย่างโค้ด from collections import Counter my_list = [34, 56, 78, 99, 99, 99, 99, 99, 12, 12, 32, 51, 15, 11, 0, 0] print(The list is:, my_list) # Counter.most_common() คืน list of (element, count)

  9. Python – แปลง Data Type ทุกคอลัมน์ใน Pandas DataFrame ด้วย astype()

    ภาพรวม ใช้ astype() แปลงชนิดข้อมูลของทุกคอลัมน์ใน DataFrame พร้อมกัน โดยส่ง string dtype หรือ dict ตัวอย่างโค้ด import pandas as pd dataFrame = pd.DataFrame({ Reg_Price: [7000.5057, 1500, 5000, 8000, 9000.75768, 6000], Units: [90, 120, 100, 150, 200, 130] }) print(DataFrame ...\n, dataFr

  10. Python – All Replacement Combinations จาก List อื่น (Combinations with Replacement Pool)

    ภาพรวม จาก list หลักขนาด k และ replacement list ให้สร้าง combination ทุกแบบของขนาด k โดยเลือกจาก pool รวม (หลัก + replacement) ตัวอย่างโค้ด from itertools import combinations my_list = [54, 98, 11] replace_list = [8, 10] # รวม list แล้วหา combinations ขนาดเท่าเดิม (3) my_result = list(combinatio

  11. Python – Extract Paired Rows (แถวที่ Element ทุกตัวมีความถี่เป็นเลขคู่)

    ภาพรวม จาก list of lists ให้คัดเฉพาะแถวที่ element ทุกตัวในแถวนั้นปรากฏจำนวนครั้งเป็นเลขคู่ (2, 4, 6...) ตัวอย่างโค้ด my_list = [ [10, 21, 34, 21, 37], [41, 41, 52, 68, 68, 41], [12, 29], [30, 30, 51, 51] ] print(The list is:, my_list) my_result = [ row for row in my_list

  12. Python – หา Peak Element แบบสลับกัน (Alternate Peak) ใน List

    ภาพรวม Peak element คือ element ที่มีค่ามากกว่าหรือเท่ากับเพื่อนบ้านทั้งสองข้าง ฟังก์ชันนี้คืน index ของ peak แรกที่เจอ (ไม่ได้หาทุก peak) ตัวอย่างโค้ด = my_array[i + 1]: return i my_list = [1, 3, 20, 4, 1, 0] list_length = len(my_list) print(The list is:, my_list) print(The result is:

  13. Python – ตรวจสอบแถวซ้ำ (Duplicate Rows) ใน Matrix ด้วย Counter

    ภาพรวม หาแถวที่ซ้ำกันใน Matrix (list of lists) โดยแปลงแต่ละแถวเป็น tuple แล้วนับความถี่ด้วย collections.Counter ตัวอย่างโค้ด 1: print(row) my_matrix = [ [1, 1, 0, 1, 0, 1], [0, 0, 1, 0, 0, 1], [1, 0, 1, 1, 0, 0], [1, 1, 0, 1, 0, 1], # ซ้ำแถวแรก [0, 0, 1, 0, 0, 1],

  14. Python – แปลง Data Type ของคอลัมน์เดียวใน Pandas DataFrame ด้วย astype()

    ภาพรวม ใช้ astype() แปลงชนิดข้อมูลของคอลัมน์เฉพาะคอลัมน์ โดยส่ง dictionary {column_name: new_type} ตัวอย่างโค้ด import pandas as pd dataFrame = pd.DataFrame({ Reg_Price: [7000.5057, 1500, 5000, 8000, 9000.75768, 6000], Units: [90, 120, 100, 150, 200, 130] }) print(DataFrame ...\n, dataFra

  15. Python Pandas – เลือกแถวจาก DataFrame โดยส่ง Row Label (loc)

    ภาพรวม ใช้ loc[] เลือกแถวตาม label (ชื่อ index) ไม่ใช่ตำแหน่งตัวเลข ตัวอย่างโค้ด import pandas as pd dataFrame = pd.DataFrame( [[10, 15], [20, 25], [30, 35]], index=[x, y, z], columns=[a, b] ) print(DataFrame...\n, dataFrame) # Select row with label z print(\nSelect rows by passing l

  16. Python Pandas – เลือกแถว (Rows) จาก DataFrame ด้วยตำแหน่งตัวเลข (Integer Location) – iloc

    ภาพรวม ใช้ iloc[] เลือกแถวตามตำแหน่งตัวเลข (0-based index) ไม่ใช่ label ตัวอย่างโค้ด import pandas as pd dataFrame = pd.DataFrame( [[10, 15], [20, 25], [30, 35]], index=[x, y, z], columns=[a, b] ) print(DataFrame...\n, dataFrame) # เลือกด้วย label (loc) print(\nSelect rows by label (

  17. Python – Index Value Repetition ใน List (ทำซ้ำ Index ตามค่า)

    ภาพรวม ทำซ้ำ 0 จำนวน 4 ครั้ง ตัวอย่างโค้ด my_list = [4, 0, 3, 1] print(The list is:, my_list) my_result = [index for index, value in enumerate(my_list) for _ in range(value)] print(The result is:, my_result) ผลลัพธ์ The list is: [4, 0, 3, 1] The result is: [0, 0, 0, 0, 2, 2, 2, 3] คำอธิบาย

  18. Python – กรองสตริงที่เป็นตัวพิมพ์ใหญ่ทั้งหมดหรือตัวพิมพ์เล็กทั้งหมด (Same Case)

    ภาพรวม กรองเฉพาะสตริงที่เป็น uppercase ทั้งหมด หรือ lowercase ทั้งหมด โดยใช้ isupper() และ islower() ร่วมกับ list comprehension ตัวอย่างโค้ด my_list = [Python, good, FOr, few, CODERS] print(The list is:, my_list) my_result = [s for s in my_list if s.islower() or s.isupper()] print(The strings with

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