Created
May 25, 2020 17:36
-
-
Save jigi-33/1690806839e8c9cd08a3d7948a76b05e to your computer and use it in GitHub Desktop.
Удаление элементов списка (чисел) по определенному условию - алгоритм средней сложности
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Удаление элементов списка по условию | |
==================================== | |
""" | |
# Задача: из списка чисел удалить элементы, значения которых больше 35 и меньше 65. При этом удаляемые числа сохранить в другом списке. | |
# В Python с помощью инструкции del можно удалить элемент списка, указав сам список и индекс удаляемого элемента. | |
import random | |
a = [] | |
for i in range(20): | |
n = round(random.random() * 100) | |
a.append(n) | |
print("A =", a) | |
b = [] | |
i = 0 | |
while i < len(a): | |
if 35 < a[i] < 65: | |
b.append(a[i]) | |
del a[i] | |
else: | |
i += 1 | |
print("A =", a) | |
print("B =", b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment