python数据结构学习

1,列表

列表是python的一种可以更改数据内容的数据类型,它是由一系列的元素组成的序列。在其他的语言中,相似的功能称为数组,但是python的列表功能可以存储相同的数据类型外,还可以保存为不用的数据类型,这些数据类型包括浮点数,字符串,整数以及其他不同的互数据结构。

1.1,列表的定义

name_list = [element1 ,element2,element3.....elementn]

列表中的元素使用进行分开,如果需要打印列表中内容可以使用print()函数,将列表的名称当做变量的名称使用即可。

james = [23,19,22,31,18]
fruits = ["apple", "orange", "banana"]
# 输出对list
print("james list output is:",james)
print("fruits list output is:",fruits)

针对list的输出结果为:

james list output is: [23, 19, 22, 31, 18]
fruits list output is: ['apple', 'orange', 'banana']

1.2,列表数据的读取

可以根据列表的名称和索引读取列表元素的内容,在Python中元素的读取是从0开始的,第二值为1,依次进行类推

james = [23,19,22,31,18]
# fruits = ["apple", "orange", "banana"]
# # 输出对list
# print("james list output is:",james)
# print("fruits list output is:",fruits)

print("the james 1th value is:",james[0])
print("the james 2th value is:",james[1])
print("the james 3th value is:",james[2])
print("the james 4th value is:",james[3])
print("the james 5th value is:",james[4])

针对list的数据读取:

the james 1th value is: 23
the james 2th value is: 19
the james 3th value is: 22
the james 4th value is: 31
the james 5th value is: 18

1.3,列表的切片

在程序的设计的时候需要的取得列表前几个元素,后几个元素,获取区间的元素或者是依照一定规律排序的元素,所取得的切片可以称为的子列表..

name_list[start:end] #读取从索引start 到 end-1的所有的元素

name_list[:n] #获取列表的前n个元素

name_list[n:] #从n到最后的所有元素

name_list[-n:] #获取后n个元素

name_list[:] #获取所有的元素

name_list[start:end:sep] #每隔step,读取从start 到 end-1的所有的元素

1.4,列表的索引值是-1

如果索引值是-1,则代表元素的最后一个值

james = [23,19,22,31,18]
fruits = ["apple", "orange", "banana"]
# 输出对list
# print("james list output is:",james)
# print("fruits list output is:",fruits)

print("the james -1th value is:",james[-1])
print("the fruits -1th value is:",fruits[-1])

输出的结果为

the james -1th value is: 18
the fruits -1th value is: banana

1.5,列表统计资料,统计列表中的最大值,最小值以及总和

james = [23,19,22,31,18]
# fruits = ["apple", "orange", "banana"]
# 输出对list
# print("james list output is:",james)
# print("fruits list output is:",fruits)

print("the james max value:",max(james))
print("the james min value:",min(james))
print("the james sum value:",sum(james))

对应的输出结果为:

the james max value: 31
the james min value: 18
the james sum value: 113

1.6,列表元素的个数len()

列表在使用的过程中有时候会对列表中的元素进行对应的操作,例如对元素进行增加或者对元素进行删除,因此为了了解列表中元素的个数可以通过len()函数获取列表元素的数量

james = [23,19,22,31,18]
fruits = ["apple", "orange", "banana"]
# 输出对list
# print("james list output is:",james)
# print("fruits list output is:",fruits)

print("the james size is:",len(james))
print("the james size is:",len(fruits))

对应的输出结果为:

the james size is: 5
the james size is: 3

1.7,列表的相加

Python运行列表的元素进行相加,相当于列表的结合

james = [23,19,22,31,18]
fruits = ["apple", "orange", "banana"]
# 输出对list
# print("james list output is:",james)
# print("fruits list output is:",fruits)

print("the james and fruit sum is:",james+fruits)

对应的输出结果为:

the james and fruit sum is: [23, 19, 22, 31, 18, 'apple', 'orange', 'banana']

1.8,更改列表的元素值

james = [23,19,22,31,18]
fruits = ["apple", "orange", "banana"]
# 输出对list
print("james list output is:",james)
# 改变列表的元素的最后一个值为14
james[-1] = 14
print("james list output is:",james)

对应的输出结果为:

james list output is: [23, 19, 22, 31, 18]
james list output is: [23, 19, 22, 31, 14]

1.9,列表和数字相乘

如何列表乘以数字,这个数字表示列表的重复次数

james = [23,19,22,31,18]
fruits = ["apple", "orange", "banana"]
# 输出对list
print("james list output is:",james)
james_list = james*3;
print("james list output is:",james_list)

输出的结果为:

james list output is: [23, 19, 22, 31, 18]
james list output is: [23, 19, 22, 31, 18, 23, 19, 22, 31, 18, 23, 19, 22, 31, 18]

1.10,删除列表元素

del name_list[i] 删除索引为i的元素
del name_list[start:end]删除索引为start到end-1的元素
del name_list[start:end:sep] 每隔sep删除索引为start到end-1的元素

1.11,列表元素的加法运作 

既然我们可以读取列表的内容,就可以使用相同的观念进行列表内元素的加法操作

james = ["apple",23,19,22,31,18]
jamess = ["orange",23,19,22,31,18]
fruits = ["apple", "orange", "banana"]
# 输出对list
# print("james list output is:",james)
# james_list = james*3;
# print("james list output is:",james_list)

print(james[0]+jamess[0],james[1]+jamess[1])

输出结果为:

appleorange 46

1.12,列表为空的判断

