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

แปลงตัวพิมพ์เล็ก/ใหญ่ของสมาชิกในลิสต์สตริงใน 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 : ['MoN', 'TuE', 'FRI']
New all lowercase list: ['mon', 'tue', 'fri']

ใช้ upper()

list comprehension: [x.upper() for x in listA]

ตัวอย่าง

listA = ['MoN', 'TuE', 'FRI']
print("Given list : \n", listA)
res = [x.upper() for x in listA]
print("New all uppercase list: \n", res)

ผลลัพธ์

Given list : ['MoN', 'TuE', 'FRI']
New all uppercase list: ['MON', 'TUE', 'FRI']