Python熊猫:星号符号表达式的含义

Python熊猫:星号符号表达式的含义,python,pandas,Python,Pandas,在寻找在一个循环中将一列拆分为多列的方法时,我偶然发现其中包含以下表达式: # Split delimited values in a DataFrame column into two new columns df['new_col1'], df['new_col2'] = zip(*df['original_col'].apply(lambda x: x.split(': ', 1))) 它工作得很好,但我无法理解它是如何工作的,特别是关于*符号。到目前为止,我只在函数定义中看到了星号,

在寻找在一个循环中将一列拆分为多列的方法时,我偶然发现其中包含以下表达式:

 # Split delimited values in a DataFrame column into two new columns
df['new_col1'], df['new_col2'] = zip(*df['original_col'].apply(lambda x: x.split(': ', 1)))
它工作得很好,但我无法理解它是如何工作的,特别是关于*符号。到目前为止,我只在函数定义中看到了星号,我还没有找到任何关于这种情况的文档

有人能解释一下它是怎么工作的吗

zip()与*运算符一起可用于解压缩列表:

输出:

[(1, 4), (2, 5), (3, 6)]
x2, y2 = zip(*zipped)
print x2
print y2
(1, 2, 3)
(4, 5, 6)
说明:

[(1, 4), (2, 5), (3, 6)]
x2, y2 = zip(*zipped)
print x2
print y2
(1, 2, 3)
(4, 5, 6)
它从列表中获取值:
x
y
(列中)并将其保存在元组中


还有(这是你感兴趣的部分):

[(1, 4), (2, 5), (3, 6)]
x2, y2 = zip(*zipped)
print x2
print y2
(1, 2, 3)
(4, 5, 6)
输出:

[(1, 4), (2, 5), (3, 6)]
x2, y2 = zip(*zipped)
print x2
print y2
(1, 2, 3)
(4, 5, 6)
说明:

[(1, 4), (2, 5), (3, 6)]
x2, y2 = zip(*zipped)
print x2
print y2
(1, 2, 3)
(4, 5, 6)
  • zip解压缩
    zip
    的内容(从列表中取出内容)
  • 在列中从每个元组中获取值,并将其保存在元组中
  • 因此,如果我们将这些元组放在列中(在解包之前),它们将如下所示:

    [
        (1, 4)
        (2, 5)
        (3, 6)
    ]
    
    (1, 4)
    (2, 5)
    (3, 6)
    
    一旦打开包装,它们将如下所示:

    [
        (1, 4)
        (2, 5)
        (3, 6)
    ]
    
    (1, 4)
    (2, 5)
    (3, 6)
    
    如果你看到,第一列有1,2和3。第二列有4,5,6

    这就是
    zip
    *
    操作符一起做的事情。


    文档

    谢谢,我已经看到它通常与splat操作员有关,现在它有意义了@PiZed,如果有助于解决问题,请不要忘记接受我的答案:)