متدهای List
پایتون مجموعهای از متدهای داخلی دارد که میتوانید از آنها در List استفاده کنید.
append
یک عنصر را در انتهای List قرار میدهد.
مثال زیر یک عنصر را به متغیر fruits که از نوع List است اضافه میکند.
# Input fruits = ["apple", "banana", "cherry"] fruits.append("orange") print(fruits) # Output ['apple', 'banana', 'cherry', 'orange']
مثال زیر یک List را به یک List دیگر اضافه میکند.
# Input a = ["apple", "banana", "cherry"] b = ["Ford", "BMW", "Volvo"] a.append(b) print(a) # Output ['apple', 'banana', 'cherry', ["Ford", "BMW", "Volvo"]]
clear
تمام عنصرهای یک List را حذف میکند.
مثال زیر تمام عنصرهای لیست fruits را حذف میکند.
# Input fruits = ["apple", "banana", "cherry"] fruits.clear() print(fruits) # Output []
copy
یک کپی از List مشخص شده را برمیگرداند.
مثال زیر یک کپی از لیست fruits را برمیگرداند.
# Input fruits = ["apple", "banana", "cherry"] x = fruits.copy() print(x) # Output ['apple', 'banana', 'cherry']
count
تعداد تکرار عنصر مشخص شده در List را برمیگرداند.
مثال زیر تعداد دفعاتی که مقدار cherry در لیست fruits تکرار شده را برمیگرداند.
# Input fruits = ["apple", "banana", "cherry"] x = fruits.count("cherry") print(x) # Output 1
مثال زیر تعداد دفعاتی که مقدار 9 در List تکرار شده را برمیگرداند.
# Input fruits = [1, 4, 2, 9, 7, 8, 9, 3, 1] x = fruits.count(9) print(x) # Output 2
index
اندیس عنصر مشخصشده را برمیگرداند. در صورت عدم وجود عنصر در List یک استثناء (Exception) برمیگرداند.
مثال زیر اندیس مقدار cherry در List را برمیگرداند.
# Input fruits = ['apple', 'banana', 'cherry'] x = fruits.index("cherry") print(x) # Output 2
مثال زیر اندیس مقدار 32 در List را برمیگرداند.
# Input fruits = [4, 55, 64, 32, 16, 32] x = fruits.index(32) print(x) # Output 3
دقت داشته باشید متد index فقط اندیس اولین عنصر مشخصشدهای را که در List پیدا کند برمیگرداند.
insert
مقدار را در اندیس مشخص شده وارد میکند.
مثال زیر مقدار orange را در موقعیت دوم لیست fruits وارد میکند.
# Input fruits = ['apple', 'banana', 'cherry'] fruits.insert(1, "orange") print(fruits) # Output ['apple', 'orange', 'banana', 'cherry']
pop
عنصر را در موقعیت مشخص شده حذف میکند.
مثال زیر عنصری را که در موقعیت دوم لیست fruits قرار دارد حذف میکند.
# Input fruits = ['apple', 'banana', 'cherry'] fruits.pop(1) print(fruits) # Output ['apple', 'cherry']
مثال زیر مقدار عنصری را که از List حذف کرده چاپ میکند.
# Input fruits = ['apple', 'banana', 'cherry'] x = fruits.pop(1) print(x) # Output banana
متد pop مقدار حذف شده را برمیگرداند.
remove
عنصر مشخص شده را حذف میکند.
مثال زیر عنصر banana را از لیست fruits حذف میکند.
# Input fruits = ['apple', 'banana', 'cherry'] fruits.remove("banana") print(fruits) # Output ['apple', 'cherry']
reverse
ترتیب List را معکوس میکند.
مثال زیر ترتیب لیست fruits را معکوس میکند.
# Input fruits = ['apple', 'banana', 'cherry'] fruits.reverse() print(fruits) # Output ['cherry', 'banana', 'apple']
sort
ترتیب List را به صورت پیشفرض صعودی مرتب میکند.
همچنین میتوانید برای تصمیمگیری در مورد معیارهای مرتبسازی یک تابع ایجاد کنید.
مثال زیر لیست cars را بر اساس حروف الفبا مرتب میکند.
# Input cars = ['Ford', 'BMW', 'Volvo'] cars.sort() print(cars) # Output ['BMW', 'Ford', 'Volvo']
مثال زیر List را نزولی مرتب میکند.
# Input cars = ['Ford', 'BMW', 'Volvo'] cars.sort(reverse=True) print(cars) # Output ['Volvo', 'Ford', 'BMW']
مثال زیر List را بر اساس طول مقدارها مرتب میکند.
# Input # A function that returns the length of the value: def myFunc(e): return len(e) cars = ['Ford', 'Mitsubishi', 'BMW', 'VW'] cars.sort(key=myFunc print(cars) # Output ['VW', 'BMW', 'Ford', 'Mitsubishi']
مثال زیر لیستی از دیکشنریها را بر اساس مقدار year مرتب میکند.
# Input def myFunc(e): return e['year'] cars = [ {'car': 'Ford', 'year': 2005}, {'car': 'Mitsubishi', 'year': 2000}, {'car': 'BMW', 'year': 2019}, {'car': 'VW', 'year': 2011} ] cars.sort(key=myFunc) print(cars) # Output [{'car': 'Mitsubishi', 'year': 2000}, {'car': 'Ford', 'year': 2005}, {'car': 'VW', 'year': 2011}, {'car': 'BMW', 'year': 2019}]
نوشتهی نوع دادهی Dictionary در پایتون را مطالعه کنید.
مثال زیر List را بر اساس طول مقدارها مرتب و سپس معکوس میکند.
# Input # A function that returns the length of the value: def myFunc(e): return len(e) cars = ['Ford', 'Mitsubishi', 'BMW', 'VW'] cars.sort(reverse=True, key=myFunc) print(cars) # Output ['Mitsubishi', 'Ford'', 'BMW', 'VW']
منابع
w3schools.com – Python Operators
Photo by hitesh choudhary from Pexels