Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. Python – รวมสตริงรอบๆ ตัวคั่น K

    เมื่อต้องการรวมสตริงรอบๆ ตัวคั่น K ในลิสต์ สามารถใช้ลูป iteration และ append() ได้ ตัวอย่าง my_list = [python, +, is, fun, +, to, learn] print(The list is :) print(my_list) K = + print(The value of K is :) print(K) my_result = [] index = 0 while index < len(my_list): element = my_list[index]

  2. Python – แทนที่ตัวอักษรทั้งหมดในลิสต์ยกเว้นตัวที่กำหนด

    เมื่อต้องการแทนที่ตัวอักษรทั้งหมดในลิสต์ยกเว้นตัวอักษรที่กำหนดให้คงไว้ สามารถใช้ list comprehension และตัวดำเนินการ == ได้ ตัวอย่าง my_list = [P, Y, T, H, O, N, P, H, P] print(The list is :) print(my_list) replace_char = $ retain_char = P my_result = [element if element == retain_char else replace_c

  3. Python – หาตำแหน่งตัวอักษรของคำที่ K จากลิสต์สตริง

    เมื่อต้องการหาตำแหน่งตัวอักษร (character position) ของคำที่ K จากลิสต์สตริง สามารถใช้ list comprehension ร่วมกับ enumerate() ได้ ตัวอย่าง my_list = [python, is, fun, to, learn] print(The list is :) print(my_list) K = 15 print(The value of K is :) print(K) my_result = [element[0] for sub in enumerate

  4. Python – หาการจัดหมู่ทั้งหมดในลิสต์ตามเงื่อนไข

    เมื่อต้องการหาการจัดหมู่ (combinations) ทั้งหมดในลิสต์ที่มีโครงสร้างผสมตามเงื่อนไข สามารถใช้ลูป iteration, append() และ isinstance() ได้ ตัวอย่าง my_list = [python, [15, 12, 33, 14], is, [fun, easy, better, cool]] print(The list is :) print(my_list) K = 4 print(The value of K is :) print(K) my_resul

  5. Python – Cross Join ทุก K ส่วนของลิสต์

    เมื่อต้องการทำ cross join (สลับกัน) ทุก K ส่วนของสองลิสต์ สามารถใช้ generator function ที่มีลูป iteration และ yield ได้ ตัวอย่าง def merge_pair_elem(my_list_1, my_list_2, K): index_1 = 0 index_2 = 0 while index_1 < len(my_list_1): for i in range(K): yield my_list_1[in

  6. Python – เรียงลำดับลิสต์ตามผลรวมหลักของตัวเลข

    เมื่อต้องการเรียงลำดับสมาชิกในลิสต์ตามผลรวมของหลักตัวเลข (digit sum) สามารถกำหนดฟังก์ชันช่วยที่ใช้ list comprehension และ sum() ได้ ตัวอย่าง def sort_list_digit(my_list): digits = [int(digit) for digit in str(my_list)] return sum(digits) my_list = [124, 20, 106, 35, 44] print(The list is :)

  7. Python – แปลง Tuple เป็น List โดยเพิ่มสตริงหลังทุกสมาชิก

    เมื่อต้องการแปลง tuple เป็น list โดยเพิ่มสตริงที่กำหนดหลังทุกสมาชิก สามารถใช้ list comprehension ได้ ตัวอย่าง my_tuple = ((15, 16), 71, 42, 99) print(The tuple is :) print(my_tuple) K = Pyt print(The value of K is :) print(K) my_result = [element for sub in my_tuple for element in (sub, K)] print(Th

  8. Python – หา Tuple ที่มีสมาชิกเป็นบวกทั้งหมดในลิสต์ของ Tuple

    เมื่อต้องการหา tuple ที่มีสมาชิกเป็นจำนวนบวกหรือศูนย์ทั้งหมดในลิสต์ของ tuple สามารถใช้ list comprehension และตัวดำเนินการ all() ได้ ตัวอย่าง = 0 for element in sub)] print(The result is :) print(my_result) ผลลัพธ์ The list is : [(14, 15, 19), (-32, 23, 32), (-31, 15, 63), (46, 68)] The result is : [

  9. Python – จัดกลุ่มสมาชิกในเมทริกซ์

    เมื่อต้องการจัดกลุ่มสมาชิกในเมทริกซ์ (ลิสต์ของลิสต์) ตามลิสต์ตรวจสอบ สามารถใช้ลูป iteration, pop(), list comprehension และ append() ได้ ตัวอย่าง my_list = [[14, 62], [51, 23], [12, 62], [78, 87], [41, 14]] print(The list is :) print(my_list) check_list = [14, 12, 41, 62] print(The list is :) print(c

  10. Python – จำกัดความถี่ของสมาชิกในลิสต์

    เมื่อต้องการจำกัดความถี่ของสมาชิกในลิสต์ให้ไม่เกินค่าที่กำหนด สามารถใช้ลูป iteration, defaultdict และ append() ได้ ตัวอย่าง my_dict[element]: continue else: my_result.append(element) print(The result is :) print(my_result) ผลลัพธ์ The list is : [11, 14, 15, 14, 11, 14, 14, 15, 1

  11. Python – รวม Tuple เป็น Key ของ Dictionary

    เมื่อต้องการรวม tuple ให้เป็น key ของ dictionary สามารถใช้ลูป iteration และเมธอด join() ได้ ตัวอย่าง my_list = [((pyt, is, best), 10), ((pyt, cool), 1), ((pyt, is, fun), 15)] print(The list is :) print(my_list) my_result = {} for sub_list in my_list: my_result[ .join(sub_list[0])] = sub_list[1]

  12. Python – คำนวณความถี่สัมพัทธ์ของสมาชิกในลิสต์

    เมื่อต้องการคำนวณความถี่สัมพัทธ์ (fractional frequency) ของแต่ละสมาชิกในลิสต์ สามารถใช้ dictionary comprehension, Counter จาก collections และลูป iteration ได้ ตัวอย่าง from collections import Counter my_list = [14, 15, 42, 60, 75, 50, 45, 55, 14, 60, 48, 65] print(The list is :) print(my_list) my_nu

  13. Python – ดึงสมาชิกจากช่วงดัชนีในลิสต์

    เมื่อต้องการดึงสมาชิกจากช่วงดัชนี (ranges) ในลิสต์ สามารถใช้ลูป iteration และเมธอด extend() ได้ ตัวอย่าง my_list = [14, 55, 41, 14, 17, 59, 22, 25, 14, 69, 42, 66, 99, 19] print(The list is :) print(my_list) range_list = [(12, 14), (17, 18), (22, 28)] print(The list is :) print(range_list) my_result

  14. Python – แยกลิสต์ตามความยาวที่กำหนด

    เมื่อต้องการแยกลิสต์ตามความยาวที่กำหนดในลิสต์อีกอันหนึ่ง สามารถใช้ list comprehension, iter() และ islice() จากโมดูล itertools ได้ ตัวอย่าง from itertools import islice my_list = [11, 22, 33, 34, 45, 26, 87, 11] print(The list is :) print(my_list) length_to_split = [2, 5, 3] print(The split length li

  15. Python – หาสมาชิกคู่ที่ K ในลิสต์

    เมื่อต้องการหาสมาชิกคู่ (even element) ที่ K ในลิสต์ สามารถใช้ list comprehension และตัวดำเนินการ % ได้ ตัวอย่าง my_list = [14, 63, 28, 32, 18, 99, 13, 61] print(The list is :) print(my_list) K = 3 print(The value of K is :) print(K) my_result = [element for element in my_list if element % 2 == 0][K

  16. Python – รวมสมาชิกตัวสุดท้ายของ Tuple ในลิสต์

    เมื่อต้องการรวม (concatenate) สมาชิกตัวสุดท้ายของทุก tuple ในลิสต์ สามารถใช้ list comprehension และเมธอด join() ได้ ตัวอย่าง my_tuple = [(13, 42, Will), (48, is a), (good boy,)] print(The tuple is : ) print(my_tuple) my_result = .join([sub[-1] for sub in my_tuple]) print(The result is : ) print(my_

  17. Python – ดึง Tuple ที่เรียงลำดับจากน้อยไปมาก

    เมื่อต้องการดึง tuple ที่มีสมาชิกเรียงลำดับจากน้อยไปมาก (ordered tuples) สามารถใช้ list comprehension, sorted(), tuple() และตัวดำเนินการ == ได้ ตัวอย่าง my_list = [(15, 74, 36, 22, 54), (13, 24, 56), (59, 60, 34), (42, 65, 56), (99, 91)] print(The list is :) print(my_list) my_result = [element for e

  18. Python – ดึงสมาชิกที่ K ของทุก Tuple ที่ N ในลิสต์

    เมื่อต้องการดึงสมาชิกที่ K ของทุก tuple ที่ N ในลิสต์ สามารถใช้ลูปiteration และเมธอด append() ได้ ตัวอย่าง my_list = [(54, 51, 23), (73, 24, 47), (24, 33, 72), (64, 27, 18), (63, 24, 67), (12, 25, 77), (31, 39, 80), (33, 55, 78)] print(The list is :) print(my_list) K = 1 print(The value of K is :) p

  19. Python – ดึง Tuple ที่มีสมาชิกอยู่ในช่วงที่กำหนด

    เมื่อต้องการดึง tuple ที่มีสมาชิกทุกตัวอยู่ในช่วงที่กำหนด สามารถใช้ฟังก์ชัน filter() ร่วมกับ lambda ได้ ตัวอย่าง = beg and element <= end for element in sub), my_list)) print(The result is :) print(my_result) ผลลัพธ์ The list is : [(13, 15, 17), (25, 56), (13, 21, 19), (44, 14)] The result is : [(13

  20. Python – กรอง Tuple ที่มีเฉพาะจำนวนเต็ม

    เมื่อต้องการกรอง tuple ที่มีเฉพาะจำนวนเต็ม ใช้การวนลูปง่าย ตัวดำเนินการ not และเมธอด isinstance ตัวอย่าง สาธิตด้านล่าง: my_tuple = [(14, 25, Python), (5, 6), (3, ), (cool, )] print(The tuple is :) print(my_tuple) my_result = [] for sub in my_tuple: temp = True for element in sub:

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