Mirzo ulug‘bek nomidagi


Download 148.56 Kb.
bet8/13
Sana30.04.2023
Hajmi148.56 Kb.
#1406197
1   ...   5   6   7   8   9   10   11   12   13
Bog'liq
Dasturiy injenering

Python 3

# Python program to demonstrate
# Removal of elements in a List

# Creating a List
List = [1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12]
print("Initial List: ")
print(List)

# Removing elements from List
# using Remove() method
List.remove(5)
List.remove(6)
print("\nList after Removal of two elements: ")
print(List)

Chiqish
Dastlabki ro'yxat:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]


Ikki element olib tashlanganidan keyin ro'yxat:
[1, 2, 3, 4, 7, 8, 9, 10, 11, 12]
2-misol:
Python 3

# Creating a List
List = [1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12]
# Removing elements from List
# using iterator method
for i in range(1, 5):
List.remove(i)
print("\nList after Removing a range of elements: ")
print(List)

Chiqish
Bir qator elementlarni olib tashlaganingizdan so'ng ro'yxat:
[5, 6, 7, 8, 9, 10, 11, 12]
Ro'yxatdagi elementlarni o'chirish uchun murakkabliklar (remove() usuli):
Vaqt murakkabligi: O(n)
Kosmik murakkablik: O(1)
2-usul: pop() usulidan foydalanish
pop() funktsiyasidan elementni ro'yxatdan olib tashlash va qaytarish uchun ham foydalanish mumkin, lekin sukut bo'yicha u ro'yxatning faqat oxirgi elementini olib tashlaydi, elementni Ro'yxatning ma'lum bir pozitsiyasidan olib tashlash uchun element indeksi uzatiladi. pop() usuliga argument sifatida.
Python 3

List = [1, 2, 3, 4, 5]

# Removing element from the
# Set using the pop() method
List.pop()
print("\nList after popping an element: ")
print(List)

# Removing element at a
# specific location from the
# Set using the pop() method
List.pop(2)
print("\nList after popping a specific element: ")
print(List)

Chiqish
Elementni ochgandan so'ng ro'yxat:
[1, 2, 3, 4]


Muayyan elementni ochgandan so'ng ro'yxat:
[1, 2, 4]
Ro'yxatdagi elementlarni o'chirish uchun murakkabliklar (pop() usuli):
Vaqt murakkabligi: O(1)/O(n) (O(1) oxirgi elementni olib tashlash uchun, O(n) birinchi va oʻrta elementlarni olib tashlash uchun)
Kosmik murakkablik: O(1)
Ro'yxatni kesish
Biz tilim yordamida pastki qatorlar va pastki ro'yxatlarni olishimiz mumkin. Python List-da butun ro'yxatni barcha elementlar bilan chop etishning bir necha yo'li mavjud, ammo ro'yxatdagi elementlarning ma'lum diapazonini chop etish uchun biz Slice operatsiyasidan foydalanamiz . 

Download 148.56 Kb.

Do'stlaringiz bilan baham:
1   ...   5   6   7   8   9   10   11   12   13




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling