'Python - I have two different list. I want to find the percentage of negative values(from list2) that corresponds to value:2 in list1?

list1 = [1,2,3,4,2,4,2,1,2,2]
list2 = [2,-3,4,5,6,7,-8,9,-1,3]
list3 = []
for x in range(0,len(list1),1):
    if(list1[x] == 2):
         list3.append(list2[x])
list4 = [y for y in list3 if y<0] #this will store the negative values in list4

I don't know how to calculate the percentage of negative values that is in list4. Should I divide the len(list4)/len(list3)*100 or len(list4)/len(list2)*100 to get the percentage?



Solution 1:[1]

I'm not sure I'm understanding this correctly, but if you are trying to find the percentage of negative numbers from List 2 that correspond to every index where the value is "2" in List 1 then I think this might work:

    list1 = [1, 2, 3, 4, 2, 4, 2, 1, 2, 2]
    list2 = [2, -3, 4, 5, 6, 7, -8, 9,-1 ,3]
    list3 = []
    for i in range(len(list1)):
        if list2[i] < 0 and list1[i] == 2: 
            list3.append(list2[i])

    percentage = (len(list3) / len (list1)) * 100

    print(percentage)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 peachyoana