Reputation: 23
Basically there are 3 lists: list1, list2, list3
list1 and list2 have 20 elements whilst list3 have 10 elements
I'm trying to effectively do this but without the mistake
for x in range(0, len(list3)):
for i in range(0, len(list1)):
if list1[i] > 20:
list3[x] = list2[i]
the problem being the 'old' value in list3 is stored but then replaced after a new value greater than 20 appears.
EDIT:
list1 = [10,20,30,40,50,60,70,80,90,100]
list2 = [1,2,3,4,5,6,7,8,9,10]
list3 = zeros(5) # array3 = [0,0,0,0,0]
for x in range(0, len(list1)):
if list1[x] >50:
list3[x] = list2[x]
The outcome that I want list3 = [6,7,8,9,10]
The outcome that the above code will product error the element value is not high enough for list3
The code should reject any values below 50 in list1. Get the elements of the values above 50 in list1 and then get the values of the same elements in list2. then store those values into list3
EDIT 2: Changed array to list -- I've just been accustomed to using arrays that I instinctively use the word to refer to both. my bad
Upvotes: 0
Views: 80
Reputation: 5746
There should be no need to zero out a list for your final array. You can set it as an empty list and append each result to array3
. Edit: You're also using lists, not arrays.
array1 = [10,20,30,40,50,60,70,80,90,100]
array2 = [1,2,3,4,5,6,7,8,9,10]
array3 = []
array3 = [array2[idx] for idx, value in enumerate(array1) if value > 50]
#[6, 7, 8, 9, 10]
Each time you match, it will append the value located at index x
in array2
to array3
Upvotes: 1