Home > OS >  Apply if statement on multiple lists with multiple conditions
Apply if statement on multiple lists with multiple conditions

Time:03-19

I would like to append ids to a list which meet a specific condition.

    output = []
    areac = [4, 4, 4, 4, 1, 6, 7,8,9,6, 10, 11]
    arean = [1, 1, 1, 4, 5, 6, 7,8,9,10, 10, 10]
    id = [1, 2, 3, 4, 5, 6, 7,8,9,10, 11, 12]
    dist = [2, 2, 2, 4, 5, 6, 7.2,5,5,5, 8.5, 9.1]
    for a,b,c,d in zip(areac,arean,id,dist):
            if a >= 5 and b==b and d >= 3:
                output.append(c)
                print(comp)
            else:
                pass
The condition is the following:
- areacount has to be >= 5
- At least 3 ids with a distance of >= 3 with the same area_number

So the id output should be [10,11,12].I already tried a different attempt with Counter that didn't work out. Thanks for your help!

CodePudding user response:

Here you go:

I changed the list names to something more descriptive.

output = []
area_counts = [4, 4, 4, 4, 1, 6, 7, 8, 9, 6, 10, 11]
area_numbers = [1, 1, 1, 4, 5, 6, 7, 8, 9, 10, 10, 10]
ids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
distances = [2, 2, 2, 4, 5, 6, 7.2, 5, 5, 5, 8.5, 9.1]

temp = []
for count, number, id, distance in zip(area_counts, area_numbers, ids, distances):
    if count >= 5 and distance >= 3:
        temp.append((number, id))

for (number, id) in temp:
    if [x[0] for x in temp].count(number) == 3:
        output.append(id)

output will be:

[10, 11, 12]
  • Related