Python列表一个星号,字典dict前边跟两个星号是什么意思?

在这里插入图片描述

Python中,单星号 * 和双星号 ** 除了作为“乘”和“乘方”的数值运算符外,还在列表、元组、字典的操作中有看到,下面对其进行解释:

单星号 *

单星号 * 用于对列表LIST或元组tuple中的元素进行取出(unpacke)。例如,np.arange函数需要独立的开始和停止参数:

import numpy as np

print(np.arange(3,6))

输出:

[3, 4, 5]

采用 * 可将列表或元祖中的元素直接取出,作为arange的上下限:

import numpy as np

LIST = [3, 6]

print(np.arange(*LIST))

输出:

[3, 4, 5]

双星号 **

双星号 ** 可将字典里的“值”取出

def parrot(voltage, state=‘a stiff’, action=‘voom’):

… print “-- This parrot wouldn’t”, action,

… print “if you put”, voltage, “volts through it.”,

… print “E’s”, state, “!”

…

d = {“voltage”: “four million”, “state”: “bleedin’ demised”, “action”: “VOOM”}

parrot(**d)

输出:

– This parrot wouldn’t VOOM if you put four million volts through

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值