一、列表的定义
1. 创建列表
>>> name = [‘gao‘, ‘sam‘, ‘snow‘, ‘jey‘] >>> type(name) <type ‘list‘> >>> name=list([‘gao‘, ‘sam‘, ‘snow‘, ‘jey‘]) # python 3.0+ 会报错 >>> type(name) <type ‘list‘> >>> name = [] >>> type(name) <type ‘list‘> >>> name = list() # python 3.0+ 会报错 >>> type(name) <type ‘list‘>
2. 切片 与字符串类似
>>> name = [‘Sam‘, ‘Jey‘, ‘Snow‘, ‘Amy‘] >>> name[2] ‘Snow‘ >>> name[1:] [‘Jey‘, ‘Snow‘, ‘Amy‘] >>> name[-2:] [‘Snow‘, ‘Amy‘] >>> name[-1:-3:-1] [‘Amy‘, ‘Snow‘]
时间: 2024-10-31 05:04:47