Update: By referencing this previous post I made my ultimate goal is much clearer. Combing 2D list of tuples and then sorting them in Python
With the following line of code
result = list(zip(*sorted(zip(l1, l2, l3, files_good_list), section(l1), key = lambda x: float(x[0]))))
Where section is
def section(s):
return[int(_) for _ in s.split(".")]
and l1, l2, l3, files_good_list are list of strings.
My goal is to combine these four list and them sort then by l1. Where
l1 = ['1', '1.1', '1.2', '1.10', '2.1', '3.1', '1', '1.1', '1.2', '1.3', '1.4', '2', '2.1', '2.10', '3', '3.1', '3.2', '3.3', '3.4', '3.5', '3.6', '3.7', '3.8']
My code works if I use
result = list(zip(*sorted(zip(l1, l2, l3, files_good_list), key = lambda x: float(x[0]))))
but it does it sorts l1 as '1', '1.1', 1.10', '1.2' where I want l1 to sort as '1', '1.1', '1.2', '1.10'. This is why I am trying to use the function section to sort in the order I want.
I found section from an answer to this post which is similar How do I sort a list of section numbers in Python?
However, when I try to pass it in as an argument I get this error.
Traceback (most recent call last):
File "<ipython-input-422-bbd574034cbd>", line 1, in <module>
runfile('C:/Users/justin.white/Documents/Work/Regex_try.py', wdir='C:/Users/justin.white/Documents/Work')
File "C:\Users\justin.white\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
execfile(filename, namespace)
File "C:\Users\justin.white\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 88, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "C:/Users/justin.white/Documents/Work/Regex_try.py", line 84, in <module>
result = list(zip(*sorted(zip(l1, l2, l3, files_good_list), section(l1), key = lambda x: float(x[0]))))
File "C:/Users/justin.white/Documents/Work/Regex_try.py", line 82, in section
return[int(_) for _ in s.split(".")]
AttributeError: 'list' object has no attribute 'split'
But when I do
sorted(l1, key=section)
I do not get an error and it sorts it in the order I need. So my question is why can I not pass section into sorted when it is in a zip?
If you need any clarification let me know. Thanks