• <bdo id='W3xSR'></bdo><ul id='W3xSR'></ul>

      <tfoot id='W3xSR'></tfoot>

      <small id='W3xSR'></small><noframes id='W3xSR'>

      <i id='W3xSR'><tr id='W3xSR'><dt id='W3xSR'><q id='W3xSR'><span id='W3xSR'><b id='W3xSR'><form id='W3xSR'><ins id='W3xSR'></ins><ul id='W3xSR'></ul><sub id='W3xSR'></sub></form><legend id='W3xSR'></legend><bdo id='W3xSR'><pre id='W3xSR'><center id='W3xSR'></center></pre></bdo></b><th id='W3xSR'></th></span></q></dt></tr></i><div id='W3xSR'><tfoot id='W3xSR'></tfoot><dl id='W3xSR'><fieldset id='W3xSR'></fieldset></dl></div>

        <legend id='W3xSR'><style id='W3xSR'><dir id='W3xSR'><q id='W3xSR'></q></dir></style></legend>
      1. Python 中的 *tuple 和 **dict 是什么意思?

        时间:2023-09-01
        <tfoot id='o6Rvs'></tfoot>
          <tbody id='o6Rvs'></tbody>
        • <bdo id='o6Rvs'></bdo><ul id='o6Rvs'></ul>

              • <small id='o6Rvs'></small><noframes id='o6Rvs'>

                <legend id='o6Rvs'><style id='o6Rvs'><dir id='o6Rvs'><q id='o6Rvs'></q></dir></style></legend>
              • <i id='o6Rvs'><tr id='o6Rvs'><dt id='o6Rvs'><q id='o6Rvs'><span id='o6Rvs'><b id='o6Rvs'><form id='o6Rvs'><ins id='o6Rvs'></ins><ul id='o6Rvs'></ul><sub id='o6Rvs'></sub></form><legend id='o6Rvs'></legend><bdo id='o6Rvs'><pre id='o6Rvs'><center id='o6Rvs'></center></pre></bdo></b><th id='o6Rvs'></th></span></q></dt></tr></i><div id='o6Rvs'><tfoot id='o6Rvs'></tfoot><dl id='o6Rvs'><fieldset id='o6Rvs'></fieldset></dl></div>

                  本文介绍了Python 中的 *tuple 和 **dict 是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如 PythonCookbook 中所述,可以在元组之前添加 *.* 在这里是什么意思?

                  <块引用>

                  第 1.18 章.将名称映射到序列元素:

                  从集合导入命名元组Stock = namedtuple('Stock', ['name', 'shares', 'price'])s = 库存(*rec)# 这里rec是一个普通的元组,例如:rec = ('ACME', 100, 123.45)

                  在同一部分,**dict 呈现:

                  <块引用>

                  从集合导入命名元组Stock = namedtuple('Stock', ['name', 'shares', 'price', 'date', 'time'])# 创建原型实例stock_prototype = Stock('', 0, 0.0, None, None)# 将字典转换为股票的函数def dict_to_stock(s):return stock_prototype._replace(**s)

                  **在这里的作用是什么?

                  解决方案

                  在函数调用中

                  *t 表示将此可迭代对象的元素视为此函数调用的位置参数."

                  def foo(x, y):打印(x,y)>>>t = (1, 2)>>>脚)1 2

                  从 v3.5 开始,您还可以在列表/元组/集合文字中执行此操作:

                  >>>[1, *(2, 3), 4][1、2、3、4]

                  **d 表示将字典中的键值对视为此函数调用的附加命名参数."

                  def foo(x, y):打印(x,y)>>>d = {'x':1, 'y':2}>>>食物)1 2

                  从 v3.5 开始,您还可以在字典文字中执行此操作:

                  >>>d = {'a': 1}>>>{'b': 2, **d}{'b':2,'a':1}

                  在函数签名中

                  *t 表示将所有附加的位置参数作为一个元组打包到这个参数中."

                  def foo(*t):打印(t)>>>富(1, 2)(1, 2)

                  **d 表示将所有附加的命名参数带到此函数,并将它们作为字典条目插入到此参数中."

                  def foo(**d):打印(d)>>>富(x=1,y=2){'y':2,'x':1}

                  在赋值和 for 循环中

                  *x 表示消耗右侧的附加元素",但它不必是最后一项.请注意,x 将始终是一个列表:

                  >>>x, *xs = (1, 2, 3, 4)>>>X1>>>xs[2, 3, 4]>>>*xs, x = (1, 2, 3, 4)>>>xs[1、2、3]>>>X4>>>x, *xs, y = (1, 2, 3, 4)>>>X1>>>xs[2, 3]>>>是的4>>>对于 [ (1, 2, 3, 4) ] 中的 (x, *y, z): print(x, y, z)...1 [2, 3] 4


                  请注意,出现在 * 之后的参数仅限关键字:

                  def f(a, *, b): ...f(1, b=2) # 很好f(1, 2) # 错误:b 仅是关键字

                  Python3.8 添加了仅位置参数, 表示不能用作关键字参数的参数.它们出现在 / 之前(* 之前的关键字参数的双关语).

                  def f(a,/, p, *, k): ...f( 1, 2, k=3) # 很好f( 1, p=2, k=3) # 很好f(a=1, p=2, k=3) # error: a is positional-only

                  As mentioned in PythonCookbook, * can be added before a tuple. What does * mean here?

                  Chapter 1.18. Mapping Names to Sequence Elements:

                  from collections import namedtuple
                  Stock = namedtuple('Stock', ['name', 'shares', 'price'])
                  s = Stock(*rec) 
                  # here rec is an ordinary tuple, for example: rec = ('ACME', 100, 123.45)
                  

                  In the same section, **dict presents:

                  from collections import namedtuple
                  Stock = namedtuple('Stock', ['name', 'shares', 'price', 'date', 'time'])
                  # Create a prototype instance
                  stock_prototype = Stock('', 0, 0.0, None, None)
                  # Function to convert a dictionary to a Stock
                  def dict_to_stock(s):
                      return stock_prototype._replace(**s)
                  

                  What is **'s function here?

                  解决方案

                  In a function call

                  *t means "treat the elements of this iterable as positional arguments to this function call."

                  def foo(x, y):
                      print(x, y)
                  
                  >>> t = (1, 2)
                  >>> foo(*t)
                  1 2
                  

                  Since v3.5, you can also do this in a list/tuple/set literals:

                  >>> [1, *(2, 3), 4]
                  [1, 2, 3, 4]
                  

                  **d means "treat the key-value pairs in the dictionary as additional named arguments to this function call."

                  def foo(x, y):
                      print(x, y)
                  
                  >>> d = {'x':1, 'y':2}
                  >>> foo(**d)
                  1 2
                  

                  Since v3.5, you can also do this in a dictionary literals:

                  >>> d = {'a': 1}
                  >>> {'b': 2, **d}
                  {'b': 2, 'a': 1}
                  

                  In a function signature

                  *t means "take all additional positional arguments to this function and pack them into this parameter as a tuple."

                  def foo(*t):
                      print(t)
                  
                  >>> foo(1, 2)
                  (1, 2)
                  

                  **d means "take all additional named arguments to this function and insert them into this parameter as dictionary entries."

                  def foo(**d):
                      print(d)
                  
                  >>> foo(x=1, y=2)
                  {'y': 2, 'x': 1}
                  

                  In assignments and for loops

                  *x means "consume additional elements in the right hand side", but it doesn't have to be the last item. Note that x will always be a list:

                  >>> x, *xs = (1, 2, 3, 4)
                  >>> x
                  1
                  >>> xs
                  [2, 3, 4]
                  
                  >>> *xs, x = (1, 2, 3, 4)
                  >>> xs
                  [1, 2, 3]
                  >>> x
                  4
                  
                  >>> x, *xs, y = (1, 2, 3, 4)
                  >>> x
                  1
                  >>> xs
                  [2, 3]
                  >>> y
                  4
                  
                  >>> for (x, *y, z) in [ (1, 2, 3, 4) ]: print(x, y, z)
                  ...
                  1 [2, 3] 4
                  


                  Note that parameters that appear after a * are keyword-only:

                  def f(a, *, b): ...
                  
                  f(1, b=2)  # fine
                  f(1, 2)    # error: b is keyword-only
                  

                  Python3.8 added positional-only parameters, meaning parameters that cannot be used as keyword arguments. They appear before a / (a pun on * preceding keyword-only args).

                  def f(a, /, p, *, k): ...
                  
                  f(  1,   2, k=3)  # fine
                  f(  1, p=2, k=3)  # fine
                  f(a=1, p=2, k=3)  # error: a is positional-only
                  

                  这篇关于Python 中的 *tuple 和 **dict 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么我们需要 Python 中的元组(或任何不可变数据类型)? 下一篇:元组到 DataFrame 转换的列表

                  相关文章

                  <tfoot id='PW1Dr'></tfoot>

                  <i id='PW1Dr'><tr id='PW1Dr'><dt id='PW1Dr'><q id='PW1Dr'><span id='PW1Dr'><b id='PW1Dr'><form id='PW1Dr'><ins id='PW1Dr'></ins><ul id='PW1Dr'></ul><sub id='PW1Dr'></sub></form><legend id='PW1Dr'></legend><bdo id='PW1Dr'><pre id='PW1Dr'><center id='PW1Dr'></center></pre></bdo></b><th id='PW1Dr'></th></span></q></dt></tr></i><div id='PW1Dr'><tfoot id='PW1Dr'></tfoot><dl id='PW1Dr'><fieldset id='PW1Dr'></fieldset></dl></div>
                    <bdo id='PW1Dr'></bdo><ul id='PW1Dr'></ul>
                  1. <small id='PW1Dr'></small><noframes id='PW1Dr'>

                      <legend id='PW1Dr'><style id='PW1Dr'><dir id='PW1Dr'><q id='PW1Dr'></q></dir></style></legend>