Sequence Slicing
Array.slice() ใน TypeScript, [start:stop:step] ใน Python
หัวข้อที่มีชื่อว่า “Array.slice() ใน TypeScript, [start:stop:step] ใน Python”TypeScript ใช้ Array.prototype.slice(start, end) เพื่อดึง subarray Python มี syntax slicing ที่กว้างกว่ามากซึ่งสร้างอยู่ใน syntax ของภาษาโดยตรง ไม่ใช่ method call: sequence[start:stop:step] สิ่งนี้ทำงานบน list, string, tuple และ sequence ใดก็ตาม ทำให้โค้ดที่เกี่ยวข้องกับ sequence กระชับและอ่านง่ายกว่ามาก
Slice พื้นฐาน
หัวข้อที่มีชื่อว่า “Slice พื้นฐาน”// TypeScript: Array.slice(start, end) — end is exclusiveconst data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(data.slice(2, 5)); // [2, 3, 4]console.log(data.slice(0, 3)); // [0, 1, 2]console.log(data.slice(7)); // [7, 8, 9]
// No built-in step — need filter or manual loopconst evens = data.filter((_, i) => i % 2 === 0);console.log(evens); // [0, 2, 4, 6, 8]
// Reverseconsole.log([...data].reverse()); // [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]# Python: [start:stop:step] — stop is exclusive, step is optionaldata = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(data[2:5]) # [2, 3, 4]print(data[:3]) # [0, 1, 2] — omit start → from beginningprint(data[7:]) # [7, 8, 9] — omit stop → to endprint(data[::2]) # [0, 2, 4, 6, 8] — every other elementprint(data[::-1]) # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] — reverseสามส่วนของ slice ล้วนเป็น optional: [:] คัดลอก sequence ทั้งหมด, [::2] ดึงทุก element ที่สอง, [::-1] กลับลำดับ
Negative index
หัวข้อที่มีชื่อว่า “Negative index”Python อนุญาตให้ใช้ index ติดลบเพื่อนับจากท้าย -1 คือ element สุดท้าย, -2 คือ element รองสุดท้าย และต่อไป สิ่งนี้ทำงานทั้งใน index เดี่ยวและใน slice
// TypeScript: negative index requires .at() (ES2022) or length mathconst data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(data.at(-1)); // 9 — last elementconsole.log(data.at(-3)); // 7
// Last 3 elements via sliceconsole.log(data.slice(-3)); // [7, 8, 9]
// All but first and lastconsole.log(data.slice(1, -1)); // [1, 2, 3, 4, 5, 6, 7, 8]# Python: negative index works natively everywheredata = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(data[-1]) # 9 — last elementprint(data[-3:]) # [7, 8, 9] — last 3 elementsprint(data[1:-1]) # [1, 2, 3, 4, 5, 6, 7, 8] — all but first and lastSlicing บน string
หัวข้อที่มีชื่อว่า “Slicing บน string”Python slice ทำงานบน string เหมือนกับ list คืนค่าเป็น string ใหม่ TypeScript ใช้ .slice() หรือ .substring() บน string
// TypeScript: string slicing with .slice()const text = "Hello, World!";
console.log(text.slice(7, 12)); // "World"console.log(text.split("").reverse().join("")); // "!dlroW ,olleH"# Python: slicing works identically on stringstext = "Hello, World!"
print(text[7:12]) # "World"print(text[::-1]) # "!dlroW ,olleH" — reverse a stringลองด้วยตัวเอง
หัวข้อที่มีชื่อว่า “ลองด้วยตัวเอง”data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(data[2:5])print(data[:3])print(data[7:])print(data[::2])print(data[::-1])
print(data[-1])print(data[-3:])print(data[1:-1])
text = "Hello, World!"print(text[7:12])print(text[::-1])Loading Python runtime (first run only)…