โครงสร้างข้อมูลมีความสำคัญในการเขียนโปรแกรม ใช้เพื่อจัดระเบียب จัดเก็บ และจัดการข้อมูลอย่างมีประสิทธิภาพเพื่อให้เข้าถึงและแก้ไขได้ง่าย
ลองนึกภาพการซักผ้ารายสัปดาห์ ในที่สุดคุณควรแยกถุงเท่า เสื้อ กางเกง และผ้าอ่อนไว้ในลิ้นชักต่างกัน เพื่อให้หาง่ายตอนเช้าก่อนออกไปทำงาน นี่คือการจัดระเบียบด้วยโครงสร้างข้อมูล แต่ถ้าคุณทิ้งของทั้งหมดลงในลิ้นชักเดียว หรือหลายลิ้นชักโดยไม่จัดระเบียบ คุณคิดว่าจะใช้เวลานานแค่ไหนในการหาชุดที่เหมาะสม? นี่คือการทำงานโดยไม่มีโครงสร้างข้อมูล
ในบทความนี้ เราจะสำรวจโครงสร้างข้อมูล built-in ที่ Python มีให้
โครงสร้างข้อมูล Built-in ใน Python
โครงสร้างข้อมูล built-in ใน Python ประกอบด้วย lists, dictionaries, tuples และ sets
Lists (รายการ)
Lists เป็นโครงสร้างข้อมูลที่ mutable (แก้ไขได้) และสามารถเก็บข้อมูลประเภทต่างๆ ไว้ใน list เดียวกันได้
my_list = ['string', 300, (2, 4), 'that previous data type was a tuple']
List มี methods ทั้งหมด 11 ตัวสำหรับเพิ่ม ลบ หรือจัดการ list เอง
การเพิ่มสมาชิกเข้า List
- append(): เพิ่มสมาชิกเดี่ยวเข้าไปที่ท้าย list
my_list = ['string, next is tuple', (2, 1), 3] my_list.append(500) print(my_list) # ['string, next is tuple', (2, 1), 3, 500]
- extend(): เพิ่มสมาชิกทุกตัวจาก iterable เข้าไปที่ท้าย list แตกต่างจาก append() ที่เพิ่ม iterable เป็นสมาชิกเดียว
my_list = ['string, next is tuple', (2, 1), 3] my_list.append((8, 9)) print(my_list) # ['string, next is tuple', (2, 1), 3, (8, 9)] # append คง tuple ไว้ my_list = ['string, next is tuple', (2, 1), 3] my_list.extend((8, 9)) print(my_list) # ['string, next is tuple', (2, 1), 3, 8, 9] # extend แยก tuple ออกเป็นสมาชิกเดี่ยว
- insert(): แทรกสมาชิกที่ตำแหน่ง (index) ที่กำหนด อาร์กิวเมนต์แรกคือ index อาร์กิวเมนต์ที่สองคือสมาชิกที่ต้องการแทรก
my_list = ['string, next is tuple', (2, 1), 3] my_list.insert(0, 700) print(my_list) # [700, 'string, next is tuple', (2, 1), 3] # แทรก 700 ที่ index 0
การลบสมาชิกออกจาก List
- remove(): ลบสมาชิกตัวแรกที่มีค่าตรงกับที่ระบุ
my_list = ['string, next is tuple', (2, 1), 3, 8, 3] my_list.remove(3) print(my_list) # ['string, next is tuple', (2, 1), 8, 3]
- pop(): ลบสมาชิกที่ตำแหน่งที่ระบุ หากไม่ระบุ index จะลบสมาชิกตัวสุดท้าย
my_list = ['string, next is tuple', (2, 1), 3] my_list.pop(0) print(my_list) # [(2, 1), 3] my_list = ['string, next is tuple', (2, 1), 3] my_list.pop() print(my_list) # ['string, next is tuple', (2, 1)]
- clear(): ลบสมาชิกทั้งหมดออกจาก list
my_list = ['string, next is tuple', (2, 1), 3] my_list.clear() print(my_list) # []
Methods อื่นๆ ของ List
- index(): คืนค่า index ของสมาชิกที่ระบุ
my_list = [8, 20, 1, 9, 2, 3, 937, 0] print(my_list.index(9)) # 3
- count(): นับจำนวนครั้งที่ค่าปรากฏใน list
my_list = [8, 20, 1, 8, 2, 8, 937, 8] print(my_list.count(8)) # 4
- sort(): เรียงลำดับ list สามารถระบุ arguments เพื่อปรับแต่งการเรียงได้
my_list = [8, 20, 1, 9, 2, 3, 937, 0] my_list.sort() print(my_list) # [0, 1, 2, 3, 8, 9, 20, 937] my_list = [8, 20, 1, 9, 2, 3, 937, 0] my_list.sort(reverse=True) print(my_list) # [937, 20, 9, 8, 3, 2, 1, 0]
- reverse(): กลับลำดับสมาชิกใน list
my_list = [8, 20, 1, 9, 2, 3, 937, 0] my_list.reverse() print(my_list) # [0, 937, 3, 2, 9, 1, 20, 8]
- copy(): คืนค่า copy ของ list
my_list = [8, 20, 1, 9, 2, 3, 937, 0] my_list.copy() print(my_list) # [8, 20, 1, 9, 2, 3, 937, 0]
Tuples (ทูเพิล)
Tuples เป็นข้อมูลที่อยู่ในวงเล็บ () ต่างจาก lists นั้น tuples เป็น immutable (แก้ไขไม่ได้) และทำงานเร็วกว่า lists เนื่องจากเป็น immutable จึงสามารถใช้เป็น keys ใน dictionaries ได้ Tuples ยังใช้สำหรับ return หลายค่าจากฟังก์ชันได้อีกด้วย
เราสามารถเพิ่มข้อมูลเข้า tuple ได้โดยใช้การต่อ (concatenation):
my_tuple = (1, 2, 3) print(my_tuple) # (1, 2, 3) my_tuple = my_tuple + (4, 5, 6) print(my_tuple) # (1, 2, 3, 4, 5, 6)
Dictionaries (พจนานุกรม)
Dictionaries เป็นโครงสร้างข้อมูลที่เก็บ key-value pairs คล้าย objects ใน JavaScript คล้าย lists dictionaries เป็น mutable สามารถเปลี่ยนแปลงข้อมูลได้
ตัวอย่าง key-value pairs คือ คุณสมบัติของบุคคลและคำอธิบายของคุณสมบัติเหล่านั้น เช่น name, age, height, weight เป็น keys ส่วน Josh, 33, 5'10, 180 lbs เป็น values
my_dict = {'name': 'Josh', 'age': 33, 'height': "5'10", 'weight': '180 lbs'}
เนื่องจาก dictionaries เป็น mutable เราจึงสามารถเปลี่ยน 'Josh' เป็นชื่ออื่นได้:
my_dict = {'name': 'Josh', 'age': 33, 'height': "5'10", 'weight': '180 lbs'}
my_dict['name'] = 'Patrick'
print(my_dict) # {'name': 'Patrick', 'age': 33, 'height': "5'10", 'weight': '180 lbs'}
เราสามารถเพิ่มค่าโดยสร้าง key-value pair ใหม่:
my_dict = {'name': 'Josh', 'age': 33, 'height': "5'10", 'weight': '180 lbs'}
my_dict['location'] = 'San Francisco'
print(my_dict) # {'name': 'Josh', 'age': 33, 'height': "5'10", 'weight': '180 lbs', 'location': 'San Francisco'}
เราสามารถลบ key-value pairs ได้โดยใช้คำสั่ง del, pop(), หรือ popitem() หมายเหตุ: pop() ใน dictionaries ต้องมี argument จึงต้องใช้ popitem() เพื่อลบ key-value pair ตัวสุดท้าย
my_dict = {'name': 'Josh', 'age': 33, 'height': "5'10", 'weight': '180 lbs'}
del my_dict['name']
print(my_dict) # {'age': 33, 'height': "5'10", 'weight': '180 lbs'}
my_dict = {'name': 'Josh', 'age': 33, 'height': "5'10", 'weight': '180 lbs'}
my_dict.pop('name')
print(my_dict) # {'age': 33, 'height': "5'10", 'weight': '180 lbs'}
my_dict = {'name': 'Josh', 'age': 33, 'height': "5'10", 'weight': '180 lbs'}
my_dict.popitem()
print(my_dict) # {'name': 'Josh', 'age': 33, 'height': "5'10"}
เราสามารถพิมพ์เฉพาะ keys หรือเฉพาะ values ของ dictionary ได้:
my_dict = {'name': 'Josh', 'age': 33, 'height': "5'10", 'weight': '180 lbs'}
print(my_dict.keys()) # dict_keys(['name', 'age', 'height', 'weight'])
my_dict = {'name': 'Josh', 'age': 33, 'height': "5'10", 'weight': '180 lbs'}
print(my_dict.values()) # dict_values(['Josh', 33, "5'10", '180 lbs'])
เพื่อพิมพ์เป็น key-value pairs ใช้ method items():
my_dict = {'name': 'Josh', 'age': 33, 'height': "5'10", 'weight': '180 lbs'}
print(my_dict.items()) # dict_items([('name', 'Josh'), ('age', 33), ('height', "5'10"), ('weight', '180 lbs')])
Sets (เซต)
Sets เป็น mutable, unordered collections ของสมาชิกที่ไม่ซ้ำกัน (unique elements) Sets 长得像 dictionaries ที่ใช้ curly brackets แต่ไม่มี key-value pairs
my_set = {1, 2, 2, 2, 3, 3, 4, 4}
print(my_set) # {1, 2, 3, 4}
เราสามารถเพิ่มสมาชิกเข้า set ด้วย method add():
my_set = {1, 2, 2, 2, 3, 3, 4, 4}
my_set.add(5)
print(my_set) # {1, 2, 3, 4, 5}
มี methods อีก 4 ตัวสำหรับ sets คือ union(), intersection(), difference(), และ symmetric_difference()
- union(): รวมสอง sets ให้เป็น set เดียวโดยไม่มีสมาชิกซ้ำ
my_set = {1, 2, 2, 2, 3, 3, 4, 4}
my_set.add(5)
print(my_set) # {1, 2, 3, 4, 5}
another_set = {3, 3, 4, 4, 5, 5, 6}
print(my_set.union(another_set)) # {1, 2, 3, 4, 5, 6}
- intersection(): หาสมาชิกที่ซ้ำกันในทั้งสอง sets
my_set = {1, 2, 2, 2, 3, 3, 4, 4}
my_set.add(5)
print(my_set) # {1, 2, 3, 4, 5}
another_set = {3, 3, 4, 4, 5, 5, 6}
print(my_set.intersection(another_set)) # {3, 4, 5}
- difference(): หาสมาชิกที่อยู่ใน set แรกแต่ไม่อยู่ใน set ที่สอง
my_set = {1, 2, 2, 2, 3, 3, 4, 4}
my_set.add(5)
print(my_set) # {1, 2, 3, 4, 5}
another_set = {3, 3, 4, 4, 5, 5, 6}
print(my_set.difference(another_set)) # {1, 2}
- symmetric_difference(): หาสมาชิกที่ต่างกันในทั้งสอง sets (union ลบ intersection)
my_set = {1, 2, 2, 2, 3, 3, 4, 4}
my_set.add(5)
print(my_set) # {1, 2, 3, 4, 5}
another_set = {3, 3, 4, 4, 5, 5, 6}
print(my_set.symmetric_difference(another_set)) # {1, 2, 6}