0

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.

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

1
  • I suggest you learn about dictionaries and classes. Commented Aug 2, 2022 at 3:46

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I think this code could solve it.
1

Use pandas dataframe functions:

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

df = pd.DataFrame([int(i) for i in a], columns=['a'])
df['b'] = b
df['c'] = c

df.sort_values(by = ['a','b'], ascending=False, inplace=True)
print(df['c'].to_list())

2 Comments

Thank you. It's a pity that I can only choose one. I recommend it.
Thanks, good to hear that you found it useful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.