Python的学习

来源:互联网 发布:李昌钰 辛普森 知乎 编辑:程序博客网 时间:2024/06/08 04:44

关于内置函数reverse()及reversed()

reverse():所有元素原地逆序

reversed():不对原表做任何修改,只是返回一个逆序排列后的迭代对象


list('abcdef'):['a', 'b', 'c', 'd', 'e', 'f']  将序列转换成列表

print (tuple('abcdefg'))
('a', 'b', 'c', 'd', 'e', 'f', 'g') 将序列转换成元组


元组:不可变序列

列表:可变序列


zip(列表1,列表2,...):将多个元组或列表对应位置的元素泽河为元组,并返回包含这些元组的列表(Python 2.x)或zip对象(Python 3.x)

aList = [1,2,3]bList = [4,5,6]cList = [7,8,9]dList = zip(aList,bList,cList)print dList
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]


locals()

globals()

a = (1,2,3,4,5)b = 'Hello world'def demo():    a = 3    b = [1,2,3]    #locals()返回当前作用域所有局部变量和值的字典    print('locals:',locals())    #globals()返回当前作用域内所有全局变量和值的字典    print('globals:',globals())demo()
('locals:', {'a': 3, 'b': [1, 2, 3]})
('globals:', {'a': (1, 2, 3, 4, 5), 'b': 'Hello world', '__builtins__': <module '__builtin__' (built-in)>, 'dList': [(1, 4, 7), (2, 5, 8), (3, 6, 9)], '__file__': 'E:/PycharmProjects/Project2/listFile.py', 'aList': [1, 2, 3], '__package__': None, 'cList': [7, 8, 9], 'bList': [4, 5, 6], 'demo': <function demo at 0x016ED830>, '__name__': '__main__', 'filename': 'timeCom.py', 'os': <module 'os' from 'E:\Python\lib\os.pyc'>, '__doc__': None})

0 0
原创粉丝点击