ThepExcel Logo
  • บทความ
    • Excel
      • Excel ทั่วไป
      • Excel Pivot Table
      • Excel Power Pivot
      • Power Query
      • Excel Array Formula
      • Excel VBA
      • Excel for Business
      • Excel and Maths
      • ฟังก์ชัน Excel ทั้งหมด
    • Power BI
      • Power Query
      • Data Model
      • DAX Formula
      • Power BI Report
    • Coding
      • Excel VBA
      • Python
      • Power Query M Code
    • AI
      • ChatGPT
      • Stable Diffusion
      • MidJourney
    • Highlights : บทความแนะนำ
    • คลิปวีดีโอ
  • อบรม
    • อบรมลูกค้าองค์กร
    • คอร์สออนไลน์ SkillLane
    • แนะนำวิทยากร
    • Excel/Power BI Skill Map
    • Quiz
  • Shop
    • คอร์สออนไลน์
    • สินค้าทั้งหมด
    • หนังสือเล่ม
    • E-Book
    • Cart
  • Download
    • Download ไฟล์จากเทพเอ็กเซล
    • ThepExcel-Mfx : M Code สำเร็จรูป
    • Date Table สำเร็จรูป
    • กราฟ My Skill
    • github.com/ThepExcel
  • รวม Link
    • รวม Link สอน Excel & Power BI ทั้งไทยและเทศ
    • รวม Link เกี่ยวกับ AI
    • รวม Link Coding
    • หนังสือแนะนำ
    • Facebook ThepExcel
    • YouTube ThepExcel
    • DAX Formatter
  • Contact
    • แนะนำ เทพเอ็กเซล (Thep Excel)
    • แนะนำวิทยากร : อาจารย์ ศิระ เอกบุตร (ระ)
    • นโยบายการคุ้มครองข้อมูลส่วนบุคคล (Privacy Policy)
  • Facebook
  • YouTube

หัด Python สำหรับคนเป็น Excel : ตอนที่ 4 – การทำงานกับ String และ List

Python String และ List

Categories 📂

Python

Tags 🏷️

comprehension, List, method

ข้อมูลประเภท String และ List เป็นสิ่งที่พบเจอและต้องจัดการบ่อยที่สุด ดังนั้นเราจะมาลงรายละเอียดวิธีการจัดการ และคำสั่งพื้นฐานที่เอาไว้ทำงานกับ String และ List กันครับ

สารบัญ

  • คำสั่งพื้นฐานที่ใช้กับ String
    • คำสั่ง format
  • คำสั่งพื้นฐานที่ใช้กับ List
  • ลองเอามาประยุกต์กับการ Loop
    • List Comprehension
  • ตอนต่อไป
  • สารบัญ Series Python

คำสั่งพื้นฐานที่ใช้กับ String

  • นับข้อมูล
    • len(myString) เพื่อนับว่ามีกี่อักขระ (นี่เหมือน LEN ใน Excel )
  • จัดการพิมพ์เล็กพิมพ์ใหญ่
    • myString.lower() ทำเป็นตัวพิมพ์เล็ก (นี่ก็เหมือน LOWER ใน Excel)
    • myString.upper() ทำเป็นตัวพิมพ์ใหญ่ (นี่ก็เหมือน UPPER ใน Excel)
  • ค้นหาข้อมูล
    • “ra” in myString หาว่ามีคำว่า ra หรือไม่ โดยให้ผลเป็น True/False (อันนี้ Excel ไม่มี)
    • myString.find(“ra”) หาว่ามีคำว่า ra อยู่ index ไหน (เริ่มที่ 0) ถ้าหาไม่เจอจะได้ค่า -1 (อันนี้เหมือน FIND ใน excel)
    • myString.find(“ra”,3,9) หาว่ามีคำว่า ra อยู่ index ไหน โดยเริ่มหาตั้งแต่ index 3 แต่ไม่ถึง 9 ถ้าหาไม่เจอจะได้ค่า -1
  • กำจัดส่วนเกิน
    • myString.strip() ตัดช่องว่างด้านหน้าและหลังออกทั้งหมด (คล้าย TRIM ใน Excel แต่ strip ตัดแค่หน้าหลัง แต่ TRIM ทำให้ตรงกลางเหลือเคาะเดียวด้วย)
    • myString.strip(“. /,”) ตัด character ที่ระบุ (dot space slash comma )ด้านหน้าและหลังออกทั้งหมด
  • แยกส่วนคำ
    • myString.split() แยกคำออกเป็น list ด้วยตัวแบ่งคือ space โดยแยกไม่จำกัดครั้ง
    • myString.split(“/”, 1) แยกคำออกเป็น list ด้วยตัวแบ่งคือ / โดยแยกแค่ 1 ครั้ง (กลายเป็น 2 item)
    • ถ้าเอาบางส่วนของข้อความ ให้ใช้การ slice index เอา เช่น myString[0:3] คือเอา 3 ตัวแรก (คล้าย LEFT)

