Найти - Пользователи
Полная версия: Pop index out of range .Вывести одинаковые элементы 2-списков, значения в которые поочерёдно вводит пользователь
Начало » Python для новичков » Pop index out of range .Вывести одинаковые элементы 2-списков, значения в которые поочерёдно вводит пользователь
1
Alexanderrrrrror
Здравствуйте! Я из отсортированного списка sum_lists возвращаю число, которое повторяется в нём ,а затем все повторяющиеся числа добавляю в список empty_list_3. Почему возникает ошибка pop index out of range в данной ситуации ?
Весь код:
 iteration_for_the_1st_while_cycle = 0
empty_list_1 = 
n = int(input())
while iteration_for_the_1st_while_cycle < n:
	cycle_input_number_1 = int(input())
	empty_list_1.append(cycle_input_number_1)
	iteration_for_the_1st_while_cycle += 1
empty_list_1.sort()
iteration_for_the_2nd_while_cycle = 0
n_2 = int(input())
empty_list_2 = 
while iteration_for_the_2nd_while_cycle < n_2:
	cycle_input_number_2 = int(input())
	empty_list_2.append(cycle_input_number_2)
	iteration_for_the_2nd_while_cycle += 1
empty_list_2.sort()
sum_lists=empty_list_1+empty_list_2
sum_lists.sort()
empty_list_3=
for i in sum_lists:
	if sum_lists.count(i)>0 and sum_lists.count(i)<=1:
		continue
	
	else:
		sum_lists.pop(i)
		empty_list_3.append(i)
		
result=empty_list_3
empty_list4=
for i in result:
	empty_list4.append(str(i))
get_the_list_from_cycle_for=empty_list4
get_string_from_list=" ".join(get_the_list_from_cycle_for)
print(get_string_from_list)

Логический кусок,где возникает ошибка:

 for i in sum_lists:
	if sum_lists.count(i)>0 and sum_lists.count(i)<=1:
		continue
	
	else:
		sum_lists.pop(i)
		empty_list_3.append(i)
PEHDOM
Alexanderrrrrror
sum_lists.pop(i)
empty_list_3.append(i)
ну мало того изменять список по которому делаешь итерацию идея так себе, так вы еще умудрились смешать мухи с котлетами.
for i in sum_lists: перебирает и помещает в переменную і элементы списка.
sum_lists.pop(i) возвращает и удаляет из списка элемент с индексом i .
Соотвевенно в итоговом списке у вас ахинея, потому как удаляется не текущий элемент, а непойми какой(вернее понятно какой, но явно не тот что нужно). А когда значение элемента списка оказывается больше чем длинна списка вы получаете out of range.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB