Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. การแปลงชนิดข้อมูลใดๆ เป็น String ใน Python (str())

    ฟังก์ชัน built-in str() แปลงออบเจ็กต์ทุกชนิดเป็น String Representation ตัวอย่าง Built-in Types str({1: 11, 2: 22, 3: 33}) {1: 11, 2: 22, 3: 33} User-defined Class: ต้อง define __str__() str(NoStr()) ความแตกต่าง str() vs repr() ฟังก์ชันวัตถุประสงค์ผู้ใช้เป้าหมาย str(obj)Readable, user-frien

  2. การเรียงลำดับ Dictionary ตาม Value ใน Python (OrderedDict + sorted)

    Python 3.7+ Dictionary รักษาลำดับ insertion order แล้ว แต่การเรียงตาม Value ต้องใช้ sorted() ร่วมกับ collections.OrderedDict (หรือ dict ธรรมดาใน 3.7+) ตัวอย่างข้อมูล D = {5: fff, 3: ttt, 1: ooo, 4: bbb, 2: ddd} วิธีที่ 1: OrderedDict + sorted (Python ทุกเวอร์ชัน) OrderedDict(sorted(D.items(), ke

  3. การ Merge สอง Dictionary ใน Expression เดียวใน Python

    รวม Dictionary สองตัวให้กลายเป็นหนึ่งตัวในบรรทัดเดียว วิธีที่ 1: update() (In-place, Python ทุกเวอร์ชัน) a {1: a, 2: b, 3: c, x: 1, y: 2, z: 3} ข้อควรระวัง: update() แก้ไข a inplace และคืนค่า None → ไม่สามารถ chain ได้ วิธีที่ 2: Dictionary Unpacking ** (Python 3.5+, สร้าง Dict ใหม่) c {1: a, 2:

  4. การเพิ่ม Key ใหม่เข้า Dictionary ใน Python

    Dictionary เป็นคอลเลกชัน key-value pairs ไม่มี index จำลอง Key ไม่ซ้ำได้ → เพิ่ม Key ใหม่โดย Assignment แบบตรงไปตรงมา ตัวอย่าง D1 {1: a, 2: b, 3: c, x: 1, y: 2, z: 3, 10: z} พฤติกรรม Key ใหม่ (10) → เพิ่ม pair ใหม่ Key มีอยู่แล้ว (เช่น D1[1] = new) → อัปเดต value เดิม วิธีอื่น: update() D1 {1

  5. การลบ Key ออกจาก Dictionary ใน Python (del และ pop)

    ลบ key-value pair ออกจาก Dictionary ได้ 2 วิธีหลัก วิธีที่ 1: del Statement D1 {1: a, 2: b, 3: c, y: 2, z: 3} del D1[key] ลบ key ที่ระบุ Key ไม่มีอยู่ → KeyError วิธีที่ 2: pop() Method (คืนค่า Value ที่ลบ) D1 {1: a, 2: b, 3: c, x: 1, z: 3} D1.pop(key) ลบและคืนค่า value D1.pop(key, default)

  6. การเขียนโปรแกรม Python แรก (Hello World)

    ใช้ Editor ใดๆ ที่รองรับ Python (VS Code, PyCharm, Sublime Text, IDLE, ฯลฯ) Python มาพร้อม IDLE (Integrated Development and Learning Environment) แล้ว ขั้นตอนด้วย IDLE เปิด IDLE File → New File (Ctrl+N) พิมพ์โค้ด: print(Hello World!) Save: hello.py (File → Save) Run: Run → Run Module (F5) รันจ

  7. ไวยากรณ์การสร้าง Tuple ใน Python

    Tuple ใน Python เป็น Immutable collection ของรายการ ไม่จำเป็นต้องเป็นชนิดเดียวกัน คั่นด้วยจุลภาค วางในวงเล็บปกติ () (optional) รูปแบบทั่วไป T1, T2 ((1, one, 3.45, [1, 2, 3]), (1, 2, 3, 4)) Tuple ว่าง (Empty Tuple) type(T3) Tuple เดียว (Singleton Tuple) - ต้องมีจุลภาคท้าย! type(not_tuple) คุ

  8. การสร้าง Dictionary ภายใน Dictionary ใน Python (Nested Dict)

    Dictionary เป็น mutable → สามารถใช้ Dictionary อีกตัวเป็น Value ของ Key ได้ → สร้าง Nested Dictionary ตัวอย่าง students {student1: {name: Raaj, age: 23, subjects: [Phy, Che, maths], GPA: 8.5}, student2: {name: Kiran, age: 21, subjects: [Phy, Che, bio], GPA: 8.25}} การเข้าถึงและแก้ไข students[st

  9. การจับคู่ Word (คำ) ใน Python Regex ด้วย Lookaround Assertions

    ใช้ Positive Lookbehind (?<=...) และ Positive Lookahead (?=...) เพื่อจับคู่คำเฉพาะ โดยเคารพอักขระรอบข้างแต่ ไม่รวมอักขระเหล่านั้นในผลลัพธ์ ตัวอย่าง: จับคู่คำว่า meeting ใน URL import re s = https://www.google.com/meeting_agenda_minutes.html result = re.findall(r(?<=[\W_])meeting(?=[\W_]), s) print(

  10. การจับคู่ Non-word Characters ใน Python Regex (\W)

    ใช้ \W (W พิมพ์ใหญ่) เพื่อจับคู่ Non-word character เพียงตัวเดียว คือ อักขระที่ ไม่ใช่ ตัวอักษร ตัวเลข หรือ underscore (_) เทียบเท่า: [^a-zA-Z0-9_] หรือ [^\w] ตัวอย่าง import re s = ab5z8d*$&Y@ regx = re.compile(r\W) result = regx.findall(s) print(result) ผลลัพธ์ [*, $, &, @] คำอธิบาย สตริง: a

  11. การจับคู่ Whitespace ใน Python Regex (\s)

    ใช้ \s (s พิมพ์เล็ก) เพื่อจับคู่ Whitespace character เพียงตัวเดียว (space, tab, newline, carriage return, form feed, vertical tab) ตัวอย่าง: search() - หาครั้งแรก import re result = re.search(r[\s], The Indian Express) print(result) ผลลัพธ์ จับคู่ space แรกระหว่าง The และ Indian ตัวอย่าง: find

  12. การจับคู่ Non-whitespace Character ใน Python Regex (\S+)

    ใช้ \S+ (S พิมพ์ใหญ่ + quantifier +) เพื่อจับคู่ ลำดับอักขระที่ไม่ใช่ whitespace 1 ตัวขึ้นไป ตัวอย่าง: search() - หาครั้งแรก import re foo = re.search(r\S+, Need for Speed 2) print(foo) ผลลัพธ์ จับคู่ Need (non-whitespace sequence แรก) ตัวอย่าง: findall() - หาทั้งหมด (แยกคำ) import re foo = re.

  13. การจับคู่เฉพาะ Digits ใน Python Regex (\d+)

    ใช้ \d+ (d พิมพ์เล็ก + quantifier +) เพื่อจับคู่ ลำดับตัวเลข 1 ตัวขึ้นไป ตัวอย่าง: search() - หาครั้งแรก import re m = re.search(r\d+, 5Need47forSpeed 2) print(m) ผลลัพธ์ จับคู่ 5 (digit sequence แรกที่เจอ) ตัวอย่าง: findall() - หาทั้งหมด import re m = re.findall(r\d, 5Need47forSpeed 2) print(m

  14. การจับคู่เฉพาะ Non-digits ใน Python Regex (\D+)

    ใช้ \D+ (D พิมพ์ใหญ่ + quantifier +) เพื่อจับคู่ ลำดับอักขระที่ไม่ใช่ตัวเลข 1 ตัวขึ้นไป ตัวอย่าง: search() - หาครั้งแรก import re m = re.search(r\D+, 5Need47for Speed 2) print(m) ผลลัพธ์ จับคู่ Need (non-digit sequence แรกที่เจอ) ตัวอย่าง: findall() - หาทั้งหมด import re m = re.findall(r\D+, 5N

  15. การหาความแตกต่างของ Keys ระหว่างสอง Dictionary ใน Python (Symmetric Difference)

    ใช้ Set (เซต) ซึ่งเป็นคอลเลกชัน unique, unordered, immutable รองรับ set operations ตามทฤษฎีเซต Symmetric Difference (ความแตกต่างแบบสมมาตร) คืนสมาชิกที่อยู่ใน เซตใดเซตหนึ่ง แต่ไม่ใช่ทั้งสอง (XOR) ใช้ตัวดำเนินการ ^ ตัวอย่าง set(D1.keys()) ^ set(D2.keys()) {2, 5} คำอธิบาย Keys ของ D1: {1, 2, 3} Ke

  16. การพิมพ์ Python Dictionary แบบสุ่ม (Random Order)

    Dictionary ไม่ได้เป็น iterable ที่มี index → ไม่สามารถสุ่มลำดับโดยตรงได้ แต่ keys() เป็น iterable สามารถสุ่มลำดับด้วย random.shuffle() แล้ววนพิมพ์ตาม key ที่สุ่มแล้ว ตัวอย่าง for k in keys: ... print(k, D1[k]) pencil 10 eraser 5 sharpner 5 book 100 pen 25 วิธีสั้นๆ (Python 3.6+) for k in ran

  17. การ Iterate Nested Dictionary แบบ Recursive ใน Python

    Nested Dictionary ที่มีความลึกไม่คงที่ ต้องใช้ Recursive Function เพื่อเดินทาง (traverse) ทุกชั้น ตัวอย่างข้อมูล D1 = { ... 1: {2: {3: 4, 5: 6}, 3: {4: 5, 6: 7}}, ... 2: {3: {4: 5}, 4: {6: 7}} ... } Recursive Function iterdict(D1) ผลลัพธ์ 3 : 4 5 : 6 4 : 5 6 : 7 4 : 5 6 : 7 เวอร์ชันที่

  18. การรวม (Merge) หลาย Dictionary ใน Python

    รวมหลาย Dictionary ให้กลายเป็น Dictionary เดียว วิธีที่ 1: Loop + update() (Classic) merged {a: 1, b: 2, c: 3, d: 2, e: 57} หมายเหตุ: Key ซ้ำ (a, c) จะใช้ค่าจาก dict ที่มาก่อน (last one wins) วิธีที่ 2: Dictionary Unpacking (Python 3.5+) merged {a: 1, b: 2, c: 3, d: 2, e: 57} วิธีที่ 3: dict()

  19. Dictionary View Objects ใน Python (keys, values, items)

    เมธอด keys(), values(), items() ของ Dictionary คืนค่า View Objects ไม่ใช่ลิสต์ View objects เป็น dynamic view ที่สะท้อนการเปลี่ยนแปลงของ dictionary ต้นทางทันที ประเภท View Objects เมธอดView Typeเนื้อหา keys()dict_keysKey ทั้งหมด values()dict_valuesValue ทั้งหมด items()dict_itemsTuple (key, valu

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:31/451  20-คอมพิวเตอร์/Page Goto:1 25 26 27 28 29 30 31 32 33 34 35 36 37