I've to compare two lists by indexes, if in list 1 index is equal to 0 than all elements in list2 are 0.
list1 = [0, 1, 1, 0] # that's for example
list2 = [[15, 19, 13, 15, 30, 14, 14], [14, 22, 30, 19, 29, 17, 15], [19, 21, 11, 25, 23, 23, 30], [15, 15, 25, 18, 22, 24, 29], [24, 30, 30, 11, 25, 18, 27]]
output:
list2 = [[0, 0, 0, 0, 0, 0, 0], [14, 22, 30, 19, 29, 17, 15], [19, 21, 11, 25, 23, 23, 30], [0, 0, 0, 0, 0, 0, 0]]
list1 = [0, 1, 1, 0]
list2 = [[15, 19, 13, 15, 30, 14, 14], [14, 22, 30, 19, 29, 17, 15], [19, 21, 11, 25, 23, 23, 30], [15, 15, 25, 18, 22, 24, 29], [24, 30, 30, 11, 25, 18, 27]]
new_list=[]
for i in list1:
for index,j in enumerate(list2):
if(i==index):
new_list.append([x*i for x in j] )
break
print(new_list)
This should also do:
list1 = [0, 1, 1, 0] # that's for example
list2 = [[15, 19, 13, 15, 30, 14, 14], [14, 22, 30, 19, 29, 17, 15], [19, 21, 11, 25, 23, 23, 30], [15, 15, 25, 18, 22, 24, 29], [24, 30, 30, 11, 25, 18, 27]]
result = [[0] * len(list2[index]) if index == 0 else list2[index] for index in list1]
print(result)
My take without any if statements
list1 = [0, 1, 1, 0]
list2 = [[15, 19, 13, 15, 30, 14, 14], [14, 22, 30, 19, 29, 17, 15], [19, 21, 11, 25, 23, 23, 30], [15, 15, 25, 18, 22, 24, 29], [24, 30, 30, 11, 25, 18, 27]]
output_list = []
for index, item in enumerate(list1):
output_list.append([element * item for element in list2[index]])
print(output_list)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.