Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating a List with 3 Conditions in Python

The code below is a simple modification of the code that reads a page with Selenium and stores the address in a variable according to the condition of the page. (It’s too long, so I’ve simplified some of it.)

a = ["70", "80", "90", "100", "110"]
b = [112, 1513, 14, 505, 36]
c = ["url_1", "url_2", "url_3", "url_4", "url_5"]

last = []
num = 0
for l in a:
    if 110 == int(l):
        last.insert(0, c[num])

    elif 100 == int(l):
        last.append(c[num])

    elif 90 == int(l):
        last.append(c[num])

    elif 80 == int(l):
        last.append(c[num])

    elif 70 == int(l):
        last.append(c[num])
    num += 1

print(last)

Originally, it was the idea of putting the elements of variable-a in variable-last in order of magnitude.

But I found that I need to reorder the contents by the elements of list b.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What I want to do is to sort the elements of variable-a in numerical order, while at the same time sorting them again in order of largest in the elements of variable-b.

For example

a = ["70", "100", "90", "100", "100"]
b = [112, 1513, 14, 505, 36]
c = ["url_1", "url_2", "url_3", "url_4", "url_5"]

When classifying ‘100’ in a above, it is also to consider the size of the numbers on the same index in b and classify them in order of size. So, again, the elements of c with the same index are put into the variable last in order. so finally

last = ["url_2", "url_4", "url_5", "url_3", "url_1"]

I want to complete a list in this order. All day long, I failed. help

>Solution :

You can do this using built-ins:

>>> a = ["70", "100", "90", "100", "100"]
>>> b = [112, 1513, 14, 505, 36]
>>> c = ["url_1", "url_2", "url_3", "url_4", "url_5"]
>>>
>>> sorted(zip(map(int, a), b, c), key=lambda x: x[:2], reverse=True)
[(100, 1513, 'url_2'), (100, 505, 'url_4'), (100, 36, 'url_5'), (90, 14, 'url_3'), (70, 112, 'url_1')]

Then if you want to extract only the "urls":

>>> x = sorted(zip(map(int, a), b, c), key= lambda x: x[:2], reverse=True)
>>> [i[2] for i in x]
['url_2', 'url_4', 'url_5', 'url_3', 'url_1']

The way sorted() works, is that it tries to compare each value, index for index. That means that you can give it a way to sort values based on different "columns" of the sliced value.

You can customize that sorting via the key keyword. You can give it a function (or as I did above, a lambda function).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading