设为首页 加入收藏

TOP

06python序列(一)
2023-07-25 21:24:18 】 浏览:56
Tags:06python 序列

数据结构是Python中一个很重要的概念,是以某种方式(如通过编号)组合起来的数据元素(如数字、字符乃至其他数据结构)的集合。

在Python中,最基本的数据结构是序列(sequence)。

  • 序列中的每个元素都有编号,及其位置或索引,其中的第一个元素的索引为0,第二个元素位的索引为1,依此类推
  • 在有些编程语言中,从1开始给序列中的元素编号,但从0开始指出相对于序列开头的偏移量
  • 同时可以回绕到序列末尾,用负索引表示序列末尾元素的位置,-1表示序列最后一个元素的索引,-2表示序列倒数第二个元素的索引...

Python内置了多种序列:

  • 列表
  • 元组
  • 字符串

列表和元组的主要不同在于,列表是可以修改的,元组不可以修改。
列表适用于需要中途添加元素的情形,而元组适用于出于某种考虑需要禁止修改序列的情形。

在需要处理一系列值时,序列很有用。
例如在数据库中,可以使用序列来表示人,第一个元素为人名,第二个元素为年龄...
序列中还可以包含其他序列,因此可以创建一个由数据库中所保存的人员组成的列表

>>> 
>>> edward = ['Edward Gumby', 42, 'male']
>>> john = ['John Smith', 50, 'female']
>>> database = [edward, john]
>>> database
[['Edward Gumby', 42, 'male'], ['John Smith', 50, 'female']]
>>>

Python支持一种数据结构的基本概念,名为容器(container):

  • 容器基本上就是可包含其他对象的对象
  • 两种主要的容器是序列(如列表和元组)和映射(字典)。在序列中,每个元素都有编号;而在映射中,每个元素都有名称(也叫键)
  • 有一种既不是序列也不是映射的容器,叫做集合(set)

有几种操作适用于序列:

  • 索引
  • 切片
  • 相加
  • 相乘
  • 成员资格检查
  • python内置函数确定序列的长度及找出序列中最大元素和最小元素
  • 迭代

索引

字符串就是有字符组成的索引,索引0指向第一个元素。不同于其他一些语言,Python没有专门用于表示字符的类型,因此一个字符就是只包含一个元素的字符串。
对于字符串字面量(以及其他的序列字面量), 可以直接对索引进行操作,无需将其赋值给变量。

序列中的所有元素都有编号,从0开始递增,依次下去,称为索引(index)。可以通过索引来获取元素,这种方式适用于所有的序列。
负数索引将从右(即最后一个元素)开始往左数,因此-1是最后一个元素。

如果调用函数返回一个序列,可以直接对其执行索引操作

>>> fourth = input('Year: ')[3]
Year: 2022
>>> fourth           
'2'
>>> 
>>> greenting = 'Hello'
>>> greenting[0]   
'H'
>>> greenting[1] 
'e'
>>> 
>>> 'Hello'[-2] 
'l'
>>>

eg.输入年、月(数1-12)、日(数1-31),再使用相应的月份名称将日期打印出来

# 将以数指定年、月、日的日期打印出来

months = [
  'January',
  'February',
  'March',
  'April',
  'May',
  'June',
  'July',
  'Auguest',
  'September',
  'October',
  'November',
  'December'  
]

# 一个列表,其中包含数 1-31 对应的结尾
endings = ['st', 'nd', 'rd'] + 17 * ['th'] + ['st', 'nd', 'rd'] + 7 * ['th'] + ['st']
'''
分日英语:
日 全称 缩写
1日 first 1st
2日 second 2nd
3日 third 3rd
4日 fourth 4th
5日 fifth 5th
6日 sixth 6th
7日 seventh 7th
8日 eighth 8th
9日 ninth 9th
10日 tenth 10th
11日 eleventh 11th
12日 twelfth 12th
13日 thirteenth 13th
14日 fourteenth 14th
15日 fifteenth 15th
16日 sixteenth 16th
17日 seventeenth 17th
18日 eighteenth 18th
19日 nineteenth 19th
20日 twentieth 20th
21日 twenty-first 21st
22日 twenty-second 22nd
23日 twenty-third 23rd
24日 twenty-fourth 24th
25日 twenty-fifth 25th
26日 twenty-sixth 26th
27日 twenty-seventh 27th
28日 twenty-eighth 28th
29日 twenty-ninth 29th
30日 thirtieth 30th
31日 thirty-first 31st
'''

year = input('Year: ')
month = input('Month(1-12): ')
day = input('Day(1-31): ')

month_number = int(month)
day_number = int(day)

# 别忘了将表示月和日的数减1,这样才能得到正确的索引
month_name = months[month_number-1]
ordinal = day + endings[day_number-1]

print(month_name + ' ' + ordinal + ' ' + year)

# output
Year: 2022
Month(1-12): 11
Day(1-31): 14 
November 14td 2022

切片

除使用索引来访问单个元素外,还可以使用切片(slicing)来访问特定范围内的元元素。使用两个索引,并用冒号分隔.

切片适用于提取序列的一部分,其中的编号非常重要:第一个索引是包含第一个元素的编号,但第二个索引是切片后余下的第一个元素的编号。
简言之,切片中提供的两个索引来确定边界,包含第一个索引的元素,不包含第二个索引的元素。

>>> 
>>> tag = '<a href="http://www.python.org">Python web site</a>'
>>> tag[9:30]                                             
'http://www.python.org'
>>> tag[32:-4]                                            
'Python web site'
>>>
>>> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
>>> numbers[3:6]
[4, 5, 6]
>>> numbers[0:1] 
[1]
>>>

访问数字列表的最后三个元素:

>
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇钩子函数 下一篇Python绘制神经网络模型图

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目