如果想建立一个列表,列表的元素为空,可以如下的方式进行创建

name_list = []

 可以通过len(name_list)判断列表是否为空

1.13,删除列表

当不想使用列表的时候可以使用如下方法进行删除列表

del name_list

1.14,列表转化

将tuple和str类型装换为列表数据类型

aTuple = (123, 'Google', 'Runoob', 'Taobao')
list1 = list(aTuple)
print ("列表元素 : ", list1)

str="Hello World"
list2=list(str)
print ("列表元素 : ", list2)

 对应的输出结果为:

列表元素 :  [123, 'Google', 'Runoob', 'Taobao']
列表元素 :  ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']

2,Python中常见的字符串操作方法

lower( ):将字符串转成小写字。 
 

upper( ):将字符串转成大写字。 
 

title( ):将字符串转成第一个字母大写,其他是小写。

 
rstrip( ):删除字符串尾端多余的空白。 


lstrip( ):删除字符串开始端多余的空白。 


strip( ):删除字符串头尾两边多余的空白。

find(str, beg=0, end=len(string))

检测 str 是否包含在字符串中如果指定范围 beg 和 end ,则检查是否包含在指定范围内,如果包含返回开始的索引值,否则返回-1


endswith(suffix, beg=0, end=len(string))

检查字符串是否以 suffix 结束,如果 beg 或者 end 指定则检查指定的范围内是否以 suffix 结束,如果是,返回 True,否则返回 False。

replace(old, new [, max])
把将字符串中的 old 替换成 new,如果 max 指定,则替换不超过 max 次

startswith(substr, beg=0,end=len(string))

检查字符串是否是以指定子字符串 substr 开头,是则返回 True,否则返回 False。如果beg 和 end 指定值,则在指定范围内检查。

join(seq)

用于将序列中的元素以指定的字符连接生成一个新的字符串

3,list数据类型

3.1,List append()方法

append() 方法用于在列表末尾添加新的对象。

list1 = ['Google', 'Runoob', 'Taobao']
list1.append('Baidu')
print ("更新后的列表 : ", list1)

对应的输出结果为:

更新后的列表 :  ['Google', 'Runoob', 'Taobao', 'Baidu']

3.2,list.pop([index=-1])方法

移除列表中的一个元素(默认最后一个元素),并且返回该元素的值

list1 = ['Google', 'Runoob', 'Taobao']
list1.pop()
print ("列表现在为 : ", list1)
list1.pop(1)
print ("列表现在为 : ", list1)

对应的输出结果为:

列表现在为 :  ['Google', 'Runoob']
列表现在为 :  ['Google']

3.3,list.count()方法

count() 方法用于统计某个元素在列表中出现的次数。返回元素在列表中出现的次数。

aList = [123, 'Google', 'Runoob', 'Taobao', 123];

print ("123 元素个数 : ", aList.count(123))
print ("Runoob 元素个数 : ", aList.count('Runoob'))

对应的输出结果为:

123 元素个数 :  2
Runoob 元素个数 :  1

3.4,list.count()方法

extend() 函数用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)。

list1 = ['Google', 'Runoob', 'Taobao']
list2=list(range(5)) # 创建 0-4 的列表
list1.extend(list2)  # 扩展列表
print ("扩展后的列表:", list1)

对应的输出结果为:

扩展后的列表: ['Google', 'Runoob', 'Taobao', 0, 1, 2, 3, 4]

3.5,list.index()方法

index() 函数用于从列表中找出某个值第一个匹配项的索引位置。该方法返回查找对象的索引位置,如果没有找到对象则抛出异常。

list1 = ['Google', 'Runoob', 'Taobao']
print ('Runoob 索引值为', list1.index('Runoob'))
print ('Taobao 索引值为', list1.index('Taobao'))

对应的输出结果为:

Runoob 索引值为 1
Taobao 索引值为 2

3.6,list.insert()方法

insert() 函数用于将指定对象插入列表的指定位置。该方法没有返回值,但会在列表指定位置插入对象。

list1 = ['Google', 'Runoob', 'Taobao']
list1.insert(1, 'Baidu')
print ('列表插入元素后为 : ', list1)

对应的输出结果为:

列表插入元素后为 :  ['Google', 'Baidu', 'Runoob', 'Taobao']

3.7,list.remove()方法

remove() 函数用于移除列表中某个值的第一个匹配项。

list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']
list1.remove('Taobao')
print ("列表现在为 : ", list1)
list1.remove('Baidu')
print ("列表现在为 : ", list1)

对应的输出结果为:

列表现在为 :  ['Google', 'Runoob', 'Baidu']
列表现在为 :  ['Google', 'Runoob']

3.8,list.reverse()方法

reverse() 函数用于反向列表中元素。

list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']
list1.reverse()
print ("列表反转后: ", list1)

对应的输出结果为:

列表反转后:  ['Baidu', 'Taobao', 'Runoob', 'Google']

3.9,list.sort()方法

sort() 函数用于对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数。

  • key -- 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
  • reverse -- 排序规则,reverse = True 降序, reverse = False 升序(默认)。
aList = ['Google', 'Runoob', 'Taobao', 'Facebook']
 
aList.sort()
print ( "List : ", aList)

对应的输出结果为:

List :  ['Facebook', 'Google', 'Runoob', 'Taobao']

3.10,list.clear()方法

list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']
list1.clear()
print ("列表清空后 : ", list1)

3.11,list.copy()方法

list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']
list2 = list1.copy()
print ("list2 列表: ", list2)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值