python - 从数字列表中删除最小元素,其中每个数字都与年份列表相关联
问题描述
1. num_list = [ 5, 1, 3, 2, 4, 56, 19, 8, 1, 7]
2. year_list = [2020, 2021, 2019, 2020, 2019, 2017, 2017, 2020, 2020, 2021]
3. year = 2020
#我想从 2020 年删除数字 1。
#这是我目前所拥有的...
num_list = [ 5, 1, 3, 2, 4, 56, 19, 8, 1, 7]
year_list = [2020, 2021, 2019, 2020, 2019, 2017, 2017, 2020, 2020, 2021]
year = 2020
batch_list=[]
for n_list,y_list in zip(num_list,year_list):
if(y_list==year):
batch_list.append(n_list)
batch_number=min(batch_list)
batch_index= num_list.index(batch_number)
num_list.remove(batch_number)
del year_list[batch_index]
解决方案
num = [5,1,3,2,4,56,19,8,1,7]
year_list=[2020,2021,2019,2020,2019,2017,2017,2020,2020,2021]
year=2020
stored = []
for j in range(0, len(year_list)):
if (year_list[j] == year):
stored.append((num[j], j))
stored.sort()
lowest_num_for_year = stored[:1]
num.pop(lowest_num_for_year[0][1])
year_list.pop(lowest_num_for_year[0][1])
print(year)
print(lowest_num_for_year[0][0])
print(num)