ผลต่างระหว่างรุ่นของ "Python Programming/Lists"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
 
แถว 47: แถว 47:
  
 
ฟังก์ชัน <tt>map(f, sequence)</tt> เรียกฟังก์ชัน <tt>f(x)</tt> ที่เรากำหนดโดยมี parameter เป็นสมาชิกของ list <tt>sequence</tt> แล้วสร้าง list ใหม่ซึ่งมีสมาชิกเป็นผลลัพธ์ฟังก์้ชันนั้น
 
ฟังก์ชัน <tt>map(f, sequence)</tt> เรียกฟังก์ชัน <tt>f(x)</tt> ที่เรากำหนดโดยมี parameter เป็นสมาชิกของ list <tt>sequence</tt> แล้วสร้าง list ใหม่ซึ่งมีสมาชิกเป็นผลลัพธ์ฟังก์้ชันนั้น
 +
<syntaxhighlight lang="python">
 +
def plusone(x):
 +
    return x+1
 +
</syntaxhighlight>
 
<pre title="interpreter">
 
<pre title="interpreter">
>>> def plusone(x):
 
...    return x+1
 
...
 
 
>>> map(plusone, [1,2,3,4,5])
 
>>> map(plusone, [1,2,3,4,5])
 
[2, 3, 4, 5, 6]
 
[2, 3, 4, 5, 6]
>>>
+
</pre>
>>> def cube(x):
+
<syntaxhighlight lang="python">
...     return x**3
+
def cube(x):
...
+
     return x**3
 +
</syntaxhighlight>
 +
<pre title="interpreter">
 
>>> map(cube, [-2,-1,0,1,2,100])
 
>>> map(cube, [-2,-1,0,1,2,100])
 
[-8, -1, 0, 1, 8, 1000000]
 
[-8, -1, 0, 1, 8, 1000000]
แถว 62: แถว 65:
  
 
ฟังก์ชัน <tt>filter(function, sequence)</tt> เรียกฟังก์ชัน <tt>f(x)</tt> ซึ่งคืนค่าเป็นบูลีน โดยมี parameter เป็นสมาชิกของ list <tt>sequence</tt> แล้วสร้าง list ของสมาชิกทุกตัวที่ฟังก์ชันคืนค่าเป็น <tt>True</tt>
 
ฟังก์ชัน <tt>filter(function, sequence)</tt> เรียกฟังก์ชัน <tt>f(x)</tt> ซึ่งคืนค่าเป็นบูลีน โดยมี parameter เป็นสมาชิกของ list <tt>sequence</tt> แล้วสร้าง list ของสมาชิกทุกตัวที่ฟังก์ชันคืนค่าเป็น <tt>True</tt>
 +
<syntaxhighlight lang="python">
 +
def divisible_by_3(x):
 +
    if x % 3 == 0:
 +
        return True
 +
    else:
 +
        return False
 +
</syntaxhighlight>
 +
 
<pre title="interpreter">
 
<pre title="interpreter">
>>> def divisible_by_3(x):
 
...    if x % 3 == 0:
 
...        return True
 
...    else:
 
...        return False
 
...
 
 
>>> filter(divisible_by_3, [1,2,3,4,5,6,7,8,9,10,11,12])
 
>>> filter(divisible_by_3, [1,2,3,4,5,6,7,8,9,10,11,12])
 
[3, 6, 9, 12]
 
[3, 6, 9, 12]
>>>
+
</pre>
>>> def is_prime(x):
+
<syntaxhighlight lang="python">
...     def check_divisible(x, y):
+
def is_prime(x):
...         if y < 2:
+
     def check_divisible(x, y):
...             return False
+
         if y < 2:
...         else:
+
             return False
...             if x % y == 0:
+
         else:
...                 return True
+
             if x % y == 0:
...             else:
+
                 return True
...                 return check_divisible(x, y-1)
+
             else:
...     if x < 2:
+
                 return check_divisible(x, y-1)
...       return False
+
     if x < 2:
...     else:
+
       return False
...       return not check_divisible(x, x-1)
+
     else:
...
+
       return not check_divisible(x, x-1)
 +
</syntaxhighlight>
 +
<pre title="interpreter">
 
>>> filter(is_prime, [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20])
 
>>> filter(is_prime, [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20])
 
