Python3元组、字典及函数

匿名 (未验证) 提交于 2019-12-02 22:51:30

Python3

Python

>>> tup2 = (1, 2, 3, 4, 5 );

>>> tup3 = "a","b", "c", "d"; #

>>> type(tup3) #

<class 'tuple'>

tup1 = ();

>>> tup1 = (50)

>>> type(tup1) #

<class 'int'>

>>> tup1 = (50,)

>>> type(tup1) #

<class 'tuple'>

len(tuple)

max(tuple)

min(tuple)

tuple(seq)

>>> list1= ['Google', 'Taobao','Runoob', 'Baidu']

>>> tuple1=tuple(list1)

>>> tuple1

('Google', 'Taobao', 'Runoob', 'Baidu')

"

>>> t = ('a', 'b', ['A', 'B'])

>>> t[2][0] = 'X'

>>> t[2][1] = 'Y'

>>> t

('a', 'b', ['X', 'Y'])

tuple

Python3

d = {key1 : value1, key2 :value2 }

d = {key1 : value1, key2 : value2 }

len(d)

keys()

values()

items():

1

2

str(dict)

>>> dict = {'Name': 'Runoob','Age': 7, 'Class': 'First'}

>>> str(dict)

"{'Name': 'Runoob', 'Class': 'First','Age': 7}"

type(variable)

radiansdict.clear()

radiansdict.copy()

radiansdict.fromkeys()

radiansdict.get(key, default=None)

key in dict

radiansdict.items()

radiansdict.keys()

radiansdict.setdefault(key, default=None)

radiansdict.update(dict2)

radiansdict.values()

pop(key[,default])

popitem()

Setset

Python3

1.

def

return XXX

2.

python

3.

def person(name,age,**kw):

print('name:',name,'age:',age,'other:',kw)

person('Frank','37')

person('Frank','37',city='Shanghai')

person('Frank','37',gender='M',job='Engineer')

extra = {'city': 'Beijing', 'job': 'Engineer'}

person('Jack', 24, **extra)

(

Python

>>> nums = [1, 2, 3]

>>> calc(*nums)

eg

def f1(a, b, c=0, *args, **kw):

print('a =', a, 'b =', b,'c =', c, 'args =', args, 'kw =', kw)

def f2(a, b, c=0, *, d, **kw):

print('a =', a, 'b =', b,'c =', c, 'd =', d, 'kw =', kw)

>>> args = (1, 2, 3, 4)

>>> kw = {'d': 99, 'x': '#'}

>>> f1(*args, **kw)

a = 1 b = 2 c = 3 args = () kw = {'d': 99, 'x': '#'}

>>> args = (1, 2, 3)

>>> kw = {'d': 88, 'x': '#'}

>>> f2(*args, **kw)

a = 1 b = 2 c = 3 d = 88 kw = {'x': '#'}

*args

**kw

4.

python

lambda [arg1 [,arg2,.....argn]]:expression

#

sum = lambda arg1, arg2: arg1 + arg2;

def XXX(arg1,arg2):

return arg1+arg2

sum=XXX(10,20)

#

print "

print "

def add(a,b,fun):

print(fun(a,b))

add(11,22,lambda arg1, arg2: arg1 - arg2)

5.

>>> foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]

>>> print filter(lambda x: x % 3 == 0, foo)

[18, 9, 24, 12, 27]

>>> print map(lambda x: x * 2 + 10, foo)

[14, 46, 28, 54, 44, 58, 26, 34, 64]

>>> print reduce(lambda x, y: x + y, foo)

139

pythonmap()

map()

pythonfilter():

li = [11, 22, 33]

new_list = filter(lambda arg: arg > 22, li)

s.strip(rm)

python

python

def myadd(x,y):

return x+y

sum=reduce(myadd,(1,2,3,4,5,6,7))

print sum

#

sum=reduce(lambdax,y:x+y,(1,2,3,4,5,6,7))

print sum

reduce()

reduce()

reduce(f, [1, 3, 5, 7, 9], 100)

6

python

Python

>>>sorted([36, 5, 12, 9, 21])

[5, 9, 12, 21, 36]

sorted()

sort()

shus.sort()

print(shus)

sorted()

print(sorted(shus))

print(shus)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!