คำสั่ง format

ปกติแล้ว หากเราจะเชื่อมตัวเลขกับข้อความด้วย + มันจะโวยวายทันที เพราะเป็นข้อมูลคนละประเภทกัน…
แต่ถ้าเราใช้ format มาช่วย ทุกอย่างจะง่ายขึ้นมาก เช่น

name = "Sira"
age = 37
weight = 68
height = 170
txt = "ผมชื่อ {} ตอนนี้อายุ {} ปี .. หนัก {} kg สูง {} cm"
print(txt.format(name,age,weight,height))

คำสั่งพื้นฐานที่ใช้กับ List

อันนี้อาจจะไม่ค่อยเหมือนใน Excel เท่าไหร่เนอะ มาดูกันว่ามีอะไรบ้าง

  • นับข้อมูล
    • len(myList) คือ นับจำนวน item ใน list
    • myList.count(“Eren”) รับคำว่า Eren ใน list ว่ามีกี่ตัว
  • การเพิ่มข้อมูล
    • myList.append(“Sira”) คือ เพิ่มคำว่า Sira เข้าไปต่อท้าย list
    • myList.insert(1, “Sira”) คือ เพิ่มคำว่า Sira เข้าไปที่ index ที่ 1 (กลายเป็นตัวที่ 2)
    • myList.extend(anotherList) เพิ่ม item ใน List อีกอันคือ anotherList เข้าไปต่อท้าย myList
      • นอกจาก extend แล้ว จะใช้เครื่องหมาย + ไปเลยก็ยังได้ เช่น myList + anotherList ซึ่งเขียนง่ายกว่า
  • ลบข้อมูล
    • myList.remove(“Sira”) คือ ลบคำว่า Sira ออกจาก List (เอาออกทีละตัว จากตัวแรกไปตัวหลัง)
    • myList.pop(1) คือ เอา item ที่อยู่ index 1 ออกไป (ถ้าไม่ระบุ index จะเอาตัวสุดท้ายออกไป)
  • เรียงข้อมูล
    • myList.sort() คือ เรียงข้อมูลใน list จากน้อยไปมาก (สนใจพิมพ์เล็กพิมพ์ใหญ่)
    • myList.sort(key = str.lower) เรียงจากน้อยไปมาก (ไม่สนใจพิมพ์เล็กพิมพ์ใหญ่ เพราะทำให้เป็นตัวพิมพ์เล็กแล้ว)
    • myList.sort(reverse = True) คือ เรียงข้อมูลใน list จากมากไปน้อย (สนใจพิมพ์เล็กพิมพ์ใหญ่)
    • mylist.reverse() = เรียงกลับด้านจาก list ดั้งเดิม
  • รวมข้อมูลใน List ด้วยตัวคั่น
    • ตัวคั่น.join(list) คือ รวมข้อมูลใน List ให้กลายเป็น string คำเดียว โดยคั่นด้วยตัวคั่นที่ระบุ คล้ายๆ กับ TEXTJOIN ใน Excel เลย

ลองเอามาประยุกต์กับการ Loop

พอเราเริ่มรู้คำสั่งต่างๆ ในการทำงานกับ String และList พอผนวกกับเรื่องวน Loop และ if เราก็จะทำเรื่องต่างๆ ได้มากขึ้น เช่น