[2, 3, 5, 7, 11, 13, 17, 19]
 
[2, 3, 5, 7, 11, 13, 17, 19]
แถว 91: แถว 98:
  
 
ฟังก์ชัน <tt>reduce(f, sequence)</tt> จะเอาสมาชิกสองตัวแรกของ list <tt>sequence</tt> ไปเป็น parameter ของฟังก์ชัน <tt>f</tt> หลังจากนั้นจะเอาผลลัพธ์ที่ได้กับสมาชิกตัวที่สามไปใส่ฟังก์ชัน <tt>f</tt> อีกครั้ง แล้วเอาผลลัพธ์ที่ได้กับสมาชิกตัวที่สี่ไปใส่ฟังก์ชัน <tt>f</tt> อีกครั้ง ไปเรื่อยๆ จนหมด list เสร็จแล้วฟังก์ชัน <tt>reduce</tt> จะคืนค่าที่เป็นผลลัพธ์ของฟังก์ชัน <tt>f</tt> ครั้งสุดท้ายออกมา
 
ฟังก์ชัน <tt>reduce(f, sequence)</tt> จะเอาสมาชิกสองตัวแรกของ list <tt>sequence</tt> ไปเป็น parameter ของฟังก์ชัน <tt>f</tt> หลังจากนั้นจะเอาผลลัพธ์ที่ได้กับสมาชิกตัวที่สามไปใส่ฟังก์ชัน <tt>f</tt> อีกครั้ง แล้วเอาผลลัพธ์ที่ได้กับสมาชิกตัวที่สี่ไปใส่ฟังก์ชัน <tt>f</tt> อีกครั้ง ไปเรื่อยๆ จนหมด list เสร็จแล้วฟังก์ชัน <tt>reduce</tt> จะคืนค่าที่เป็นผลลัพธ์ของฟังก์ชัน <tt>f</tt> ครั้งสุดท้ายออกมา
 +
<syntaxhighlight lang="python">
 +
def add(x,y):
 +
    return x+y
 +
</syntaxhighlight>
 +
 
<pre title="interpreter">
 
<pre title="interpreter">
>>> def add(x,y):
 
...    return x+y
 
...
 
 
>>> reduce(add, [1,5,9,10])
 
>>> reduce(add, [1,5,9,10])
 
25
 
25
>>>
 
 
>>> reduce(max, [6,0,-5,9])
 
>>> reduce(max, [6,0,-5,9])
 
9
 
9

รุ่นแก้ไขปัจจุบันเมื่อ 04:09, 4 ตุลาคม 2558

ลิิสต์ (list) เป็นข้อมูลซึ่งแทนลำดับของค่าต่างๆ เหมือน tuple แต่ว่าเราสามารถเปลี่ยนสมาชิกที่ตำแหน่งต่างๆ ของ list ได้ ซึ่งทำให้ลิสต์คล้ายอะเรย์ในภาษา C มากกว่า tuple เราสามารถสร้าง list ได้ด้วยการเขียนลำดับของสมาชิกใน list ภายในวงเล็บก้ามปู

>>> a = [True, "saber", 3.1415927, "archer", "lancer"]
>>> a
[True, 'saber', 3.1415926999999999, 'archer', 'lancer']
>>> b = ['berserker']
>>> b
['berserker']
>>> c = []
>>> c
[]

สังเกตว่าเราสามารถสร้าง list ที่มีสมาชิกตัวเดียว (b) และ list ว่าง (c) ได้โดยไม่ต้องอาศัยไวยากรณ์แบบพิเศษเช่นเดียวกับ tuple

เราสามารถเปลี่ยนสมาชิก ณ ตำแหน่งต่างของ list ได้

>>> a
[False, 'saber', 3.1415926999999999, 'archer', 'lancer']
>>> a[2] = 22/7
>>> a
[False, 'saber', 3, 'archer', 'lancer']

นอกจากนี้เราสามารถเช็คว่าค่าค่าหนึ่งอยู่ใน list, ทำ slicing, ใช้เครื่องหมายบวก, และคูณ list ด้วยจำนวนเต็มได้เหมือนกับ tuple

