Python 在函数调用中拆分元组

Python 在函数调用中拆分元组,python,sorting,Python,Sorting,更新:通过引用前一篇文章,我的最终目标更加明确 使用以下代码行 result = list(zip(*sorted(zip(l1, l2, l3, files_good_list), section(l1), key = lambda x: float(x[0])))) 节在哪里 def section(s): return[int(_) for _ in s.split(".")] l1、l2、l3、文件列表是字符串列表 我的目标是合并这四个列表,然后按l1排序。在哪里 l1

更新:通过引用前一篇文章,我的最终目标更加明确

使用以下代码行

result = list(zip(*sorted(zip(l1, l2, l3, files_good_list), section(l1), key = lambda x: float(x[0]))))
节在哪里

 def section(s):
     return[int(_) for _ in s.split(".")]
l1、l2、l3、文件列表是字符串列表

我的目标是合并这四个列表,然后按l1排序。在哪里

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']
如果我使用

结果=列表(zip(*已排序(zip(l1、l2、l3,文件\u良好\u列表),key=lambda x:float(x[0]))

但它确实将l1排序为“1”、“1.1”、“1.10”、“1.2”,我希望l1排序为“1”、“1.1”、“1.2”、“1.10”。这就是为什么我试图使用函数部分按我想要的顺序排序

我从这篇文章的答案中找到了类似的部分

然而,当我试图将其作为参数传入时,我得到了这个错误

 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'
我没有得到一个错误,它按照我需要的顺序进行排序。所以我的问题是,为什么我不能将节传递到排序中,而它位于zip中


如果您需要任何澄清,请告诉我。谢谢

部分
接受一个包含由点分隔的整数的字符串,但在代码中,您正在传递一个列表。正确的使用方法如下:

result=list(zip(*已排序(zip(l1、l2、l3,文件\u良好\u列表),key=lambda x:section(x[0]))

但话说回来,我真的不确定你想用代码部分做什么


一条建议是避免使用这样的一行程序,并将代码分解成更可读的块。这使得人们很难理解你在做什么。

部分接受一个包含由点分隔的整数的字符串,但在你的代码中,你传递给它一个列表。正确的使用方法如下:

result=list(zip(*已排序(zip(l1、l2、l3,文件\u良好\u列表),key=lambda x:section(x[0]))

但话说回来,我真的不确定你想用代码部分做什么


一条建议是避免使用这样的一行程序,并将代码分解成更可读的块。这使得人们很难理解你想做什么。

我会给你一个简单的例子,说明你想要什么。一个部分有两个部分(至少在本例中是这样)。你有
x.y
,其中
x
可以是任何数字,我假设
y
可以是
1-10
。在这种情况下,您可以创建一个自定义比较函数,该函数根据
y
的最大值对
x
进行加权

可以这样做:

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']
def section(s):
    # Set the inital values
    s1, s2 = 0, 0
    # If there is a `.` then we have a subsection
    if s.find('.') != -1:
        s1, s2 = s.split('.')
        s1, s2 = int(s1), int(s2)
    # Otherwise it's whatever value is there
    else:
        s1 = int(s)
    # Perform the actual weighting of the section and subsection
    return s1*10+s2

print(sorted(l1, key=section))
# Prints ['1',
 '1',
 '1.1',
 '1.1',
 '1.2',
 '1.2',
 '1.3',
 '1.4',
 '1.10',
 '2',
 '2.1',
 '2.1',
 '2.10',
 '3',
 '3.1',
 '3.1',
 '3.2',
 '3.3',
 '3.4',
 '3.5',
 '3.6',
 '3.7',
 '3.8']

我给你举一个你想要的例子。一个部分有两个部分(至少在本例中是这样)。你有
x.y
,其中
x
可以是任何数字,我假设
y
可以是
1-10
。在这种情况下,您可以创建一个自定义比较函数,该函数根据
y
的最大值对
x
进行加权

可以这样做:

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']
def section(s):
    # Set the inital values
    s1, s2 = 0, 0
    # If there is a `.` then we have a subsection
    if s.find('.') != -1:
        s1, s2 = s.split('.')
        s1, s2 = int(s1), int(s2)
    # Otherwise it's whatever value is there
    else:
        s1 = int(s)
    # Perform the actual weighting of the section and subsection
    return s1*10+s2

print(sorted(l1, key=section))
# Prints ['1',
 '1',
 '1.1',
 '1.1',
 '1.2',
 '1.2',
 '1.3',
 '1.4',
 '1.10',
 '2',
 '2.1',
 '2.1',
 '2.10',
 '3',
 '3.1',
 '3.1',
 '3.2',
 '3.3',
 '3.4',
 '3.5',
 '3.6',
 '3.7',
 '3.8']

您正在按键的浮点值排序,并想知道为什么它会将
1.1
1.10
放在一起,而
1.10
放在
1.2
之前?在我看来,这正是你应该期待的……我知道它会这样做,但这不是我想要的排序方式。这就是函数部分将其排序为1.1、1.2、1.10而不是1.1、1.10、1.2的原因。但是
sorted
的函数签名要么是
sorted(iterable,cmp=None,key=None,reverse=False)
(Python 2.x)要么是
sorted(iterable,key=None,reverse=False)
(Python 3.x)。我不知道你怎么期望你的
部分
函数发挥作用-它很可能作为一个无关的参数被忽略了…好吧,我没有意识到,有没有其他方法可以做到这一点?我是python新手,我已经用尽了有限的编程知识。@它不应该是
key=section()
?您正在按键的浮点值排序,并且想知道为什么它将
1.1
1.10
放在一起,以及
1.10
放在
1.2
之前?在我看来,这正是你应该期待的……我知道它会这样做,但这不是我想要的排序方式。这就是函数部分将其排序为1.1、1.2、1.10而不是1.1、1.10、1.2的原因。但是
sorted
的函数签名要么是
sorted(iterable,cmp=None,key=None,reverse=False)
(Python 2.x)要么是
sorted(iterable,key=None,reverse=False)
(Python 3.x)。我不知道你怎么期望你的
部分
函数发挥作用-它很可能作为一个无关的参数被忽略了…好吧,我没有意识到,有没有其他方法可以做到这一点?我是python新手,我已经用尽了有限的编程知识。@不应该是
key=section()
?如果您按照更新下的链接进行操作,我相信我的代码会变得更清晰。刚刚做了,我手边没有解释器,但我相信答案中的这一行会起到作用。如果你按照更新下的链接进行操作,我相信我的代码会变得更清晰。刚刚做了,我手边没有解释器,但我相信答案中的这一行会起到作用。