Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. วิธีนับจำนวนครั้งที่ Object ปรากฏใน Python List

    ใช้ list class count function นับ occurrences ของ object ใน list ใช้เมื่อต้องการนับ object เดียว หาทั้งหมดของ object ที่ส่งเข้าใน list ตัวอย่าง [red, blue, red, red, blue].count(red) 3 หากต้องการนับทุก object ใน list ใช้ Counter จาก collections นับ frequency ทุก object คืน dictionary keys=objects

  2. วิธีแทรก Object ใน List ที่ตำแหน่งที่กำหนดใน Python

    แทรก element ที่ตำแหน่งที่กำหนด ใช้ insert(pos, obj) รับ object หนึ่ง เพิ่มที่ตำแหน่ง pos ของ list ตัวอย่าง my_list = [2, 3, 1, -4, -1, -4] my_list.insert(1, 0) print(my_list) ผลลัพธ์ [2, 0, 3, 1, -4, -1, -4] หากแทรกที่ท้ายอย่างเดียว ใช้ append แทน insert รับ object หนึ่ง เพิ่มที่ท้าย list ตัวอย่า

  3. วิธีลบ Object จาก List ใน Python

    มี 3 วิธีลบ object จาก list ใน Python: remove, del, pop remove() ลบค่าแรกที่ตรงกับ argument ไใช่ index เฉพาะ ตัวอย่าง a = [3, 2, 3, 2] a.remove(3) print(a) ผลลัพธ์ [2, 3, 2] del ลบ index เฉพาะจาก list ตัวอย่าง a = [3, Hello, 2, 1] del a[1] print(a) ผลลัพธ์ [3, 2, 1] pop() คืน element ที่ลบ argum

  4. วิธีกลับลำดับ Objects ใน List ใน Python

    ใช้เมธอด reverse จาก list class กลับลำดับ list in-place ตัวอย่าง a = [3, Hello, 2, 1] a.reverse() print(a) ผลลัพธ์ [1, 2, Hello, 3] ใช้ list slicing [::-1] หากต้องการ list ใหม่แทน in-place start=ต้น stop=ปลาย step=-1 ตัวอย่าง a = [3, Hello, 2, 1] print(a[::-1]) ผลลัพธ์ [1, 2, Hello, 3] หากต้องกา

  5. วิธีเรียงลำดับ Objects ใน List ใน Python

    เรียง list ของ ints, floats, strings, chars หรือ class ที่ implement __cmp__ ได้โดยเรียก sort บน list หากต้องการ descending ส่ง reverse=True ตัวอย่าง my_list = [1, 5, 2, 6, 0] my_list.sort() print(my_list) my_list.sort(reverse=True) print(my_list) ผลลัพธ์ [0, 1, 2, 5, 6] [6, 5, 2, 1, 0] Tuples imm

  6. การทำงานของ * Operator บน List ใน Python

    Star * operator unpacks sequence/collection เป็น positional arguments หากมี list ต้องการส่ง items เป็น arguments ตามตำแหน่ง แทน index ทีละตัว ใช้ * operator ตัวอย่าง def multiply(a, b): return a * b values = [1, 2] print(multiply(*values)) Unpack list ให้ execute จริงเป็น: print(multiply(1, 2)

  7. การทำงานของ In Operator บน List ใน Python

    Operator in ของ Python ให้ loop ผ่าน members ของ collection (list/tuple) ตรวจสอบว่ามี member เท่ากับ item ที่ให้มาหรือไม่ ตัวอย่าง my_list = [5, 1, 8, 3, 7] print(8 in my_list) print(0 in my_list) ผลลัพธ์ True False หมายเหตุ: in ต่อ dictionary ตรวจสอบ key ตัวอย่าง my_dict = {name: TutorialsPoint,

  8. การทำงานของ Concatenation Operator บน List ใน Python

    Concatenation operator สร้าง list ใหม่จาก lists เริ่มต้นตามลำดับที่บวกเข้ามา ไม่ใช่ in-place operation ตัวอย่าง list1 = [1, 2, 3] list2 = [a, b] list3 = list1 + list2 print(list3) ผลลัพธ์ [1, 2, 3, a, b] วิธีอื่น concatenate 2 lists ใช้ extend หากต้องการ extend in-place ตัวอย่าง list1 = [1, 2, 3]

  9. การทำงานของ Repetition Operator บน List ใน Python

    ปกติ * แทนคูณ แต่ถ้า operand ซ้ายเป็น list จะกลายเป็น repetition operator ทำหลาย copies ของ list แล้ว join เข้าด้วยกัน สร้าง list ด้วย repetition operator * ตัวอย่าง numbers = [0] * 5 print(numbers) ผลลัพธ์ [0, 0, 0, 0, 0] [0] list มี 1 element คือ 0 Repetition operator ทำ 5 copies join กัน ตัวอย่

  10. การทำงานของ Del Operator บน List ใน Python

    Operator del ลบ index เฉพาะจาก list ที่กำหนด ต้องการลบ element index 1 จาก list a ใช้: ตัวอย่าง a = [3, Hello, 2, 1] del a[1] print(a) ผลลัพธ์ [3, 2, 1] del ลบ elements in-place ไม่สร้าง list ใหม่

  11. วิธี Index และ Slice Lists ใน Python

    Index หรือ slice list ใช้ operator [] บน list index บวกดึงจากซ้าย index ลบดึงจากขวา ตัวอย่าง my_list = [a, b, c, d] print(my_list[1]) print(my_list[-1]) ผลลัพธ์ b d ดึงส่วนของ list ใช้ slicing [start:stop:step] ตัวอย่าง my_list = [a, b, c, d] print(my_list[1:]) # index 1 ถึงสุด print(my_list[:2]

  12. Sequence Data Type ใน Python คืออะไร

    Sequences เก็บ multiple values อย่างมีระเบียบและมีประสิทธิภาพ มี sequence types หลายอย่าง: strings, Unicode strings, lists, tuples, bytearrays, range objects Dictionaries และ sets เป็น containers สำหรับ non-sequential data จากเอกสาร Python อย่างเป็นทางการ: Strings: immutable sequences ของ Unicode c

  13. ความแตกต่างระหว่าง List, Sequence และ Slice Data Types ใน Python

    List เป็น sequence แต่ sequence ไม่จำเป็นต้องเป็น list Sequence คือ type ใดก็ตามที่รองรับ sequence interface (protocol) Sequence types อธิบาย functional superset Slice objects สร้างโดย implicit ผ่าน syntactic sugar (foo[2:5]) ส่งให้ container type special methods (เช่น __getitem__) ที่ override ได้

  14. ความแตกต่างระหว่าง Append และ + Operator บน List ใน Python

    Operator + สร้าง list ใหม่เมื่อรวม 2 lists object เดิมไม่เปลี่ยน ขณะที่ extend และ append เพิ่ม in-place (object เดิมเปลี่ยน) นอกจากนี้ append ใส่ list เป็น object เดียว ขณะ + concatenate 2 lists ตัวอย่าง + list1 = [1, 2, 3] list2 = [a, b] list3 = list1 + list2 print(list3) ผลลัพธ์ [1, 2, 3, a, b]

  15. วิธีประกาศตัวแปรใน Python โดยไม่กำหนดค่า

    Python เป็น dynamic ไม่ต้อง declare things มันมีอยู่อัตโนมัติใน scope แรกที่ assign ทำได้แค่ assignment statement ธรรมดาที่ assign None ให้ตัวแปร ตัวอย่าง my_var = None ทำอย่างนี้จะไม่มี uninitialized variable แต่ไม่ได้หมายว่าไม่มี incorrectly initialized variables หาก init เป็น None ต้องแน่ใจว่านั

  16. ความแตกต่างระหว่าง Python 2.x และ 3.x

    Python 2.x เป็น legacy Python 3.x เป็นปัจจุบันและอนาคตของภาษา ฟีเจอร์เฉพาะ 3.x (ไม่มีใน 2.x): Strings เป็น Unicode by default Unicode/bytes separation ชัดเจน Exception chaining Function annotations Syntax สำหรับ keyword-only arguments Extended tuple unpacking Non-local variable declarations ความแต

  17. ความแตกต่างระหว่าง Python List และ Tuple

    ความแตกต่างหลัก: Tuples เป็น immutable ไม่สามารถเปลี่ยนค่าได้หลังสร้าง หากต้องเปลี่ยนค่า ใช้ List ข้อดีของ Tuples: เล็กน้อย Immutable จึงใช้เป็น key ใน dictionary ได้ คุณเปลี่ยนไม่ได้ คนอื่น (API functions) ก็เปลี่ยนไม่ได้ ใช้พื้นที่น้อยกว่า lists อ่านเพิ่มเติม: https://news.e-scribe.com/397

  18. ความแตกต่างระหว่าง Python List และ Array

    Python lists ยืดหยุ่น เก็บ heterogeneous data ได้ append แบบ amortized constant time เหมาะสำหรับ shrink/grow แต佔พื้นที่มากกว่า C arrays array.array เป็น thin wrapper ของ C arrays เก็บ homogeneous data เท่านั้น (type เดียวกัน) ใช้ memory แค่ sizeof(one object) * length List เช่น: [1, a, [1, 2], strin

  19. ความแตกต่างระหว่าง Python Tuple และ Dictionary

    โครงสร้างข้อมูลต่างกันมาก Elements ใน tuple มีคุณสมบัติ: รักษาลำดับ (Order maintained) Immutable เก็บ type ใดก็ได้ และผสม type ได้ เข้าถึงด้วย numeric index (zero-based) Python dictionary เป็น hash table implementation Elements มีคุณสมบัติ: ไม่รับประกันลำดับ (Ordering not guaranteed) ทุก entry มี

  20. ทำไม Tuple ถึงเป็น Immutable ใน Python

    Tuples เป็น immutable ด้วยเหตุผลดังนี้: Maintaining order: Tuples ใช้แสดงลำดับ เช่น ดึงข้อมูลจาก database เป็น list of tuples ทุก tuple อยู่ในลำดับ fields ที่ดึงมา Copy efficiency: แทนที่จะ copy immutable object สร้าง alias (bind variable to reference) ได้ Comparison efficiency: copy-by-reference เ

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