4_dictionary&tuple

dictionary

*

用于指定函数传入参数的类型。*用于参数前面,表示传入的多个参数将按照元组的形式存储,而*的作用是将一个可迭代的变量全部拆开,例如:

a = (1,2,)
print(*a)

"""
1 2
"""

在参数传递中的用法: 

def t1(param1, *param2):
        print(param1)
        print(param2)
t1(1,2,3,4)

# 1
# (2,3,4)

此外,*还可以解压参数列表:

def t3(p1, p2):
        print(p1, p2)
args = [1, 2]
t3(*args)
​
# 1 2

**

用于参数前则表示传入的多个参数将按照字典的形式存储,是一个字典。

def t2(param1, **param2):
        print param1
        print param2
t2(1,a=2,b=3)
​
# 1
# {a:2, b:3}

dict()函数

用于创建一个字典,字典的key必须是不可变的(元组也可以作为key值)

class dict(**kwarg)
#关键字

class dict(mapping, **kwarg)
#映射+关键字
#映射:例如字典,zip等

class dict(iterable, **kwarg)
#可迭代对象+关键字
#可迭代对象:如列表,元组等

在字典中,有多个成员的键相同,但是键对应的值不同的情况,在这种情况下,后来赋给键的值将成为键的真实值,例如:

a={'x':1,'y':2}
b={'y':3,'z':4}
c=dict(a,**b)
print(c)

#{'x':1,'y':3,'z':4}

in函数

默认检索key,加入.values()在value中检索。

x={'one':'apple','two':'banana','three':'orange'}

'one' in x
#True

'uno' in x
#False

'orange' in  x.values()
#True

 dir()函数

dir(对象),查看该对象可以使用什么方法;dir(模块),查看模块内的函数

import math
print(dir(math))

"""
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']
"""

help()函数

查询某类型的方法help(type.method),例如:

help(dict.get)

"""
Help on method_descriptor:

get(self, key, default=None, /)
    Return the value for key if key is in the dictionary, else default.
"""

.get()函数

返回key对应的value,若不存在,返回指定值(默认None)

counts = {'chuck':1, 'annie':42, 'jan':100}

print(counts.get('jan',0))
#100

print(counts.get('hi',1))
#1

.items()函数

返回可遍历的(key,value)元组数组。

favorite = {
    'a':'b',
    'c':'d',
    'e':'f',
    }
for z in favorite.items():
    print(z)

"""
('a', 'b')
('c', 'd')
('e', 'f')
"""

.values(),.keys()函数

返回一个视图对象,提供了字典实体的动态视图,这就意味着字典改变,视图也会跟着变化。

视图对象不是列表,不支持索引,可以使用 list() 来转换为列表。

我们不能对视图对象进行任何的修改,因为字典的视图对象都是只读的。

favorite = {
    'a':'b',
    'c':'d',
    'e':'f',
    }
vls = favorite.values()
print(vls)
del favorite['a']
print(vls)

"""
dict_values(['b', 'd', 'f'])
dict_values(['d', 'f'])
"""

tuple

元组是不可变的,用()表示:

t1 = ('a')
print(type(t1))

t2 = ('a',)
print(type(t2))

"""
<class 'str'>
<class 'tuple'>
"""

但是元组可以被另一个元组所替代,例如:

t = ('a','b','c','d','e')
print(t)
t = ('A',) + t[1:]
print(t)

"""
('a', 'b', 'c', 'd', 'e')
('A', 'b', 'c', 'd', 'e')
"""

多重赋值与交换

m = ('a', 'b')
x,y = m
print(x,y)
x,y = y,x
print(x,y)

"""
a b
b a
"""

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值