Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. วิธี Clamp ตัวเลขทศนิยมใน Python

    Clamp function จำกัดค่าให้อยู่ในช่วงที่กำหนด Python ไม่มีฟังก์ชันนี้ built-in สร้างได้ดังนี้ def clamp(num, min_value, max_value): return max(min(num, max_value), min_value) print(clamp(5, 1, 20)) print(clamp(1, 10, 20)) print(clamp(20, 1, 10)) ผลลัพธ์ 5 10 10

  2. วิธีพิมพ์ Narcissistic (Armstrong) Numbers ด้วย Python

    เพื่อพิมพ์ Narcissistic Numbers มาดูความหมายก่อน คือเลขที่เท่ากับผลรวมของหลักตัวเองแต่ละหลักยกกำลังจำนวนหลัก เช่น 1, 153, 370 เป็น Narcissistic numbers พิมพ์ได้ด้วยโค้ดนี้ def print_narcissistic_nums(start, end): for i in range(start, end + 1): # ดึงหลักตัวเลขในลิสต์ digits = lis

  3. วิธีดึงชื่อสัญญาณ (Signal Names) จากหมายเลขใน Python

    ไม่มีวิธีตรงไปตรงมาดึงชื่อสัญญาณจากหมายเลขใน Python ใช้โมดูล signal ดึง attributes ทั้งหมด แล้วกรองตัวแปรที่ขึ้นต้นด้วย SIG และเก็บใน dict ตัวอย่าง import signal sig_items = reversed(sorted(signal.__dict__.items())) final = dict((k, v) for v, k in sig_items if v.startswith(SIG) and not v.startswith(

  4. วิธีหาค่ารากที่สองของจำนวน مختلطใน Python

    หาค่ารากที่สองของจำนวน مختلطใน Python ด้วยไลบรารี cmath ไลบรารี cmath ใน Python ใช้จัดการจำนวน مختلط ใช้ดังนี้เพื่อหารากที่สอง ตัวอย่าง from cmath import sqrt a = 0.2 + 0.5j print(sqrt(a)) ผลลัพธ์ (0.6076662244659689+0.4114100635092987j)

  5. วิธีพล็อตจำนวน مختلط (Complex Numbers) ใน Python

    สามารถพล็อตจำนวน مختلطบน polar plot ถ้ามีอาร์เรย์ของจำนวน مختلط พล็อตได้ดังนี้ import matplotlib.pyplot as plt import numpy as np cnums = np.arange(5) + 1j * np.arange(6, 11) X = [x.real for x in cnums] Y = [x.imag for x in cnums] plt.scatter(X, Y, color=red) plt.show() จะพล็อตกราฟตัวเลขใน complex p

  6. วิธีหา Keith Numbers ด้วย Python

    ใช้โค้ดต่อไปนี้ตรวจสอบว่าเลขเป็น Keith number ใน Python ตัวอย่าง def is_keith_number(n): # หาผลรวมหลักด้วยการแปลงเป็นอาร์เรย์หลักแล้วบวก c = str(n) a = list(map(int, c)) b = sum(a) # ตรวจสอบว่าเป็น Keith number # เช่น 14 เป็น Keith number เพราะ: # 1+4 = 5 # 4+5 = 9

  7. วิธีลบตัวเลขออกจากสตริงใน Python

    สร้างอาร์เรย์เก็บตัวอักษรที่ไม่ใช่ตัวเลขในสตริง แล้ว join ด้วย .join() ตัวอย่าง my_str = qwerty123asdf32 non_digits = [] for c in my_str: if not c.isdigit(): non_digits.append(c) result = .join(non_digits) print(result) ผลลัพธ์ qwertyasdf ตัวอย่าง ทำได้ในบรรทัดเดียวด้วย list comprehensio

  8. Python - แปลง while loop เป็น for loop อย่างไร?

    ใช้ count() ใน itertools module ให้ iterator ค่าที่ห่างกันเท่าๆ กัน ฟังก์ชันรับสองพารามิเตอร์ start default 0 step default 1 ใช้ default จะได้ infinite iterator ใช้ break หยุดลูป import itertools percentNumbers = [] finish = n num = 0 for x in itertools.count(): num = input(enter the mark : )

  9. ตอบกลับข้อความผู้ใช้ด้วย Python

    แก้ปัญหานี้ด้วย if-elif-else statements และเพื่อให้ถามซ้ำจนกว่าจะเลือกตัวเลือกที่ถูกต้อง ใช้ while loop ตัวเลือกถูกต้อง break loop ไม่อย่างนั้นถามซ้ำ รับอินพุทเป็น integer ต้อง typecast ด้วย int() ตัวอย่าง print(Come-on in. Need help with any bags?) while True: # ลูปใช้รับตัวเลือกจนกว่าจะถูกต้อง

  10. ช่วยแก้โปรแกรม Python นี้หน่อย

    ปัญหาแรกในส่วน bold เกิดจาก non-indent block ให้เพิ่ม indentation ปัญหาที่สอง ตัวแปร name ไม่ได้ถูกกำหนด โค้ดที่แก้แล้ว: print(Come-on in. Need help with any bags?) bag = input((1) Yes please (2) Nah, thanks (3) Ill get em later TYPE THE NUMBER ONLY) if bag == (1): print(Ok, ill be right ther

  11. วิธีลบสมาชิกซ้ำในลิสต์ด้วย Python

    ย้าย return statement นอกบล็อก for จะทำงานได้ นอกจากนี้ print ตัวสุดท้ายควรเป็น remove_same ไม่ใช่ remaove_new def remove_same(L1, L2): L1_copy = L1[:] for e in L1_copy: if e in L2: L1.remove(e) return L1 L1 = [1,2,3,4] L2 = [1,2,5,6] print(remove_same(L1, L2)) ผลลัพ

  12. วิธีเปลี่ยนค่าใน Python Dictionary

    สามารถกำหนดค่า dictionary ให้ตัวแปรด้วย access operator [] เช่น ตัวอย่าง my_dict = { foo: 42, bar: 12.5 } new_var = my_dict[foo] print(new_var) ผลลัพธ์ 42 Syntax นี้ใช้ reassign ค่าที่เกี่ยวข้องกับ key ได้ เช่น ตัวอย่าง my_dict = { foo: 42, bar: 12.5 } my_dict[foo] = Hello print(my_d

  13. หา pattern 10+1 ทั้งหมดในสตริงด้วย Python Regex

    ต้องหา regex pattern 10+1 ในสตริงที่กำหนด ใช้ re module ใน Python มีเมธอด findall รับ regex และสตริงที่ต้องการค้นหา คืน occurrence ทั้งหมดของ pattern นั้น เช่น สตริงอินพุท 10000001 hello world 10011 test100000001test. ควรได้ผลลัพธ์ 10000001 1001 100000001 ใช้ re package ดังนี้ import re occ = re.fin

  14. Python Regex แยกค่าตัวเลขสูงสุดจากสตริง

    วิธีง่ายที่สุดแยกค่าตัวเลขสูงสุดจากสตริงด้วย regex คือ ใช้ regex module แยกตัวเลขทั้งหมดจากสตริง หาค่าสูงสุดจากตัวเลขเหล่านั้น เช่น สตริงอินพุท มีคน 121005 คนในเมืองนี้ 1587469 ในเมืองข้างเคียง และ 18775994 ในเมืองไกล ควรได้ผลลัพธ์ 18775994 ใช้ regex \d+ หาตัวเลขทั้งหมด \d แทน digit และ + หาลำดับ

  15. Regex ใน Python ใส่ช่องว่างระหว่างคำที่ขึ้นต้นด้วยตัวพิมพ์ใหญ่

    ปัญหาคือแปลง CamelCase เป็นคำแยกกัน แก้ได้โดย regex หาตัวพิมพ์ใหญ่ทั้งหมด ใส่ช่องว่างหน้ามัน ใช้ sub จาก re module เช่น สตริงอินพุท AReallyLongVariableNameInJava ควรได้ผลลัพธ์ A Really Long Variable Name In Java ใช้ regex [A-Z] หาตัวพิมพ์ใหญ่ แทนที่ด้วยช่องว่างและตัวอักษรเดิม ใช้ re package ตัวอย่าง

  16. แยกที่อยู่อีเมลด้วย Regular Expressions ใน Python

    ที่อยู่อีเมลซับซ้อน ไม่มีมาตรฐานเดียวทั่วโลก ทำให้ยากที่จะระบุใน regex RFC 5322 ระบุรูปแบบอีเมล ใช้รูปแบบนี้แยกอีเมลจากข้อความ เช่น สตริงอินพุท Hi my name is John and email address is john.doe@somecompany.co.uk and my friends email is jane_doe124@gmail.com ควรได้ผลลัพธ์ john.doe@somecompany.co.uk ja

  17. เรียงคำตามลำดับ Lexicographical ใน Python

    การเรียงคำ lexicographical order หมายถึงจัดเรียงตามตัวอักษรตัวแรกของคำ แล้วคำที่ตัวแรกเหมือนกัน เรียงตามตัวที่สอง ต่อไป เช่นพจนานุกรม Python มี sort และ sorted สำหรับการเรียงนี้ In-place sorting: ใช้ sort() โดยตรง my_arr = [hello, apple, actor, people, dog] print(my_arr) my_arr.sort() print(my_arr)

  18. โปรแกรม Python เรียงคำในประโยคตามลำดับตัวอักษร

    เพื่อเรียงคำในประโยคตามลำดับตัวอักษร ต้อง split ประโยคเป็นคำด้วยช่องว่าง กรณีนี้ split แค่ช่องว่าง ให้ punctuation อยู่ ไม่ได้ลบ สามารถใช้ replace หรือ regex ลบได้ หลัง split เรียงคำ lexicographically (แบบพจนานุกรม) ด้วย sort หรือ sorted ขึ้นอยู่กับต้องการ sort in-place หรือสร้างใหม่ In-place sortin

  19. ลบ Stop Words ด้วย NLTK ใน Python

    เมื่อคอมพิวเตอร์ประมวลผลภาษาธรรมชาติ คำที่พบบ่อยมากซึ่งดูไม่น่ามีค่าในการช่วยเลือกเอกสารตรงความต้องการผู้ใช้ จะถูกตัดออกจากคำศัพท์ทั้งหมด เรียกว่า stop words เช่น ประโยคอินพุท John is a person who takes care of the people around him. หลังลบ stop words จะได้ [John, person, takes, care, people, around

  20. ฟังก์ชัน startswith() และ endswith() ใน Python

    Python มีเมธอด startswith(string) ในคลาส String เมธอดนี้รับ prefix string ที่ต้องการค้นหา และเรียกบนอ็อบเจ็กต์สตริง ใช้ได้ดังนี้ hello world.startswith(nope) False เช่นกันมี endswith สำหรับค้นหา suffix ที่ท้ายสตริง hello world.endswith(nope) False

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:69/451  20-คอมพิวเตอร์/Page Goto:1 63 64 65 66 67 68 69 70 71 72 73 74 75