Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Python TypeError: can only concatenate list (not 'int') to list - วิธีแก้

Lists สามารถ concatenate (ต่อ) กับ lists อื่นได้ หมายถึงเพิ่มเนื้อหาของลิสต์หนึ่งเข้าไปอีกลิสต์หนึ่ง ค่าที่มี data types อื่น เช่น integers ไม่สามารถ concatenate เข้า list ได้

ถ้าพยายาม concatenate integer เข้า list Python interpreter จะคืน error TypeError: can only concatenate list (not "int") to list

คู่มือนี้อธิบาย error นี้หมายถึงอะไร ทำงานยังไง พร้อมตัวอย่าง ช่วย navigate การแก้ไขในโค้ดของคุณ

TypeError: can only concatenate list (not "int") to list

Concatenation ทำให้เพิ่ม 2 lists เข้าด้วยกันง่าย แม้จะใช้ extend() method เพิ่ม list เข้า list อีกได้ แต่ concatenation ใช้แค่สัญลักษณ์เดียว: plus sign (+)

Lists ไม่ใช่ object เดียวที่ concatenate ได้ Iterable objects อื่น เช่น dictionaries, tuples ก็ concatenate ได้

2 objects ที่ data types ต่างกัน concatenate ไม่ได้ หมายถึงไม่สามารถ concatenate list กับ dictionary, หรือ integer กับ list ได้

คุณจะเจอ TypeError: can only concatenate list (not "int") to list ถ้าใช้ concatenation เพิ่ม single integer item เข้า list

ตัวอย่างสถานการณ์ (An Example Scenario)

สร้างโปรแกรมติดตามออเดอร์แซนด์วิชที่ร้านคาเฟ่วันศุกร์ ต้องการกรองแซนด์วิชที่สั่งมากกว่า 75 ครั้ง ดูว่าแซนด์วิชไหนนิยม

เริ่มกำหนด 2 ลิสต์: ลิสต์แซนด์วิช และลิสต์จำนวนออเดอร์แต่ละชนิด:

sandwiches = ["Egg and Cress", "Chicken Club", "Hummus and Tomato", "Cheese"]
orders = [54, 77, 22, 98]

ลิสต์กำหนดให้ตัวแปร "sandwiches" และ "orders" ต่อไป กำหนดลิสต์ติดตาม index numbers ของแซนด์วิชที่สั่งมากกว่า 75 ครั้ง:

orders_75 = []

ตอนนี้ลิสต์ว่าง เพราะยังไม่รู้ว่าแซนด์วิชไหนสั่งเกิน 75 ครั้ง

วนลูปลิสต์แซนด์วิชและออเดอร์ หาแซนด์วิชที่สั่งเกิน 75 ครั้ง 用 for loop:

for s in range(0, len(sandwiches)):
    if orders[s] > 75:
        orders_75 = orders_75 + s

โค้ดวนทุกตัวเลขใน range 0 ถึงความยาว "sandwiches" ถ้าค่าที่สัมพันธ์ใน "orders" มากกว่า 75 เพิ่ม index position ของแซนด์วิชนั้นเข้า "orders_75"

เขียน for loop พิมพ์แซนด์วิชที่สั่งเกิน 75 ครั้ง เช็คว่าโปรแกรมทำงาน:

for o in orders_75:
    print(sandwiches[o])

รัน Python code:

Traceback (most recent call last):
File "main.py", line 9, in 
orders_75 = orders_75 + s
TypeError: can only concatenate list (not "int") to list

โค้ด error

วิธีแก้ (The Solution)

ดูบรรทัดที่ error message ชี้:

orders_75 = orders_75 + s

เราพยายาม concatenate integer value เข้า list ไม่ได้ เพราะ concatenation ทำงานได้เฉพาะ iterable objects ที่ data type เหมือนกัน

แก้ error: ใช้ append() method เพิ่ม item เข้า list เมธอดนี้ออกแบบมาสำหรับเพิ่ม items เข้า lists Items สามารถเป็น data type ใดก็ได้: dictionaries, integers, floating-point numbers

เปลี่ยนบรรทัด orders_75 ใช้ append():

orders_75.append(s)

ไม่ต้อง assign ค่าให้ "orders_75" เพราะ append() เพิ่ม item เข้า list in-place รันโค้ด:

Chicken Club
Cheese

โค้ดพิมพ์ลิสต์แซนด์วิชที่สั่งเกิน 75 ครั้ง มี 2 ชนิด: Chicken Club และ Cheese

สรุป

Error TypeError: can only concatenate list (not "int") to list เกิดเมื่อพยายาม concatenate integer เข้า list

Error เกิดเพราะ concatenate ได้เฉพาะ list กับ list วิธีแก้: ใช้ append() method เพิ่ม item เข้า list

ตอนนี้คุณพร้อมแก้ Python error นี้อย่างมืออาชีพแล้ว!