ถ้าใน list มีคำที่ไม่มีตัว i ให้ลบทิ้งด้วย remove ซะ โดยใช้ for loop และ if ช่วยเช็คเงื่อนไขที่ต้องการก็ได้

myList = ["Eren", "Mikasa", "Armin", "Levi"]
for x in myList:
  if "i" not in x: #ถ้าคำนั้นไม่มีตัว i
    myList.remove(x)  # ให้ลบคำนั้นซะ
print (myList)

ใช้ for loop ทำให้ข้อมูลใน List เป็นตัวพิมพ์ใหญ่ทั้งหมด ด้วย upper() โดยเอาผลลัพธ์ไปสร้างเป็น List ใหม่อีกอันนึงด้วย append

myList = ["Eren", "Mikasa", "Armin", "Levi"]
NewList=[]  #สร้าง List ว่างเปล่า
for x in myList:
  NewList.append(x.upper()) #ทำให้ item เป็นตัวพิมพ์ใหญ่ แล้วใส่ต่อท้ายเข้าไปใน list ใหม่
print (NewList)

หรือจะใช้ผสมกับเงื่อนไข if ก็ได้ เช่น จะเอาไปเป็น item ใน List ใหม่เฉพาะ item ที่มีตัว i อยู่เท่านั้น

myList = ["Eren", "Mikasa", "Armin", "Levi"]
NewList=[]  
for x in myList:
  if "i" in x:  #ใส่เงื่อนไขว่าเอาเฉพาะที่มีตัว i
    NewList.append(x.upper()) 
print (NewList)

เรื่องของเรื่องคือ หากเราต้องการสร้าง List ใหม่จาก List เดิม นอกจากจะเขียนด้วยวิธีใช้ for loop แล้ว ยังมีอีกวิธีที่ทำได้ และเขียน Code ได้สั้นและกระชับกว่า นั่นก็คือ วิธีที่เรียกว่า List Comprehension

List Comprehension

List Comprehension มีวิธีการเขียนดังนี้

[expression for item in list if condition]   #ส่วน if condition ไม่ใส่ก็ได้

เช่น ในตัวอย่างการแปลงเป็นตัวพิมพ์ใหญ่ เราสามารถเขียนสูตรสั้นๆ แบบนี้ได้เลย

myList = ["Eren", "Mikasa", "Armin", "Levi"]
NewList=[x.upper() for x in myList]  #List Comprehension
print (NewList)
python list comprehension

หรือ ถ้าจะเอาเฉพาะที่มีตัว i เราก็ไปใส่เงื่อนไขใน if condition ได้เลย

myList = ["Eren", "Mikasa", "Armin", "Levi"]
NewList=[x.upper() for x in myList if "i" in x]  #List Comprehension แบบมีเงื่อนไข
print (NewList)

แต่ถ้ากรณีมีเงื่อนไข else ด้วย จะเขียนอีกลักษณะนึง คือ เอา if else ขึ้นมาก่อน ดังนี้

[expression_if_true if condition else expression_if_false for item in list ]   

เช่น ถ้ามี i ให้ทำ upper ถ้าไม่มี i ให้ทำ lower

myList = ["Eren", "Mikasa", "Armin", "Levi"]
#List Comprehension แบบมี if else
NewList=[ x.upper() if "i" in x else x.lower() for x in myList ]  
print (NewList)

ลองดูตัวอย่างการผสมผสานหลายๆ เรื่อง เช่น Split คำจากประโยคยาวๆ แล้วเอามาเฉพาะคำที่มีตัว e แบบ trim . ดูบ้าง

วิธีการอ้างอิงข้อความยาวๆ แบบชัวร์ๆ คือใส่ใน Quote 3 ชั้น ดังนี้ (เอาประโยคมาจาก Wiki ของคำว่า Excel)

myParagraph = '''Microsoft Excel is a spreadsheet developed by Microsoft for Windows, macOS, Android and iOS. 
It features calculation, graphing tools, pivot tables, and a macro programming language called Visual Basic for Applications. 
It has been a very widely applied spreadsheet for these platforms, especially since version 5 in 1993, 
and it has replaced Lotus 1-2-3 as the industry standard for spreadsheets. 
Excel forms part of the Microsoft Office suite of software.'''