>>> "saber" in a
True
>>> 3.1415927 in a
False
>>> a[2:5]
[3, 'archer', 'lancer']
>>> a[:-1]
[False, 'saber', 3, 'archer']
>>> a + b
[False, 'saber', 3, 'archer', 'lancer', 'berserker']
>>> b + a
['berserker', False, 'saber', 3, 'archer', 'lancer']
>>> 4*b
['berserker', 'berserker', 'berserker', 'berserker']
>>> 5*c
[]

การประมวลผล list

ไพทอนมีฟังก์ชันสำหรับทำการคำนวณเกี่ยวกับ list ที่สำคัญอยู่สามฟังก์ชัน ได้แก่ map, filter, และ reduce

ฟังก์ชัน map(f, sequence) เรียกฟังก์ชัน f(x) ที่เรากำหนดโดยมี parameter เป็นสมาชิกของ list sequence แล้วสร้าง list ใหม่ซึ่งมีสมาชิกเป็นผลลัพธ์ฟังก์้ชันนั้น

def plusone(x):
    return x+1
>>> map(plusone, [1,2,3,4,5])
[2, 3, 4, 5, 6]
def cube(x):
    return x**3
>>> map(cube, [-2,-1,0,1,2,100])
[-8, -1, 0, 1, 8, 1000000]

ฟังก์ชัน filter(function, sequence) เรียกฟังก์ชัน f(x) ซึ่งคืนค่าเป็นบูลีน โดยมี parameter เป็นสมาชิกของ list sequence แล้วสร้าง list ของสมาชิกทุกตัวที่ฟังก์ชันคืนค่าเป็น True

def divisible_by_3(x):
    if x % 3 == 0:
        return True
    else:
        return False
>>> filter(divisible_by_3, [1,2,3,4,5,6,7,8,9,10,11,12])
[3, 6, 9, 12]
def is_prime(x):
    def check_divisible(x, y):
        if y < 2:
            return False
        else:
            if x % y == 0:
                return True
            else:
                return check_divisible(x, y-1)
    if x < 2:
      return False
    else:
      return not check_divisible(x, x-1)
>>> filter(is_prime, [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20])
[2, 3, 5, 7, 11, 13, 17, 19]

ฟังก์ชัน reduce(f, sequence) จะเอาสมาชิกสองตัวแรกของ list sequence ไปเป็น parameter ของฟังก์ชัน f หลังจากนั้นจะเอาผลลัพธ์ที่ได้กับสมาชิกตัวที่สามไปใส่ฟังก์ชัน f อีกครั้ง แล้วเอาผลลัพธ์ที่ได้กับสมาชิกตัวที่สี่ไปใส่ฟังก์ชัน f อีกครั้ง ไปเรื่อยๆ จนหมด list เสร็จแล้วฟังก์ชัน reduce จะคืนค่าที่เป็นผลลัพธ์ของฟังก์ชัน f ครั้งสุดท้ายออกมา

def add(x,y):
    return x+y
>>> reduce(add, [1,5,9,10])
25
>>> reduce(max, [6,0,-5,9])
9

ฟังก์ชัน range

ฟังก์ชัน range(a,b,x) เป็นฟังก์ขันที่ใช้สร้าง list ที่เก็บตัวเลขที่เรียงกันเป็นลำดับเลขคณิต โดยมันจะสร้าง list ที่มีค่า a, a+x, a+2x, a+3x, ..., a+kx โดยที่ a+kx < b ถ้า x เป็นบวก หรือ a+kx > b ถ้า x เป็นลบ

>>> range(1,11,3)
[1, 4, 7, 10]
>>> range(11, 1, -3)
[11, 8, 5, 2]
>>> range(0, 100, 10)
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]

ถ้าเราละเละ x ฟังก์ชัน range จะให้ x มีค่าเป็น 1 โดยอัตโนมัติ

>>> range(17, 21)
[17, 18, 19, 20]
>>> range(0, 10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

ถ้าเราเรียกฟังก์ชัน range โดยมี parameter เพียงแค่ตัวเดียว เราจะได้ list ของจำนวนเต็มที่ไม่เป็นลบทั้งหมดที่น้อยกว่าค่านั้น เรียงจากน้อยไปมาก

>>> range(20)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> range(5)
[0, 1, 2, 3, 4]
หน้าก่อน: Tuples สารบัญ หน้าต่อไป: Loops