1

I have list:

productlinks = ['google.com', 'tes.com', 'lol.com']
name = ['google', 'tes', 'lol']
prices = ['$ 125.000,00', '$ 123.000,00','$ 135.000.000,00']

I want to linked each other and sorted it by the price, so when i sorted it would be look like this:

 productlinks = ['tes.com', 'google.com', 'lol.com']
 name = ['tes', 'google', 'lol']
 prices = ['$ 123.000,00', '$ 125.000,00','$ 135.000.000,00']

I have used zip function to merged it, but i dont know how to sort it by the price.

NOTE: the price in a string.

Awan
  • 25
  • 8

2 Answers2

1

To sort the list you get after zipping all three lists you can use this code,

import re

new_prices = list(map(lambda text: re.findall("\d+\.\d+", text)[0], prices))
final_list = list(zip(productlinks, name, new_prices))
sorted_list = pd.DataFrame(final_list).sort_values(by = 2).values

I have used the pandas module for the sorting part here.

The first line gets rid of all the "," and "$" from the price and converts the rest into float. Then these new prices are zipped with the other two lists and the zipped output we get is then converted into a list.

Because final_list looks an awful lot like a table, I converted it into a pandas dataframe and sorted it by the column with the prices values. Now that the whole dataframe has been sorted, I convert it into an array using .values.

If you want to add "$" you can replace the third line with the code below,

sorted_df = pd.DataFrame(final_list).sort_values(by = 2)
sorted_df[2] = "$" + sorted_df[2].astype(str)
sorted_list = sorted_df.values
Zero
  • 1,800
  • 1
  • 5
  • 16
  • This is what works for me, but actually my prices is look like this: ["$ 13.000,00", "$ 12.000,00", $ 1.000,00"] and because of that there is errorValueError: could not convert string to float. – Awan Apr 25 '22 at 04:22
  • Check the updated answer. – Zero Apr 25 '22 at 05:36
  • It's almost solve the problem, but if there is data like $ 5.000.000,00 it will be error because there is 2 '.' and 1 ',' the error: ValueError: could not convert string to float: ' 5.000.00000' – Awan Apr 25 '22 at 09:21
  • success with this new_prices = list(map(lambda price: int(price.replace('.', '').replace(',00', '').strip('$ ')), prices)) thank you for your help – Awan Apr 25 '22 at 09:51
  • but new answer is not sorted ya? – Awan Apr 25 '22 at 10:16
  • I think it still better previous answer + last added from me. its not remove number, and just focus on '.' and ',' but the code is ugly – Awan Apr 25 '22 at 10:19
  • Yh it's using `re` so it's bound to be pretty ugly. But this code is more understandable and efficient than the previous one. You can sort it using the code in the below code cells. – Zero Apr 25 '22 at 11:35
0

using sort function and providing the key to on basic of which you need to sort , you can sort data


>>> productlinks = ['google.com', 'tes.com', 'lol.com']
>>> name = ['google', 'tes', 'lol']
>>> prices = ['$ 125', '$ 123','$ 135']
>>> 
>>> l = list(zip(productlinks, name, prices))
>>> l.sort(key=lambda x:int(x[2].split()[-1]))
>>> 
>>> l
[('tes.com', 'tes', '$ 123'), ('google.com', 'google', '$ 125'), ('lol.com', 'lol', '$ 135')]
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
  • This is what works for me, but actually my prices is look like this: ["$ 13.000,00", "$ 12.000,00", $ 1.000,00"] and because of that there is errorValueError: could not convert string to float. – Awan Apr 25 '22 at 04:26
  • i have also splited the prices so it look like 123.000 but when i convert it to int, error occured " invalid literal for int() with base 10" – Awan Apr 25 '22 at 04:27
  • use `float` or `double` – sahasrara62 Apr 25 '22 at 04:56
  • cannot use float or double, because there is ',' and '.' in the string. so it return error. – Awan Apr 25 '22 at 09:22