myList=myParagraph.split() #แยกคำด้วยช่องว่าง
print(myList)
print(len(myList))
selectedWord=[x.strip(".") for x in myList if "e" in x.lower()] #เอาที่มีตัว e และตัด . หัวท้ายออก
print(selectedWord)
print(len(selectedWord))

หวังว่าๆ เพื่อนๆ คงจะพอเห็นภาพการทำงานกับ String และ List มากขึ้นนะครับ

ตอนต่อไป

เนื้อหาตอนต่อไป ได้เวลาเรียนรู้เรื่องเกี่ยวกับการสร้างฟังก์ชันขึ้นมาใช้เอง รับรองว่าเป็นความสามารถที่เจ๋งมากๆ เลยล่ะ!

สารบัญ Series Python

  • หัดเขียนโปรแกรม Python สำหรับคนเป็น Excel มาก่อน : ตอนที่ 1
  • หัด Python สำหรับคนเป็น Excel : ตอนที่ 2 – ประเภทข้อมูล (Data Types)
  • หัด Python สำหรับคนเป็น Excel : ตอนที่ 3 – การวน Loop และ เงื่อนไข if
  • หัด Python สำหรับคนเป็น Excel : ตอนที่ 4 – การทำงานกับ String และ List
  • หัด Python สำหรับคนเป็น Excel : ตอนที่ 5 – การสร้างฟังก์ชันขึ้นมาใช้เอง (Function)
  • หัด Python สำหรับคนเป็น Excel : ตอนที่ 6 – การเรียกใช้ Module / Packages เจ๋งๆ ที่มีคนสร้างไว้แล้ว
  • หัด Python สำหรับคนเป็น Excel : ตอนที่ 7 – Web Scraping ด้วย Beautiful Soup
  • แนวทางการใช้ Python ใน Power BI
  • หัด Python สำหรับคนเป็น Excel : ตอนที่ 8 – การสร้างกราฟด้วย Matplotlib
  • รวม Link สอน Python / Programming / AI/ Machine Learning แบบฟรีๆ

แชร์ความรู้ให้เพื่อนๆ ของคุณ
839    
839    

ติดตามเทพเอ็กเซล

  • Facebook
  • YouTube

อบรมกับเทพเอ็กเซล

🔥 คอร์สใหม่ล่าสุด 🔥

การทำ Optimization ด้วย Excel Solver
สำหรับงานวางแผน
คอร์สออนไลน์ เทพเอ็กเซล
คอร์สออนไลน์ จากเทพเอ็กเซล ดูกี่รอบก็ได้
อบรม Excel / Power BI ให้องค์กรของคุณ

บทความล่าสุด

  • แนวทางฝึกฝน Excel ให้เก่งขึ้น
  • รวม Link เว็บ/เพจเกี่ยวกับ AI
  • วิธีกำหนดท่าทางแบบให้ได้ดั่งใจด้วย ControlNet ใน Stable Diffusion [Part4]
  • วิธีสั่ง Prompt และตั้งค่าใน Stable Diffusion ให้รูปสวยโดนใจ [Part3]
  • วิธีเรียกใช้งาน Model เจ๋งๆ ใน Stable Diffusion [ตอนที่2]
  • วิธีใช้งาน AI สร้างรูปสุดเจ๋งและฟรีด้วย Stable Diffusion ฉบับมือใหม่ [ตอนที่1]
  • 10 ไอเดีย เรียนรู้ Excel ผ่าน ChatGPT AI สุดเจ๋ง

บทความแนะนำ

