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

    กำหนด dictionary ที่ value เป็นลิสต์ ต้องการสร้างลิสต์ของ dictionary ทุก combination ของ key-value ตัวอย่าง: {Day: [Tue,Wed], Time: [2pm,9am]} → 4 combinations ใช้ sorted และ itertools.product product(*dict.values()) สร้าง Cartesian product แล้ว zip กับ sorted keys ตัวอย่าง import itertools as it Ad

  2. รวมสองลิสต์แบบ element-wise ใน Python

    รวมสมาชิกของสองลิสต์ตามลำดับ (index ตรงกัน) โดยการ concatenate สตริง ตัวอย่าง: [Outer-,Frost-,Sun-] + [Space,bite,rise] → [Outer-Space,Frost-bite,Sun-rise] ใช้ zip [i + j for i, j in zip(listA, listB)] ตัวอย่าง listA = [Outer-, Frost-, Sun-] listB = [Space, bite, rise] print(Given list A: , listA) p

  3. หาผลคูณสูงสุดของตัวเลขติดกันในสตริงใน Python

    กำหนดสตริงตัวเลข ต้องหาผลคูณสูงสุดของ 2 หลักที่ติดกัน ตัวอย่าง: 5238521 → max product = 5*8=40 (หลักที่ 3 และ 4) ใช้ zip และ max แปลงสตริงเป็นลิสต์ zip กับ list[1:] คูณหา max ตัวอย่าง Astring = 5238521 print(Given String : , Astring) Astring = list(Astring) print(String converted to list:\n, Astring

  4. จับคู่สมาชิกติดกันในลิสต์ใน Python

    ต้องการสร้างคู่ของสมาชิกที่ติดกันในลิสต์ (sliding window ขนาด 2) ใช้ index และ range [[listA[i], listA[i+1]] for i in range(len(listA)-1)] ตัวอย่าง listA = [51, 23, 11, 45] print(Given list A: , listA) res = [[listA[i], listA[i + 1]] for i in range(len(listA) - 1)] print(The list with paired element

  5. แปลงลิสต์เป็น tuple ใน Python

    ต้องการแปลงลิสต์ทั่วไปเป็น tuple ใช้ tuple() tuple(listA) โดยตรง ตัวอย่าง listA = [Mon, 2, Tue, 3] print(Given list A: , listA) res = tuple(listA) print(The tuple is : , res) ผลลัพธ์ Given list A: [Mon, 2, Tue, 3] The tuple is : (Mon, 2, Tue, 3) ใช้ * operator (*listA,) unpack ลิสต์เข้า tuple ตัวอย่

  6. แปลงลิสต์เป็น tuple ของลิสต์ใน Python

    ต้องการแปลงลิสต์เป็น tuple โดยที่แต่ละสมาชิกของ tuple เป็นลิสต์เดี่ยว ใช้ tuple และ list comprehension tuple([i] for i in listA) ตัวอย่าง listA = [Mon, 2, Tue, 3] print(Given list A: , listA) res = tuple([i] for i in listA) print(The tuple is : , res) ผลลัพธ์ Given list A: [Mon, 2, Tue, 3] The tuple

  7. แปลงลิสต์ของจำนวนเต็มหลายตัวเป็นจำนวนเต็มเดียวใน Python

    ได้ลิสต์ของจำนวนเต็ม ต้องการรวมเป็นจำนวนเต็มเดียว (เช่น [22,11,34] → 221134) ใช้ join แปลงแต่ละตัวเป็นสตริง join แล้ว int() ตัวอย่าง listA = [22, 11, 34] print(Given list A: , listA) res = int(.join([str(i) for i in listA])) print(The integer is : , res) ผลลัพธ์ Given list A: [22, 11, 34] The intege

  8. แปลง nested list เป็น flat list ใน Python

    Nested list คือลิสต์ที่สมาชิกเป็นลิสต์อีก ทำให้ต้อง flatten ให้เป็นลิสต์แบน ใช้ recursion ตัวอย่าง listA = [[43, [0]], 12, 19, [13, [8, 8]], 21] print(Given nested list: \n, listA) res = [] def flatlist(l): for x in l: if type(x) == list: flatlist(x) else:

  9. แปลงสตริงที่เป็นการแทนลิสต์ให้กลายเป็นลิสต์จริงใน Python

    บางครั้งลิสต์ปรากฏในรูปสตริง ต้องการแปลงกลับเป็นลิสต์จริง ใช้ strip และ split strip([]) ลบวงเล็บ แล้ว split(, ) แยกสมาชิก ตัวอย่าง stringA = [Mon, 2, Tue, 5,] print(Given string, stringA) print(type(stringA)) res = stringA.strip([]).split(, ) print(final list, res) print(type(res)) ผลลัพธ์ Given str

  10. แปลง byte string key:value ของพจนานุกรมเป็นสตริงใน Python

    Byte string ใน Python มี prefix b ต้องการแปลง dictionary ที่ key และ value เป็น byte string ให้เป็นสตริงทั่วไป ใช้ decode(ascii) decode(ascii) แปลง byte string เป็น ASCII string ตัวอย่าง bstring = {bday: bTue, btime: b2 pm, bsubject: bGraphs} print(bstring) stringA = {y.decode(ascii): bstring.get(y)

  11. แปลงตัวพิมพ์เล็ก/ใหญ่ของสมาชิกในลิสต์สตริงใน Python

    ต้องการให้ตัวอักษรในลิสต์สตริงเป็น case เดียวกัน (ทั้ง lower หรือทั้ง upper) ใช้ lower() map(lambda x: x.lower(), listA) แล้ว list() ตัวอย่าง listA = [MoN, TuE, FRI] print(Given list : \n, listA) res = list(map(lambda x: x.lower(), listA)) print(New all lowercase list: \n, res) ผลลัพธ์ Given list :

  12. แปลงพจนานุกรมเป็นสตริงใน Python

    ต้องการแปลง dictionary object เป็น string object ใช้ str() ใช้ str() โดยตรง ตรวจสอบ type ก่อนและหลัง ตัวอย่าง DictA = {Mon: 2 pm, Wed: 9 am, Fri: 11 am} print(Given dictionary : \n, DictA) print(Type : , type(DictA)) res = str(DictA) print(Result as string:\n, res) print(Type of Result: , type(res))

  13. แปลงลิสต์ของสตริงตัวเลขเป็นลิสต์ของจำนวนเต็มใน Python

    ได้ลิสต์ที่สมาชิกเป็นสตริงตัวเลข ต้องการแปลงเป็นจำนวนเต็มเพื่อคำนวณต่อ ใช้ int ใน list comprehension Nested loops แปลงแต่ละสตริงในแต่ละ sublist ตัวอย่าง listA = [[29,12], [25], [70]] print(Given list A: , listA) res = [[int(n) for n in element] for i in listA for element in i] print(The numeric list

  14. แปลงลิสต์ของสตริงตัวเลขเป็นลิสต์จำนวนเต็มเรียงลำดับใน Python

    ได้ลิสต์ที่สมาชิกเป็นสตริงตัวเลข (รวมลบได้) ต้องการแปลงเป็น int แล้วเรียงลำดับ ใช้ map และ sorted map(int, listA) แล้ว sorted() ตัวอย่าง listA = [54, 21, -10, 92, 5] print(Given list : \n, listA) listint = map(int, listA) res = sorted(listint) print(Sorted list of integers: \n, res) ผลลัพธ์ Given li

  15. แปลงลิสต์ของสตริงและตัวอักษรเป็นลิสต์ของตัวอักษรใน Python

    ได้ลิสต์ที่มีทั้งสตริงและตัวอักษรเดี่ยว ต้องการแปลงเป็นลิสต์ของตัวอักษรเดี่ยวทั้งหมด ใช้ List Comprehension Nested loops: วนแต่ละสมาชิก แล้ววนแต่ละตัวอักษรในสมาชิกนั้น ตัวอย่าง listA = [Mon,d,ay] print(Given list : \n, listA) res = [i for ele in listA for i in ele] print(List of characters: \n, res)

  16. แปลงลิสต์ของสตริงเป็นลิสต์ของ tuples ใน Python

    ได้ลิสต์ที่สมาชิกเป็นสตริงตัวเลข (เช่น 21, 3) ต้องการแปลงเป็นลิสต์ของ tuples ใช้ map และ eval ใช้ eval() แปลงสตริงเป็น tuple (ต้องระวังความปลอดภัย) ตัวอย่าง listA = [21, 3, 13, 4, 15, 7] print(Given list : \n, listA) res = list(map(eval, listA)) print(List of tuples: \n, res) ผลลัพธ์ Given list : [2

  17. แปลงลิสต์ของ tuples เป็นหลักตัวเลขที่ไม่ซ้ำใน Python

    กำหนดลิสต์ที่สมาชิกเป็น tuples ของตัวเลข ต้องการดึงหลักตัวเลขที่ไม่ซ้ำจากทุก tuple ใช้ re และ set ใช้ regex ลบอักขระไม่ต้องการ แล้วแปลงเป็น set เพื่อเอาค่าที่ไม่ซ้ำ ตัวอย่าง import re listA = [(21, 3), (13, 4), (15, 7), (8, 11)] print(Given list : \n, listA) temp = re.sub(r[\[\]\(\), ], , str(listA)

  18. แปลงลิสต์ของ tuples เป็นลิสต์แบนใน Python

    ได้ลิสต์ที่สมาชิกเป็น tuples ต้องการแปลงเป็นลิสต์แบน (flat list) ที่รวมสมาชิกทุกตัว ใช้ List Comprehension ใช้ nested for loops ใน list comprehension ตัวอย่าง listA = [(Mon, 3), (Wed, 4), (Fri, 7, pm)] print(Given list : \n, listA) res = [item for t in listA for item in t] print(Final list: \n, res)

  19. แปลงลิสต์ของ tuples เป็นลิสต์ของลิสต์ใน Python

    ได้ลิสต์ที่สมาชิกเป็น tuples ต้องการแปลงแต่ละ tuple เป็นลิสต์เพื่อประมวลผลต่อ ใช้ List Comprehension วนลูปแต่ละสมาชิก ใช้ list() แปลง tuple เป็นลิสต์ ตัวอย่าง listA = [(Mon, 3), (Wed, 4), (Fri, 7, pm)] print(Given list : \n, listA) res = [list(ele) for ele in listA] print(Final list: \n, res) ผลลัพธ

  20. แปลงลิสต์ของ tuples เป็นลิสต์ของสตริงใน Python

    ในการประมวลผลข้อมูล อาจได้ลิสต์ที่สมาชิกเป็น tuples ต้องการแปลงแต่ละ tuple เป็นสตริง ใช้ join() join() รวมสมาชิกของ sequence ด้วย separator ที่กำหนด ใช้ list comprehension ตัวอย่าง listA = [(M,o,n), (d,a,y), (7, pm)] print(Given list : \n, listA) res = [.join(i) for i in listA] print(Final list: \n,

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