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

วิธีเรียงลำดับ List ของ String ใน Python

1. เรียงในตัว (in-place) ด้วย sort()

>> a = ["Hello", "My", "Followers"]
>>> a.sort()
>>> print(a)
['Followers', 'Hello', 'My']

2. สร้างลิสต์ใหม่เรียงแล้ว ด้วย sorted()

>> a = ["Hello", "My", "Followers"]
>>> b = sorted(a)
>>> print(b)
['Followers', 'Hello', 'My']

ค่าเดิม a ไม่เปลี่ยนแปลง