Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. Python – เรียงลำดับสตริงตามจำนวนตัวอักษรที่ไม่ซ้ำ

    เมื่อต้องการเรียงลำดับลิสต์สตริงตามจำนวนตัวอักษรที่ไม่ซ้ำกัน (unique characters) สามารถใช้ฟังก์ชันช่วยที่มี set(), list() และ len() ได้ ตัวอย่าง def my_sort_func(my_elem): return len(list(set(my_elem))) my_list = [python, Will, Hi, how, fun, learn, code] print(The list is : ) print(my_list) my_

  2. Python – ตรวจสอบความยาวสตริงตามที่กำหนด

    เมื่อต้องการตรวจสอบว่าสตริงแต่ละตัวในลิสต์มีความยาวตรงตามที่กำหนดหรือไม่ สามารถใช้ลูป iteration และ len() ได้ ตัวอย่าง my_list = [python, is, fun, to, learn, Will, how] print(The list is :) print(my_list) length_list = [6, 2, 3, 2, 5, 4, 3] my_result = True for index in range(len(my_list)): if l

  3. Python – เรียงลำดับสตริงตามส่วนตัวเลข

    เมื่อต้องการเรียงลำดับสตริงตามตัวเลขที่อยู่ในสตริง สามารถใช้ regular expression และ re.findall() ได้ ตัวอย่าง import re def my_num_sort(my_str): return list(map(int, re.findall(r\d+, my_str)))[0] my_list = [pyth42on, 14is, 32fun, 89to, lea78rn] print(The list is :) print(my_list) my_list.sort(k

  4. Python – หาตำแหน่งทั้งหมดของ Substring ในสตริง

    เมื่อต้องการหาตำแหน่งเริ่มต้นทั้งหมดของ substring ในสตริง สามารถใช้ list comprehension และ startswith() ได้ ตัวอย่าง my_string = Is python fun to learn? print(The list is :) print(my_string) substring = pyt print(The substring is :) print(substring) my_result = [i for i in range(len(my_string)) if m

  5. Python – ลบ Dictionary จากลิสต์ถ้าไม่มีค่าที่กำหนด

    เมื่อต้องการลบ dictionary ออกจากลิสต์ของ dictionary ถ้าไม่มีค่าที่กำหนดใน key ที่ต้องการ สามารถใช้ลูป iteration และ del ได้ ตัวอย่าง my_list = [{id: 1, data: Python}, {id: 2, data: Code}, {id: 3, data: Learn}] print(The list is :) print(my_list) for index in range(len(my_list)): if my_list[index

  6. Python – ลบ Tuple ที่ความต่างมากกว่า K

    เมื่อต้องการลบ tuple ที่ความต่างระหว่างสมาชิกมากกว่า K สามารถใช้ abs() และ list comprehension ได้ ตัวอย่าง my_tuple = [(41, 18), (21, 57), (39, 22), (23, 42), (22, 10)] print(The tuple is :) print(my_tuple) K = 20 my_result = [element for element in my_tuple if abs(element[0] - element[1]) <= K] pri

  7. Python – สร้าง Dictionary จากลิสต์

    เมื่อต้องการสร้าง dictionary จากลิสต์โดยใช้สมาชิกเป็น key และค่าเดียวกันสำหรับทุก key สามารถใช้ dict.fromkeys() ได้ ตัวอย่าง my_list = [Hi, Will, how, Python, cool] print(The list is ) print(my_list) my_dict = dict.fromkeys(my_list, my_value) print(my_dict) ผลลัพธ์ The list is [Hi, Will, how, Python

  8. Python – ดึง N สมาชิกถัดจากตำแหน่ง K

    เมื่อต้องการดึง N สมาชิกถัดจากตำแหน่ง K ในลิสต์ (หรือแทนที่ค่า) สามารถใช้ลูป iteration ได้ ตัวอย่าง my_list = [31, 24, 46, 18, 34, 52, 26, 29] print(The list is :) print(my_list) K = 2 print(The value of K is :) print(K) N = 3 print(The value of N is :) print(N) for index in range(K): my_list[in

  9. Python – แปลงลิสต์สตริงที่มีตัวคั่นเป็นลิสต์ของ Tuple

    เมื่อต้องการแปลงลิสต์ของสตริงที่มีตัวคั่น (delimiter) เป็นลิสต์ของ tuple ตัวเลข สามารถใช้ list comprehension, tuple() และ split() ได้ ตัวอย่าง my_list = [21$12, 33$24$48$69, 14$10$44] print(The list is :) print(my_list) key = $ print(The key is :) print(key) my_result = [tuple(int(element) for eleme

  10. Python – นับจำนวนตัวเลขที่มากกว่า K ในลิสต์

    เมื่อต้องการนับจำนวนตัวเลขในลิสต์ที่มีค่ามากกว่า K สามารถใช้ลูป iteration ธรรมดาได้ ตัวอย่าง K: my_count = my_count + 1 print(The result is :) print(my_count) ผลลัพธ์ The list is : [11, 17, 25, 16, 23, 18] The value of K is 15 The result is : 5 คำอธิบาย กำหนดลิสต์ตัวเลขและค่า K แสดงผล สร้า

  11. Python – แทรกตัวอักษรในสตริงซ้ำทุก K ตัวอักษร

    เมื่อต้องการแทรกตัวอักษรในสตริงซ้ำทุก K ตัวอักษร สามารถใช้ฟังก์ชันที่มี append(), ตัวดำเนินการ + และ list slicing ได้ ตัวอย่าง def insert_char_after_key_elem(my_string, my_key, my_char): my_result = [] for index in range(0, len(my_string), my_key): my_result.append(my_string[:index]

  12. Python – แปลงสตริงเป็นเมทริกซ์ K ตัวอักษรต่อแถว

    เมื่อต้องการแปลงสตริงเป็นเมทริกซ์ (ลิสต์ของลิสต์) ที่มี K ตัวอักษรต่อแถว สามารถใช้ฟังก์ชันที่มี list comprehension และ list slicing ได้ ตัวอย่าง def convert_to_matrix(my_string, my_key): temp = [my_string[index: index + my_key] for index in range(0, len(my_string), my_key)] my_result = [list

  13. Python – แทนที่ K สมาชิกแรกด้วยค่า N

    เมื่อต้องการแทนที่ K สมาชิกแรกในลิสต์ด้วยค่า N สามารถใช้ลูป iteration ธรรมดาได้ ตัวอย่าง my_list = [13, 34, 26, 58, 14, 32, 16, 89] print(The list is :) print(my_list) K = 2 print(The value of K is :) print(K) N = 99 print(The value of N is :) print(N) for index in range(K): my_list[index] = N p

  14. Python – ดึงสมาชิกสตริงจากเมทริกซ์ผสม

    เมื่อต้องการดึงเฉพาะสมาชิกที่เป็นสตริงจากเมทริกซ์ที่มีข้อมูลผสม (mixed matrix) สามารถใช้ list comprehension และ isinstance() ได้ ตัวอย่าง my_list = [[35, 66, 31], [python, 13, is], [15, fun, 14]] print(The list is :) print(my_list) my_result = [element for index in my_list for element in index if is

  15. Python – เรียงลำดับสตริงตามช่วง substring

    เมื่อต้องการเรียงลำดับสตริงตามช่วง substring ที่กำหนด สามารถกำหนดฟังก์ชันช่วยที่ใช้ list slicing ได้ ตัวอย่าง def get_substring(my_string): return my_string[i:j] my_list = [python, is, fun, to, learn] print(The list is :) print(my_list) i, j = 1, 3 print(The value of i and j are :) print(str(i)

  16. Python – ดึงคีย์เวิร์ดจากลิสต์

    เมื่อต้องการดึงคีย์เวิร์ด (keyword) ของ Python จากลิสต์สตริง สามารถใช้ลูป iteration และ keyword.iskeyword() ได้ ตัวอย่าง import keyword my_list = [python, is, fun, to, learn] print(The list is :) print(my_list) my_result = [] for element in my_list: for word in element.split(): if keywor

  17. Python – ลบแถวที่มีสมาชิกซ้ำในเมทริกซ์

    เมื่อต้องการลบแถว (สับลิสต์) ที่มีสมาชิกซ้ำกันในเมทริกซ์ สามารถใช้ list comprehension และ set() ได้ ตัวอย่าง my_list = [[34, 23, 34], [17, 46, 47], [22, 14, 22], [28, 91, 19]] print(The list is :) print(my_list) my_result = [element for element in my_list if len(set(element)) == len(element)] print(

  18. Python – หาดัชนีของสมาชิกลิสต์หนึ่งในอีกลิสต์หนึ่ง

    เมื่อต้องการหาดัชนีของสมาชิกแต่ละตัวในลิสต์หนึ่งปรากฏในอีกลิสต์หนึ่ง สามารถใช้ enumerate(), setdefault() และ list comprehension ได้ ตัวอย่าง my_list = [14, 52, 23, 47, 18, 23, 12, 54, 43, 22, 28, 13] print(The list is :) print(my_list) my_list_2 = [17, 52, 13] print(The second list is :) print(my_li

  19. Python – หาผลรวมตัวเลขติดกันแบบทับซ้อนในลิสต์

    เมื่อต้องการหาผลรวมของตัวเลขติดกันแบบทับซ้อน (overlapping) ในลิสต์ สามารถใช้ list comprehension, list slicing, ตัวดำเนินการ + และ zip() ได้ ตัวอย่าง my_list = [41, 27, 53, 12, 29, 32, 16] print(The list is :) print(my_list) my_result = [a + b for a, b in zip(my_list, my_list[1:] + [my_list[0]])] pri

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:328/451  20-คอมพิวเตอร์/Page Goto:1 322 323 324 325 326 327 328 329 330 331 332 333 334