Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. Python – นับ Distinct Values ใน Pandas Aggregation ด้วย Numpy

    ภาพรวม ใช้ groupby().agg() ร่วมกับ pd.Series.nunique (distinct count) และ np.sum เพื่อสรุปข้อมูล ตัวอย่างโค้ด import pandas as pd import numpy as np dataFrame = pd.DataFrame({ Car: [BMW, Audi, BMW, Lexus, Lexus], Place: [Delhi, Bangalore, Delhi, Chandigarh, Chandigarh], Units: [100, 1

  2. Python – ตรวจสอบว่า List มีเฉพาะ Digits ที่กำหนด (Check Specific Digits Only)

    ภาพรวม ตรวจสอบว่าทุก digit ในทุกตัวเลขของ list อยู่ในชุด digits ที่อนุญาตเท่านั้น ตัวอย่างโค้ด my_list = [427, 789, 345, 122, 471, 124] my_digits = [1, 4, 7, 2] print(The list is:, my_list) # รวม digits ที่อนุญาตเป็น string allowed_digits = .join(str(d) for d in my_digits) # รวมทุก digits ใน my_

  3. Python – แปลง Matrix (List of Lists) เป็น Dictionary Value List

    ภาพรวม แปลง matrix (list of lists) ให้เป็น dictionary ที่ key คือ row number (1-based) และ value คือ row list ตัวอย่างโค้ด my_list = [[71, 26, 35], [65, 56, 37], [89, 96, 99]] print(The list is:, my_list) # Dictionary comprehension my_result = {i + 1: my_list[i] for i in range(len(my_list))} pri

  4. Python – Extract Strings ที่มี Character จากชุดที่กำหนด มากกว่าหรือเท่ากับ K ตัว

    ภาพรวม = key ตัว ตัวอย่างโค้ด = my_key ] print(The resultant list is:, my_result) ผลลัพธ์ The list is: [Python, is, fun, to, learn] The value of key is: 2 The resultant list is: [Python, learn] คำอธิบาย StringChars in setCount=2? Pythont, n2✓ iss1✗ funn1✗ tot1✗ learne, a, r, n (e,

  5. Python – ลบ First Diagonal Elements จาก Square Matrix

    ภาพรวม จาก square matrix (list of lists ขนาด n x n) ให้ลบ element บน main diagonal (index i,i) ออกจากแต่ละแถว ตัวอย่างโค้ด my_list = [ [45, 67, 85, 42, 11], [78, 99, 10, 13, 0], [91, 23, 23, 64, 23], [91, 11, 22, 14, 35] ] print(The list is:, my_list) my_result = [] for i, row in

  6. Python – เรียง Tuple ตามความถี่ของ Absolute Difference

    ภาพรวม จาก list of tuples ให้เรียงตามความถี่ของ |x - y| (absolute difference) ของแต่ละ tuple จากน้อยไปมาก ตัวอย่างโค้ด my_list = [(11, 26), (21, 33), (90, 11), (26, 21), (32, 18), (25, 37)] print(The list is:, my_list) # คำนวณ absolute difference ทุก tuple diff_list = [abs(x - y) for x, y in my_l

  7. Python – แปลง List เป็น Matrix โดยขนาดแถวเพิ่มขึ้นทีละ K

    ภาพรวม จาก list แปลงเป็น matrix (list of lists) โดยแถวแรกมี k elements, แถวที่สองมี 2k, แถวที่สามมี 3k, ... จนหมด list ตัวอย่างโค้ด my_list = [42, 45, 67, 89, 99, 10, 23, 12, 31, 43, 60, 1, 0] my_key = 3 print(The list is:, my_list) print(The value of key is:, my_key) my_result = [] for i in ran

  8. Python – Extract Matrix Rows ที่ Element ทุกตัวมีความถี่เป็นเลขคู่

    ภาพรวม จาก matrix (list of lists) ให้คัดเฉพาะแถวที่ ทุก unique element ในแถวนั้นมีความถี่เป็นเลขคู่ ตัวอย่างโค้ด from collections import Counter my_list = [ [41, 25, 25, 62], [41, 41, 41, 41, 22, 22], [65, 57, 65, 57], [11, 24, 36, 48] ] print(The list is:, my_list) my_result =

  9. Python – แทนที่ Element ใน List ตามการเปรียบเทียบกับ Key (Replace by Comparison)

    ภาพรวม เปรียบเทียบแต่ละ element ใน list กับ key ถ้ามากกว่า key ให้แทนที่ด้วย high value ถ้าน้อยกว่าหรือเท่ากับให้แทนที่ด้วย low value ตัวอย่างโค้ด my_key else low for x in my_list] print(The resultant list is:, my_result) ผลลัพธ์ The list is: [32, 37, 44, 38, 92, 61, 28, 92, 20] The key is: 32

  10. Python – ตรวจสอบว่า List of Strings เป็นตัวเลขทั้งหมด (All Numeric Strings)

    ภาพรวม ใช้ all() ร่วมกับ str.isdigit() ตรวจสอบว่าทุก element ใน list เป็นสตริงตัวเลขทั้งหมด ตัวอย่างโค้ด my_list = [434, 823, 98, 74, 9870] print(The list is:, my_list) my_result = all(s.isdigit() for s in my_list) if my_result: print(All the elements in the list are numeric) else: print

  11. Python – ลบ Digit เฉพาะตัวออกจากทุก Element ใน List

    ภาพรวม 44 ตัวอย่างโค้ด (ต้นฉบับมี logic ผิดปกติ) 0 หรือ skip new_val = int(new_str) if new_str else 0 my_result.append(new_val) print(The result is:, my_result) ผลลัพธ์ (เวอร์ชันแก้ไข) The list is: [123, 565, 1948, 334, 4598] The key is: 3 The result is: [12, 565, 1948, 44, 4598] คำอ

  12. Python – ลบ Index ของ Duplicate Elements จาก List อื่น

    ภาพรวม จาก 2 list ความยาวเท่ากัน ให้ลบ element ใน list ที่ 2 ที่ index ตรงกับตำแหน่งของ duplicate elements ใน list ที่ 1 ตัวอย่างโค้ด my_list_1 = [4, 5, 6, 5, 4, 7, 8, 6] my_list_2 = [1, 7, 6, 4, 7, 9, 10, 11] print(The first list is:, my_list_1) print(The second list is:, my_list_2) # หา index

  13. Python Pandas – แปลง Nested Dictionary เป็น MultiIndex DataFrame

    ภาพรวม แปลง Nested Dictionary (dict ของ dict) ให้เป็น DataFrame ที่มี MultiIndex Columns (หลายระดับหัวคอลัมน์) โครงสร้างข้อมูล dictNested = { Cricket: { Boards: [BCCI, CA, ECB], Country: [India, Australia, England] }, Football: { Boards: [TFA, TCSA, GFA],

  14. Python – กรองสตริงที่ขึ้นต้นด้วย Prefix ใดๆ จาก List (Startswith Any Prefix)

    ภาพรวม จาก list ของสตริง ให้คัดเฉพาะตัวที่ขึ้นต้นด้วย prefix ใด prefix หนึ่ง จาก prefix list ที่กำหนด ตัวอย่างโค้ด my_list = [streek, greet, meet, leeks, mean] prefix_list = [st, ge, me, re] print(The list is:, my_list) print(The prefix list is:, prefix_list) my_result = [ s for s in my_list

  15. Python – ปัดทศนิยมค่าคอลัมน์ใน Pandas DataFrame (Display Precision)

    ภาพรวม ปรับจำนวนหลักทศนิยมที่แสดงผลของ DataFrame ใช้ pd.set_option(display.precision, n) – ส่งผลต่อการแสดงผลเท่านั้น ไม่เปลี่ยนข้อมูลจริง ตัวอย่างโค้ด import pandas as pd dataFrame = pd.DataFrame({ Car: [BMW, Lexus, Tesla, Mustang, Mercedes, Jaguar], Reg_Price: [7000.5057, 1500, 5000.9578

  16. Python – หาตัวเลขที่หารลงตัวด้วยทุกค่าใน List ที่กำหนด (Multiples of All Divisors)

    ภาพรวม จาก list ของตัวเลข ให้คัดเฉพาะตัวที่หารด้วย ทุกค่า ใน divisor list ลงตัว (remainder = 0) ตัวอย่างโค้ด my_list = [45, 67, 89, 90, 10, 98, 10, 12, 23] my_division_list = [6, 4] print(The list is:, my_list) print(The division list is:, my_division_list) my_result = [ element for element

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:341/451  20-คอมพิวเตอร์/Page Goto:1 335 336 337 338 339 340 341 342 343 344 345 346 347