1

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!

1 Answer 1

1

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_numbers, temp_ids = [], []
for count, number, id, distance in zip(counts, numbers, ids, distances):
    if count >= 5 and distance >= 3:
        temp_numbers.append(number)
        temp_ids.append(id)

for (number, id) in zip(temp_numbers, temp_ids):
    if temp_numbers.count(number) == 3:
        output.append(id)

output will be:

[10, 11, 12]
Sign up to request clarification or add additional context in comments.

1 Comment

Cheers. Note that I updated the code to match your own style of using lists with zip. So now instead of one temp you have temp_numbers and temp_ids. Makes understanding the second part even easier.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.