Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. เปรียบเทียบความยาวสตริงสองสตริงโดยไม่ใช้ Built-in Function ใน Python

    เมื่อต้องการหาสตริงที่ยาวกว่าระหว่างสองสตริงโดยไม่ใช้ len() ให้วนลูปนับตัวอักษรเอง แล้วเปรียบเทียบตัวอย่างโค้ดs1 = Hi there s2 = Hi how are ya print(First string:, s1) print(Second string:, s2) c1 = c2 = 0 for ch in s1: c1 += 1 for ch in s2: c2 += 1 if c1 < c2: print(Longer string:, s

  2. นับจำนวนตัวอักษรพิมพ์เล็ก (Lowercase) ในสตริง Python

    เมื่อต้องการนับตัวอักษรพิมพ์เล็กในสตริง ใช้เมธอด islower() ร่วมกับลูป forตัวอย่างโค้ดmy_string = Hi there how are you print(The string is:, my_string) count = 0 for ch in my_string: if ch.islower(): count += 1 print(Number of lowercase characters:, count)ผลลัพธ์The string is: Hi there

  3. ดึงค่าที่ไม่ซ้ำกัน (Unique Values) จากดิกชันนารีใน Python

    เมื่อต้องการรวมค่าทั้งหมดจากทุกลิสต์ในดิกชันนารี และลบค่าซ้ำ แล้วเรียงลำดับ ใช้ Set Comprehension ร่วมกับ sorted()ตัวอย่างโค้ดmy_dict = { hi: [5, 3, 8, 0], there: [22, 51, 63, 77], how: [7, 0, 22], are: [12, 11, 45], you: [56, 31, 89, 90] } print(The dictionary is:) print(my_dic

  4. เรียงลำดับลิสต์ของดิกชันนารีตามค่าด้วย itemgetter ใน Python

    ใช้ operator.itemgetter แทน lambda เพื่อประสิทธิภาพและความอ่านง่ายเมื่อเรียงลิสต์ดิกชันนารีตามค่าตัวอย่างโค้ดfrom operator import itemgetter people = [ {name: Will, age: 56}, {name: Rob, age: 20}, {name: Mark, age: 34}, {name: John, age: 24} ] print(Sorted by age:) print(sorted(peo

  5. เรียงลำดับลิสต์ของดิกชันนารีตามค่าด้วย Lambda Function ใน Python

    เมื่อต้องการเรียงลำดับลิสต์ที่มีดิกชันนารีเป็นองค์ประกอบ ตามค่าเฉพาะ (เช่น age) ใช้ sorted() กับ lambdaตัวอย่างโค้ดfrom operator import itemgetter people = [ {name: Will, age: 56}, {name: Rob, age: 20}, {name: Mark, age: 34}, {name: John, age: 24} ] print(Sorted by age:) print(sort

  6. แทรกข้อมูลที่จุดเริ่มต้นของ OrderedDict ใน Python

    เมื่อต้องการเพิ่มคีย์-ค่าใหม่ที่จุดเริ่มต้นของ OrderedDict ใช้ update() แล้วตามด้วย move_to_end(key, last=False)ตัวอย่างโค้ดfrom collections import OrderedDict od = OrderedDict([(Will, 1), (James, 2), (Rob, 4)]) print(The dictionary is:) print(od) od.update({Mark: 7}) od.move_to_end(Mark, last=Fal

  7. ตรวจสอบลำดับตัวอักษรในสตริงด้วย OrderedDict ใน Python

    เมื่อต้องการตรวจสอบว่าตัวอักษรในรูปแบบ (pattern) ปรากฏในสตริงตามลำดับที่ถูกต้องหรือไม่ ใช้ OrderedDict.fromkeys() เพื่อเก็บลำดับตัวอักษรที่ไม่ซ้ำตัวอย่างโค้ดfrom collections import OrderedDict def check_order(text, pattern): od = OrderedDict.fromkeys(text) pattern_idx = 0 for char

  8. เรียงลำดับดิกชันนารีใน Python ตามคีย์หรือค่า

    เมื่อต้องการเรียงลำดับดิกชันนารีตามคีย์หรือค่า สามารถใช้ sorted() กับ items() และระบุ key parameterตัวอย่างโค้ดdef sort_dict(): d = {} d[2] = 56 d[1] = 2 d[5] = 12 d[4] = 24 d[6] = 18 d[3] = 323 print(Sorted by key:) for k in sorted(d): print((k, d[k]))

  9. เรียงลำดับคีย์และค่า (ที่เป็นลิสต์) ในดิกชันนารี Python

    เมื่อต้องการเรียงลำดับดิกชันนารีทั้งตามคีย์และตามค่า (ที่เป็นลิสต์) ภายใน ใช้ sorted() ร่วมกับ Dictionary Comprehensionตัวอย่างโค้ดmy_dict = {Hi: [1, 6, 3], there: [2, 9, 6], Mark: [16, 7]} print(The dictionary is:) print(my_dict) result = {} for key in sorted(my_dict): result[key] = sorted(my_

  10. สร้างดิกชันนารีที่มีคีย์เป็นทัพเพิลหลายค่าใน Python

    ใน Python คีย์ของดิกชันนารีสามารถเป็นทัพเพิลได้ (เพราะทัพเพิล immutable และ hashable) ทำให้เก็บค่าได้หลายมิติตัวอย่างโค้ดmy_dict = {} a, b, c = 15, 26, 38 my_dict[a, b, c] = a + b - c a, b, c = 5, 4, 11 my_dict[a, b, c] = a + b - c print(The dictionary is:) print(my_dict)ผลลัพธ์The dictionary is:

  11. ตัวอย่างการใช้ Counter และ Dictionary Intersection ใน Python

    เมื่อต้องการตรวจสอบว่าสตริงหนึ่งสามารถสร้างจากตัวอักษรของอีกสตริงหนึ่งได้หรือไม่ ใช้ Counter นับตัวอักษร แล้วใช้ตัวดำเนินการ & (intersection)ตัวอย่างโค้ดfrom collections import Counter def can_form(str1, str2): c1 = Counter(str1) c2 = Counter(str2) intersection = c1 & c2 return inte

  12. ตรวจสอบความถี่ของตัวอักษรในสตริงว่าสามารถทำให้เท่ากันได้หรือไม่ด้วย Counter และ Set

    เมื่อต้องการตรวจสอบว่าความถี่ของตัวอักษรในสตริงสามารถทำให้เท่ากันได้โดยการลบตัวอักษรออกได้สูงสุด 1 ตัว ใช้ Counter นับความถี่ แล้วตรวจสอบเงื่อนไขตัวอย่างโค้ด 1: print(The frequencies are not same) else: print(The frequencies are same) my_str = xxxyyyzzzzzz print(The string is:,

  13. หาคีย์ที่เชื่อมโยงกับค่าเฉพาะในดิกชันนารี Python

    เมื่อต้องการหาคีย์ที่มีค่าเท่ากับค่าที่กำหนดในดิกชันนารี สามารถแปลงคีย์และค่าเป็นลิสต์ แล้วใช้ index() หาตำแหน่งตัวอย่างโค้ดmy_dict = {Hi: 100, there: 121, Mark: 189} print(The dictionary is:) print(my_dict) keys = list(my_dict.keys()) print(The keys are:, keys) values = list(my_dict.values()) pri

  14. โปรแกรม Python หาขนาดหน่วยความจำของทัพเพิล (Tuple Size)

    เมื่อต้องการทราบขนาดหน่วยความจำ (bytes) ที่ทัพเพิลใช้ สามารถใช้ sys.getsizeof()ตัวอย่างโค้ดimport sys tuple_1 = (A, 1, B, 2, C, 3) tuple_2 = (Java, Lee, Code, Mark, John) tuple_3 = ((1, Bill), (2, Ant), (3, Fox), (4, Cheetah)) print(The first tuple is:) print(tuple_1) print(The second tuple is:) p

  15. หาค่าสูงสุดและต่ำสุด K ค่าในทัพเพิลด้วย Python

    เมื่อต้องการหา K ค่าที่มากที่สุดและ K ค่าที่น้อยที่สุดในทัพเพิล สามารถเรียงลำดับแล้วหยิบ K ตัวแรกและ K ตัวสุดท้ายตัวอย่างโค้ดmy_tuple = (7, 25, 36, 9, 6, 8) print(The tuple is:, my_tuple) K = 2 print(The value of K is:, K) temp = sorted(my_tuple) result = [] for idx, val in enumerate(temp): i

  16. สร้างลิสต์ของทัพเพิลจากลิสต์ตัวเลข โดยแต่ละทัพเพิลมีตัวเลขและกำลังสามของมัน

    เมื่อต้องการสร้างลิสต์ทัพเพิลจากลิสต์ตัวเลข โดยแต่ละทัพเพิลประกอบด้วย (ตัวเลข, ตัวเลข^3) ใช้ List Comprehension กับ pow() หรือ **ตัวอย่างโค้ดmy_list = [32, 54, 47, 89] print(The list is:) print(my_list) result = [(val, pow(val, 3)) for val in my_list] print(The result is:) print(result)ผลลัพธ์The l

  17. เพิ่มทัพเพิลเข้าไปในลิสต์ และกลับกันใน Python

    เมื่อต้องการรวมทัพเพิลเข้ากับลิสต์ หรือลิสต์เข้ากับทัพเพิล สามารถใช้ตัวดำเนินการ + ได้โดยตรงตัวอย่างโค้ดmy_list = [3, 6, 9, 45, 66] print(The list is:) print(my_list) my_tuple = (11, 14, 21) print(The tuple is:) print(my_tuple) my_list += my_tuple print(The list after addition is:) print(my_list)ผ

  18. หาทัพเพิลที่ใกล้เคียง K-th index มากที่สุดใน Python

    เมื่อต้องการหาทัพเพิลในลิสต์ที่มีค่าใน index K ใกล้เคียงกับค่าใน index K ของทัพเพิลอ้างอิงมากที่สุด ใช้ enumerate และ abs()ตัวอย่างโค้ดmy_list = [(5, 6), (66, 76), (21, 35), (90, 8), (9, 0)] print(The list is:) print(my_list) target = (17, 23) print(The target tuple is:) print(target) K = 2 print(

  19. รวมทัพเพิลที่มีองค์ประกอบแรกเหมือนกันใน Python

    เมื่อต้องการรวมทัพเพิลที่อยู่ติดกันและมีองค์ประกอบแรก (index 0) เหมือนกัน สามารถใช้ลูป for ตรวจสอบและใช้ extend() รวมค่าตัวอย่างโค้ดmy_list = [(43, 15), (66, 98), (64, 80), (14, 9), (47, 17)] print(The list is:) print(my_list) result = [] for sub in my_list: if result and result[-1][0] == sub[0

  20. แยกตัวเลขที่มีจำนวนหลักเฉพาะจากลิสต์ทัพเพิลใน Python

    เมื่อต้องการกรองทัพเพิลในลิสต์ ให้เหลือเฉพาะทัพเพิลที่ทุกองค์ประกอบมีจำนวนหลัก (digits) เท่ากับ N ที่กำหนดตัวอย่างโค้ดmy_list = [(67, 2), (34, 65), (212, 23), (17, 67), (18,)] print(The list is:) print(my_list) N = 2 print(The value of N is:, N) result = [sub for sub in my_list if all(len(str(ele)

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