3 תשובות
import random
# generate list with random numbers between 0 and 5
original_list = [random.randint(0, 5) for _ in range(15)]
print("original list:")
print(original_list)
# move all zeros to the end
final_list = [i for i in original_list if i != 0] + [i for i in original_list if i == 0]
print("\nlist after moving all zeros to the end:")
print(final_list)
# generate list with random numbers between 0 and 5
original_list = [random.randint(0, 5) for _ in range(15)]
print("original list:")
print(original_list)
# move all zeros to the end
final_list = [i for i in original_list if i != 0] + [i for i in original_list if i == 0]
print("\nlist after moving all zeros to the end:")
print(final_list)
import random
# generate a list of 15 random numbers between 0 and 5
original_list = [random.randint(0, 5) for _ in range(15)]
print("original list:", original_list)
# move all zeros to the end of the list
zeros_count = original_list.count(0)
modified_list = [num for num in original_list if num != 0] + [0] * zeros_count
print("modified list:", modified_list)
# generate a list of 15 random numbers between 0 and 5
original_list = [random.randint(0, 5) for _ in range(15)]
print("original list:", original_list)
# move all zeros to the end of the list
zeros_count = original_list.count(0)
modified_list = [num for num in original_list if num != 0] + [0] * zeros_count
print("modified list:", modified_list)
'''
import random
my_list = []
for i in range(15):
my_list.append(random.randint(0, 5))
print("original list:", my_list)
count = my_list.count(0)
for i in range(count):
my_list.remove(0)
my_list.append(0)
print("list after moving all zeros to the end:", my_list)
'''
התוכנית מייצרת רשימה רנדומלית של 15 מספרים בין 0 ל-5, מדפיסה את הרשימה המקורית ואז מעבירה את כל האפסים לסוף הרשימה ומדפיסה את הרשימה המעודכנת
import random
my_list = []
for i in range(15):
my_list.append(random.randint(0, 5))
print("original list:", my_list)
count = my_list.count(0)
for i in range(count):
my_list.remove(0)
my_list.append(0)
print("list after moving all zeros to the end:", my_list)
'''
התוכנית מייצרת רשימה רנדומלית של 15 מספרים בין 0 ל-5, מדפיסה את הרשימה המקורית ואז מעבירה את כל האפסים לסוף הרשימה ומדפיסה את הרשימה המעודכנת