Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. Find Maximum Value in Each Sublist ใน Python: หาค่าสูงสุดของแต่ละสับลิสต์

    ได้ลิสต์ของลิสต์ หาค่าสูงสุดในแต่ละสับลิสต์ วิธี 1: List Comprehension กับ max() Alist = [[10, 13, 454, 66, 44], [10, 8, 7, 23]] res = [max(sub) for sub in Alist] print(res) # [454, 23] วิธี 2: ใช้ map(max, ...) Alist = [[10, 13, 454, 66, 44], [10, 8, 7, 23]] res = list(map(max, Alist)) print(re

  2. Find Most Common Element ใน 2D List Python: หาสมาชิกที่พบบ่อยที่สุดในลิสต์ซ้อน

    ได้ 2D List (ลิสต์ของลิสต์) หาสมาชิกที่ปรากฏบ่อยที่สุดรวมทุกสับลิสต์ วิธี 1: Flatten ด้วย List Comprehension + max/count def highest_freq(lst): flat = [el for sub in lst for el in sub] return max(flat, key=flat.count) listA = [[45,20,11],[20,17,45],[20,13,9]] print(highest_freq(listA)) #

  3. Find Most Frequent Element ใน List Python: หาสมาชิกที่ปรากฏบ่อยที่สุด

    ได้ลิสต์ หาสมาชิกที่มีความถี่สูงสุด (Mode) วิธี 1: ใช้ max() กับ set() และ count() listA = [45, 20, 11, 50, 17, 45, 50, 13, 45] res = max(set(listA), key=listA.count) print(res) # 45 วิธี 2: ใช้ collections.Counter (แนะนำ สำหรับข้อมูลมาก) from collections import Counter listA = [45, 20, 11, 50,

  4. Find List Elements Starting with Specific Letter ใน Python

    ได้ลิสต์สตริง และตัวอักษรเป้าหมาย หาสมาชิกทั้งหมดที่ขึ้นต้นด้วยตัวอักษรนั้น (ไม่สนใจพิมพ์ใหญ่/เล็ก) วิธี 1: ใช้ Index [0] กับ lower() listA = [Mon, Tue, Wed, Thu] target = T res = [s for s in listA if s[0].lower() == target.lower()] print(res) # [Tue, Thu] วิธี 2: ใช้ startswith() (Pythonic กว่า

  5. Find Number Occurring Odd Number of Times ด้วย Lambda และ Reduce Python

    ได้ลิสต์ตัวเลข หาตัวเลขที่ปรากฏจำนวนครั้งเป็นคี่ (Odd) โดยใช้ lambda และ reduce (XOR Operation) หลักการ XOR ของตัวเลขเดียวกันคู่กันจะเป็น 0 (a ^ a = 0) ตัวที่ปรากฏคี่ครั้งจะเหลืออยู่ การติดตั้งใน Python from functools import reduce def odd_count(lst): return reduce(lambda x, y: x ^ y, lst) l

  6. Find Sublist with Maximum Value ใน Nested List Python

    ได้ Nested List (ลิสต์ของลิสต์) แต่ละสับลิสต์มี 2 สมาชิก [key, value] หาสับลิสต์ที่มี value สูงสุด วิธี 1: ใช้ max() กับ lambda listA = [[Mon, 90], [Tue, 32], [Wed, 120]] res = max(listA, key=lambda x: x[1]) print(res) # [Wed, 120] วิธี 2: ใช้ operator.itemgetter(1) import operator listA = [[Mon

  7. Find Tuples Containing Given Element จาก List of Tuples Python

    ได้ลิสต์ของ Tuple และค่าที่ต้องการค้นหา ต้องการ Tuple ทุกตัวที่มีค่านั้น (ในตำแหน่งใดก็ได้) วิธี 1: List Comprehension กับ in listA = [(Mon, 3), (Tue, 1), (Mon, 2), (Wed, 3)] target = Mon res = [t for t in listA if target in t] print(res) # [(Mon, 3), (Mon, 2)] วิธี 2: ใช้ filter() กับ lambda li

  8. Find Top K Frequent Elements จาก List of Tuples ใน Python

    ได้ลิสต์ของ Tuple (แต่ละ Tuple มี key และค่า) หา Top K คีย์ที่มีค่ารวมหรือความถี่สูงสุด ตัวอย่าง [[(Mon,126)],[(Tue,768)],[(Wed,512)],[(Thu,13)],[(Fri,341)]], K=3 → Top 3: [(Tue,768), (Wed,512), (Fri,341)] วิธี 1: ใช้ defaultdict รวมค่า แล้ว sorted import collections from operator import itemgette

  9. Find Whether All Tuples Have Same Length ใน Python: ตรวจสอบความยาว Tuple เท่ากันทุกตัว

    ได้ลิสต์ของ Tuple ตรวจสอบว่าทุก Tuple มีความยาวเท่ากันหรือไม่ วิธี 1: ใช้ len() ในลูป For listA = [(Mon, 2 pm, Physics), (Tue, 11 am, Maths)] k = 3 res = 1 for t in listA: if len(t) != k: res = 0 break print(Each tuple has same length if res else All tuples are not of same lengt

  10. Finding Relative Order of Elements ใน List Python: หาลำดับสัมพัทธ์ของสมาชิก

    ได้ลิสต์จำนวนเต็ม ต้องการ ลำดับสัมพัทธ์ คือ ถ้าเรียงจากน้อยไปมาก แต่ละตัวอยู่ตำแหน่งที่เท่าไหร่ ตัวอย่าง [78, 14, 0, 11] → เรียงได้ [0, 11, 14, 78] → ดัชนี: 78→3, 14→2, 0→0, 11→1 → ผลลัพธ์ [3, 2, 0, 1] วิธี 1: ใช้ sorted() กับ index() listA = [78, 14, 0, 11] res = [sorted(listA).index(x) for x in

  11. First Non-Empty String ใน List Python: หาสตริงไม่ว่างแรก

    ได้ลิสต์สตริง อาจมีสตริงว่างหน้าหลายตัว ต้องการสตริงแรกที่ไม่ใช่สตริงว่าง วิธี 1: ใช้ next() กับ Generator listA = [, top, pot, hot, , shot] res = next(s for s in listA if s) print(res) # top วิธี 2: ใช้ filter() (Python 2) / list(filter(...))[0] (Python 3) listA = [, top, pot, hot, , shot] #

  12. First Occurrence of True Number ใน Python: หาตำแหน่งตัวเลขไม่ศูนย์แรก

    ได้ลิสต์ตัวเลข ต้องการดัชนีของตัวเลขแรกที่ไม่ใช่ 0 วิธี 1: ใช้ enumerate กับ next() listA = [0, 0, 13, 4, 17] res = next((i for i, v in enumerate(listA) if v), None) print(res) # 2 วิธี 2: ใช้ next, filter, lambda listA = [0, 0, 13, 4, 17] res = listA.index(next(filter(lambda x: x != 0, listA)))

  13. Flatten 2D NumPy Array เป็น 1D Array ใน Python

    ได้อาร์เรย์ NumPy 2 มิติ ต้องการทำให้เป็น 1 มิติ วิธี 1: ใช้ flatten() (สร้าง Copy) import numpy as np arr2D = np.array([[31, 12, 43], [21, 9, 16], [0, 9, 0]]) res = arr2D.flatten() print(res) # [31 12 43 21 9 16 0 9 0] วิธี 2: ใช้ ravel() (คืน View ถ้าเป็นไปได้ ประหยัดหน่วยความจำ) import numpy

  14. Flatten List of Dictionaries ใน Python: รวม Dictionary หลายตัวเป็นตัวเดียว

    ได้ลิสต์ที่แต่ละสมาชิกเป็น Dictionary ต้องการรวมเป็น Dictionary เดียว วิธี 1: ใช้ for กับ update() listA = [{Mon:2}, {Tue:11}, {Wed:3}] res = {} for d in listA: res.update(d) print(res) # {Mon: 2, Tue: 11, Wed: 3} วิธี 2: ใช้ functools.reduce กับ update() from functools import reduce listA =

  15. Get First Element with Maximum Value ใน List of Tuples Python

    ได้ลิสต์ของ Tuple ต้องการ Tuple ที่มีค่า (index 1) สูงสุด หากมีหลาย Tuple ค่าเท่ากัน ให้ Tuple แรกที่เจอ วิธี 1: max() กับ operator.itemgetter(1) from operator import itemgetter listA = [(Mon, 3), (Tue, 20), (Wed, 9)] res = max(listA, key=itemgetter(1))[0] print(res) # Tue วิธี 2: max() กับ lamb

  16. Get First Index Values ใน Tuple of Strings Python: ดึงตัวอักษรแรกของแต่ละสตริง

    ได้ Tuple ของสตริง ต้องการลิสต์ของตัวอักษรแรกแต่ละสตริง วิธี 1: ใช้ Index ใน Generator Expression tupA = (Mon, Tue, Wed, Fri) res = [s[0] for s in tupA] print(res) # [M, T, W, F] วิธี 2: ใช้ next(zip(*tupA)) tupA = (Mon, Tue, Wed, Fri) res = list(next(zip(*tupA))) print(res) # [M, T, W, F] คำ

  17. Get Indices of True Values ใน Binary List Python: หาตำแหน่งค่า True

    ได้ Binary List (มีค่า True/False หรือ 1/0) ต้องการดัชนีของทุกตำแหน่งที่เป็น True วิธี 1: ใช้ enumerate กับ List Comprehension listA = [True, False, 1, False, 0, True] res = [i for i, val in enumerate(listA) if val] print(res) # [0, 2, 5] วิธี 2: ใช้ itertools.compress from itertools import comp

  18. Get Key with Maximum Value ใน Dictionary Python: หาคีย์ที่มีค่าสูงสุด

    ได้ Dictionary ต้องการคีย์ที่มีค่า (value) สูงสุด วิธี 1: ใช้ max() กับ dict.get dictA = {Mon: 3, Tue: 11, Wed: 8} max_key = max(dictA, key=dictA.get) print(max_key) # Tue วิธี 2: ใช้ operator.itemgetter กับ max() import operator dictA = {Mon: 3, Tue: 11, Wed: 8} max_key = max(dictA.items(), key

  19. Get Last N Elements จากลิสต์ใน Python: ดึง N ตัวท้ายด้วย Slicing และ islice

    ได้ลิสต์ ต้องการดึง N ตัวสุดท้าย วิธี 1: List Slicing (ง่ายที่สุด) listA = [Mon,Tue,Wed,Thu,Fri,Sat] n = 4 res = listA[-n:] print(res) # [Wed, Thu, Fri, Sat] วิธี 2: ใช้ itertools.islice กับ reversed from itertools import islice listA = [Mon,Tue,Wed,Thu,Fri,Sat] n = 4 res = list(islice(reversed(

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:146/451  20-คอมพิวเตอร์/Page Goto:1 140 141 142 143 144 145 146 147 148 149 150 151 152