Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. หาความต่างของคอลัมน์ที่ N ติดกันใน Tuple List ด้วย Python

    เมื่อต้องการหาความต่างของคอลัมน์ที่ N (Nth column) ระหว่าง tuple ติดกันใน list of tuples สามารถวนลูปคำนวณความต่างสัมบูรณ์ (absolute difference) โค้ดตัวอย่าง my_list = [(67, 89, 32), (11, 23, 44), (65, 75, 88)] print(The list is:) print(my_list) print(The value of k has been initialized) K = 1 my_res

  2. โปรแกรม Python เรียงลำดับ Tuple ตามค่าสูงสุดในแต่ละ Tuple

    เมื่อต้องการเรียงลำดับ list of tuples ตามค่าสูงสุด (maximum element) ในแต่ละ tuple สามารถใช้ฟังก์ชัน max() เป็น key ในการ sort โค้ดตัวอย่าง def get_max_value(my_val): return max(my_val) my_list = [(4, 6, 8, 1), (13, 21, 42, 56), (7, 1, 9, 0), (1, 2)] print(The list is:) print(my_list) my_list.s

  3. โปรแกรม Python หา Tuple ที่สมาชิกทุกตัวหารด้วย K ลงตัวจาก List of Tuples

    เมื่อต้องการกรอง tuple ที่สมาชิกทุกตัวหารด้วย K ลงตัว จาก list of tuples สามารถใช้ list comprehension ร่วมกับ all() โค้ดตัวอย่าง my_list = [(45, 90, 135), (71, 92, 26), (2, 67, 98)] print(The list is:) print(my_list) K = 45 print(The value of K has been initialized to) print(K) my_result = [sub for

  4. โปรแกรม Python แปลง Tuple เป็น List โดยเพิ่ม String หลังแต่ละสมาชิก

    เมื่อต้องการแปลง tuple เป็น list โดยแทรก string ที่กำหนด (เช่น Hi) หลังสมาชิกแต่ละตัว สามารถใช้ list comprehension แบบ nested โค้ดตัวอย่าง my_tup = (56, 78, 91, 32, 45, 11, 23) print(The tuple is:) print(my_tup) K = Hi my_result = [elem for sub in my_tup for elem in (sub, K)] print(The tuple after c

  5. โปรแกรม Python หา Tuple ที่มีสมาชิกเป็นบวกทั้งหมดจาก List of Tuples

    เมื่อต้องการกรอง tuple ที่มีสมาชิกทุกตัวเป็นจำนวนบวก (หรือศูนย์) จาก list of tuples สามารถใช้ list comprehension ร่วมกับฟังก์ชัน all() โค้ดตัวอย่าง = 0 for elem in sub)] print(The positive elements are:) print(my_result) ผลลัพธ์ The list is: [(56, 43), (-31, 21, 23), (51, -65, 26), (24, 56)] The pos

  6. โปรแกรม Python ตรวจสอบว่า Singly Linked List เป็น Palindrome หรือไม่

    เมื่อต้องการตรวจสอบว่า Singly Linked List เป็น Palindrome (อ่านจากหน้าหลังได้เหมือนกัน) หรือไม่ สามารถเปรียบเทียบโหนดแรกกับโหนดสุดท้าย แล้วเลื่อนเข้ามาเรื่อยๆ โค้ดตัวอย่าง class Node: def __init__(self, data): self.data = data self.next = None class LinkedList_struct: def __

  7. วิธีใช้ Boto3 แบ่งหน้า (Paginate) ผ่าน Table Versions ใน AWS Glue

    ข้อกำหนดปัญหา: ใช้ไลบรารี boto3 ใน Python เพื่อแบ่งหน้าผ่าน table versions ของ table จาก AWS Glue Data Catalog ขั้นตอนการแก้ปัญหา ขั้นตอน 1: นำเข้า boto3 และ botocore.exceptions ขั้นตอน 2: พารามิเตอร์: database_name, table_name (บังคับ), max_items, page_size, starting_token (เลือกได้) max_items: จ

  8. โปรแกรม Python แปลง Singly Linked List เป็น Circular Linked List

    เมื่อต้องการแปลง Singly Linked List ให้กลายเป็น Circular Linked List ให้กำหนดให้โหนดสุดท้าย (last node) ชี้กลับไปที่โหนดแรก (head) โค้ดตัวอย่าง class Node: def __init__(self, data): self.data = data self.next = None class LinkedList_struct: def __init__(self): self.h

  9. วิธีใช้ Boto3 แบ่งหน้า (Paginate) ผ่าน Tables ทั้งหมดใน AWS Glue

    ข้อกำหนดปัญหา: ใช้ไลบรารี boto3 ใน Python เพื่อแบ่งหน้าผ่าน tables ทั้งหมดจาก AWS Glue Data Catalog ขั้นตอนการแก้ปัญหา ขั้นตอน 1: นำเข้า boto3 และ botocore.exceptions ขั้นตอน 2: พารามิเตอร์: database_name (บังคับ), max_items, page_size, starting_token (เลือกได้) max_items: จำนวนระเบียนสูงสุด page_

  10. วิธีใช้ Boto3 แบ่งหน้า (Paginate) ผ่าน Triggers ทั้งหมดใน AWS Glue

    ข้อกำหนดปัญหา: ใช้ไลบรารี boto3 ใน Python เพื่อแบ่งหน้าผ่าน triggers ทั้งหมดจาก AWS Glue Data Catalog ขั้นตอนการแก้ปัญหา ขั้นตอน 1: นำเข้า boto3 และ botocore.exceptions ขั้นตอน 2: พารามิเตอร์เลือกได้: max_items, page_size, starting_token max_items: จำนวนระเบียนสูงสุด page_size: ขนาดแต่ละหน้า start

  11. โปรแกรม Python สร้าง Binary Tree จาก Inorder และ Postorder Traversal

    เมื่อได้รับ Inorder traversal และ Postorder traversal สามารถสร้าง Binary Tree กลับมาได้ (เพราะ Inorder + Postorder ระบุโครงสร้างต้นไม้ได้อย่างเดียว) โค้ดตัวอย่าง class BinaryTree_struct: def __init__(self, key=None): self.key = key self.left = None self.right = None

  12. โปรแกรม Python หาค่าน้อยที่สุดและมากที่สุดใน Binary Search Tree

    เมื่อต้องการหาค่าน้อยที่สุด (smallest) และมากที่สุด (largest) ใน Binary Search Tree (BST) สามารถใช้คุณสมบัติของ BST: ค่าน้อยที่สุดอยู่ที่โหนดซ้ายสุด (leftmost) และค่ามากที่สุดอยู่ที่โหนดขวาสุด (rightmost) โค้ดตัวอย่าง node.key: if self.left is None: self.left = node

  13. วิธีดึงรายการ Version ทั้งหมดของ Object ใน S3 จาก AWS Resource

    ในบทความนี้จะแสดงวิธีดึงรายการ version ทั้งหมดของ object ใน S3 ที่อยู่ใน AWS Resource ตัวอย่าง ดึง version ทั้งหมดของ test.zip จาก Bucket_1/testfolder ใน S3 ข้อกำหนดปัญหา: ใช้ไลบรารี boto3 ใน Python เพื่อดึงรายการ version ทั้งหมดของ object จาก S3 ขั้นตอนการแก้ปัญหา ขั้นตอน 1: นำเข้า boto3 และ botoc

  14. วิธีใช้ Boto3 แบ่งหน้า (Paginate) ผ่าน Object Versions ของ S3 Bucket ใน AWS Glue

    ข้อกำหนดปัญหา: ใช้ไลบรารี boto3 ใน Python เพื่อแบ่งหน้าผ่าน object versions ของ S3 bucket จาก AWS Glue Data Catalog ขั้นตอนการแก้ปัญหา ขั้นตอน 1: นำเข้า boto3 และ botocore.exceptions ขั้นตอน 2: พารามิเตอร์: bucket_name (บังคับ), max_items, page_size, starting_token (เลือกได้) max_items: จำนวนระเบี

  15. โปรแกรม Python ใช้งาน Stack (สแต็ก) แบบ Interactive

    เมื่อต้องการนำ Stack มาใช้งานใน Python สามารถสร้างคลาส Stack ที่มีเมธอด push (เพิ่ม), pop (ลบ), และตรวจสอบว่าว่างหรือไม่ โค้ดตัวอย่าง class Stack_struct: def __init__(self): self.items = [] def check_empty(self): return self.items == [] def add_elements(self, my

  16. โปรแกรม Python ใช้งาน Queue (คิว) แบบ Interactive

    เมื่อต้องการนำ Queue มาใช้งานใน Python สามารถสร้างคลาส Queue ที่มีเมธอด enqueue (เพิ่ม), dequeue (ลบ), และตรวจสอบว่าว่างหรือไม่ โค้ดตัวอย่าง class Queue_struct: def __init__(self): self.items = [] def check_empty(self): return self.items == [] def enqueue_elem(s

  17. ตรวจสอบว่าเลขมีศูนย์ติดกันในฐานที่กำหนดหรือไม่ด้วย Python

    เมื่อต้องการตรวจสอบว่าเลขในฐาน (base) ใดฐานหนึ่งมีศูนย์ติดต่อกัน (consecutive zeros) หรือไม่ สามารถแปลงเลขเป็นฐานนั้น แล้วตรวจสอบรูปแบบของตัวเลข โค้ดตัวอย่าง 0: res = False continue res = True return True N, K = 8, 2 print(Does the number have consecutive zeroes

  18. โปรแกรม Python สร้าง Binary Tree พร้อมการ Insert, Delete, และ Display

    เมื่อต้องการสร้าง Binary Tree และดำเนินการ insert, delete, display สามารถสร้างคลาสที่มีเมธอดสำหรับแต่ละการดำเนินการ แล้วสร้างอินสแตนซ์เพื่อใช้งาน โค้ดตัวอย่าง class Tree_struct: def __init__(self, data=None, parent=None): self.key = data self.children = [] self.parent = p

  19. โปรแกรม Python สำหรับคัดลอกสมาชิกทั้งหมดจากอาร์เรย์หนึ่งไปยังอีกอาร์เรย์หนึ่ง

    เมื่อต้องการคัดลอกสมาชิกทั้งหมดจากลิสต์หนึ่งไปยังอีกลิสต์หนึ่ง สามารถสร้างลิสต์เปล่าขนาดเท่ากัน แล้ววนลูปกำหนดค่าแต่ละตำแหน่ง ตัวอย่างโค้ด my_list_1 = [34, 56, 78, 90, 11, 23] my_list_2 = [None] * len(my_list_1) for i in range(0, len(my_list_1)): my_list_2[i] = my_list_1[i] print(The list is : )

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:264/451  20-คอมพิวเตอร์/Page Goto:1 258 259 260 261 262 263 264 265 266 267 268 269 270