0

I am having a problem with my code and getting it to work. Im not sure if im sorting this correctly. I am trying to sort with out lambda pandas or itemgetter.

Here is my code that I am having issues with.


with open('ManufacturerList.csv', 'r') as man_list:
    ml = csv.reader(man_list, delimiter=',')
    for row in ml:
        manufacturerList.append(row)
        print(row)
with open('PriceList.csv', 'r') as price_list:
    pl = csv.reader(price_list, delimiter=',')
    for row in pl:
        priceList.append(row)
        print(row)
with open('ManufacturerList.csv', 'r') as service_list:
    sl = csv.reader(service_list, delimiter=',')
    for row in sl:
        serviceList.append(row)
        print(row)

new_mfl = (sorted(manufacturerList, key='None'))
new_prl = (sorted(priceList, key='None'))
new_sdl = (sorted(serviceList, key='None'))

for x in range(0, len(new_mfl)):
    new_mfl[x].append(priceList[x][1])

for x in range(0, len(new_mfl)):
    new_mfl[x].append(serviceList[x][1])

new_list = new_mfl
inventoryList = (sorted(list, key=1))

i have tried to use the def function to try to get it to work but i dont know if im doing it right. This is what i tried.

def new_mfl(x):
    return x[0]


x.sort(key=new_mfl)
Victor
  • 1
  • `key='None'` won't sort anything. It's not `key=None`! Just remove the parameter. Even `new_mfl` returns `x[0]` which is more or less like natural order sort (no parameter) – Jean-François Fabre Dec 12 '22 at 21:18
  • What exactly are you trying to do? What data are you using? What results are you getting? Are they different from the results you expect, and if so, how? – MattDMo Dec 12 '22 at 21:18
  • you have issues understanding how `key` parameter of `sort` works. If you pass a constant, then nothing will be done on your list – Jean-François Fabre Dec 12 '22 at 21:19
  • I am trying to sort a csv file and use a key to which links all three files. I am trying to create a new csv file that has all the items sorted – Victor Dec 12 '22 at 21:26
  • i know that i can sort it using itemgetter by new_mfl=(sorted(mfl, key=itemgetter(0))) new_prl=(sorted(prl, key=itemgetter(0))) new_sdl=(sorted(sdl, key=itemgetter(0))) – Victor Dec 12 '22 at 21:29
  • but i am trying to use a basic fuction to sort – Victor Dec 12 '22 at 21:29
  • The `key` argument has to be a function, which returns the part of the item that should be compared when sorting. It makes no sense to use a string or number as the key. – Barmar Dec 12 '22 at 22:06
  • 1
    Did you really intend to read the same CSV file for both `manufacturerList` and `serviceList`? – Barmar Dec 12 '22 at 22:10
  • FYI, you can replace the `for` loops with just `manufacturerList = list(ml)` – Barmar Dec 12 '22 at 22:11
  • once you have `new_mfl` use [writerows](https://docs.python.org/3/library/csv.html#writer-objects) with all data sorted, by `open('some.csv', 'w', newline='') as file: writer = csv.writer(f) writer.writerows(new_mfl)` – user11717481 Dec 13 '22 at 00:46

2 Answers2

1

You can do it like this:

def manufacturer_key(x):
    return x[0]

sorted_mfl = sorted(manufacturerList, key=manufacturer_key)

The key argument is the function that extracts the field of the CSV that you want to sort by.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

sorted_mfl = sorted(manufacturerList, key=lambda x: x[0])

There are different Dialects and Formatting Parameters that allow to handle input and output of data from comma separated value files; Maybe it could be used in a way with fewer statements using the correct delimiter which depends on the type of data you handle, this would be added to using built in methods like split for string data or another method to sort and manipulate lists, for example for single column data, delimiter=',' separate data by comma and it would iterate trough each value and not as a list of lists when you call csv.reader

['9.310788653967691', '4.065746465800029', '6.6363356879192965', '7.279020237137884', '4.010297786910394'] 
['9.896092029283933', '7.553018448286675', '0.3268282119829197', '2.348011394854333', '3.964531054345021'] 
['5.078622663277619', '4.542467725728741', '3.743648062104161', '12.761916277286993', '9.164698479088221'] 

# out:
             column1             column2             column3             column4             column5
0  4.737897984379577   6.078414943611958  2.7021438955897095  5.8736388919905895   7.878958949784588
1  4.436982168483749  3.9453563399358544   12.66647791861843   5.323017508568736   4.156777982870004
2  4.798241413768279  12.690268531982028   9.638858110105895   7.881360524434767  4.2948334000783195

This is achieved because I am using lists that contain singular values, since for columns or lists that are of the form sorted_mfl = {'First Name' : ['name', 'name', 'name'], 'Second Name ' : [...], 'ID':[...]}, new_prl = ['example', 'example', 'example'] new_sdl = [...] the data would be added by something like sorted_mfl + new_prl + new_sdl and since different modules are also used to set and manage comma separated files, you should add more information to your question like the data type you use or create a minimal reproducible example with pandas.

user11717481
  • 1
  • 9
  • 15
  • 25