🔥ฟังก์ชันทั้งหมดใน Excel 🔥

  • แกะเคล็ดวิชา Excel Wizard ในการแข่ง Speed Run Excel ระดับโลก
  • เจาะลึก CALCULATE ใน DAX แบบลึกสุดใจ : Part 1
  • Series สอนดึงข้อมูลจากเว็บ ด้วย Power Automate Desktop
  • สรุปการใช้ LAMBDA ฟังก์ชันที่ใช้สร้างฟังก์ชันใน Excel 365 และผองเพื่อน
  • วิธีใช้ Excel คำนวณระยะเวลาการทำงานรวม แถมระบุเวลาพักได้แบบยืดหยุ่น
  • วิธีจัดการข้อมูลแย่ๆ ด้วย Power Query ทั้งข้อมูลปนกัน ข้อมูลอยู่บนหัวตาราง
  • แยกข้อมูลที่อยู่สุดเน่า ด้วย Excel Power Query

Categories

Tags

ai collection concepts copy database Data Model data validation date dax dropdown error excel filter finance find format formula function game graph IF index intro inventory len link logic lookup match m code merge mid overview paste pivot power query right row solver sort speed split substitute table text time tips trim vba vlookup

Archives

  • April 2023 (3)
  • March 2023 (2)
  • February 2023 (2)
  • January 2023 (1)
  • October 2022 (1)
  • September 2022 (3)
  • August 2022 (3)
  • July 2022 (1)
  • June 2022 (3)
  • May 2022 (1)
  • April 2022 (2)
  • February 2022 (1)
  • December 2021 (2)
  • November 2021 (10)
  • September 2021 (2)
  • August 2021 (6)
  • July 2021 (2)
  • June 2021 (2)
  • May 2021 (10)
  • April 2021 (3)
  • March 2021 (3)
  • February 2021 (4)
  • January 2021 (8)
  • December 2020 (5)
  • November 2020 (13)
  • October 2020 (5)
  • September 2020 (11)
  • August 2020 (4)
  • July 2020 (13)
  • June 2020 (17)
  • May 2020 (16)
  • April 2020 (16)
  • March 2020 (10)
  • February 2020 (15)
  • January 2020 (16)
  • December 2019 (4)
  • November 2019 (3)
  • October 2019 (9)
  • September 2019 (1)
  • August 2019 (7)
  • June 2019 (3)
  • May 2019 (9)
  • April 2019 (9)
  • March 2019 (2)
  • February 2018 (1)
  • January 2018 (3)
  • November 2017 (3)
  • August 2017 (1)
  • July 2017 (1)
  • June 2017 (1)
  • May 2017 (6)
  • April 2017 (6)
  • March 2017 (7)
  • February 2017 (1)
  • January 2017 (2)
  • December 2016 (1)
  • October 2016 (2)
  • September 2016 (3)
  • August 2016 (2)
  • July 2016 (2)
  • June 2016 (1)
  • May 2016 (1)
  • April 2016 (1)
  • March 2016 (2)
  • February 2016 (1)
  • January 2016 (2)
  • December 2015 (2)
  • November 2015 (5)
  • October 2015 (3)
  • June 2015 (2)
  • May 2015 (1)
  • April 2015 (26)
  • January 2015 (1)
  • December 2014 (1)
  • November 2014 (2)
  • October 2014 (1)
  • September 2014 (2)
  • August 2014 (1)
  • June 2014 (1)
  • May 2014 (1)
  • April 2014 (3)
  • March 2014 (3)
  • February 2014 (12)
  • January 2014 (7)
  • December 2013 (2)
  • November 2013 (8)
  • October 2013 (2)

เทพเอ็กเซล : Thep Excel

copyright © 2022

  • Facebook
  • YouTube
เว็บไซต์นี้ใช้คุกกี้ (Cookies)
บริษัท เทพเอ็กเซล จำกัด ให้ความสำคัญต่อข้อมูลส่วนบุคคลของท่าน เพื่อการพัฒนาและปรับปรุงเว็บไซต์รวมถึงสินค้าและบริการต่างๆ หากท่านใช้บริการเว็บไซต์นี้ โดยไม่มีการปรับตั้งค่าใดๆ แสดงว่าท่านยินยอมที่จะรับคุกกี้บนเว็บไซต์ และนโยบายสิทธิส่วนบุคคลของเรา
ตั้งค่าคุกกี้ยอมรับทั้งหมดอ่านเพิ่มเติม
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT