python学习笔记#2元组和列表
前言
学习python的复杂数据类型,tuple和list的区别和其部分功能的使用。
补充:string需要到包含引号的使用。
一、string包含引号
举例:
str_var1 = '"666"'
print(str_var1, type(str_var1))
str_var2 = "I'm,student"
print(str_var2, type(str_var2))
结果如下:
"666" <class 'str'>
I'm,student <class 'str'>
即当字符串含有引号时应该尽量不要使用相同类型防止冲突。
二、复杂数据类型
1.序列
序列:sequence
储存时,对值有下标供访问。正下标从左往右从0开始,负下标从右往左从-1开始。
str_data = "1234567890"
print(str_data[4])
print(str_data[-6])
两者访问到的值均为 5
2.tuple:元组
也是一种序列的形式:可以存储不同类型的数据。定义的形式:(元素1, 元素2, 元素3) ,另一种定义形式tuple()
tuple_var = (1, 1.1, 1+2j, "123", b'', True, None, tuple())
print(tuple_var, type(tuple_var))
结果如下:
(1, 1.1, (1+2j), '123', b'', True, None, ()) <class 'tuple'>
元素不仅包括7种基本数据类型,也包括tuple本身
获得函数的帮助可使用ctrl+鼠标左键或者help()以及 python官方文档
help(tuple)
可以得到使用方法,解释和可用方法
class tuple(object)
| tuple(iterable=(), /) -- iterable 可迭代的 即多元素,/表示其之前的参数不能使用参数名 = 这种方式传值,其本身不是一个参数,只用来限制它前面参数传参的形式
| Built-in immutable sequence. 元组是一个不可变的序列即无法给元组赋值
...
count(self, value, /) -- 计数
| Return number of occurrences of value.
|
index(self, value, start=0, stop=9223372036854775807, /) -- 索引,默认从0开始索引到最后(不超过最大值)返回找到的第一个值的下标
| Return first index of value.
| Raises ValueError if the value is not present. -- 无法找到不存在的值,会报错
使用方式如下
tuple_var1 = tuple("123332")
print(tuple_var1.count('3'))
print(tuple_var1.index('3'))
可以得到返回值计数为3,索引为下标2
2.list:列表
也是一种序列,可以存储不同类型的数据。定义的形式: [元素1, 元素2, 元素3],另一种定义形式list[]
list_var = [1, 1.1, 1+2j, "123", b'', True, None, (1, 2, 3), [1, 2, 3]]
print(list_var, type(list_var))
元素除7种基本数据类型,也包括tuple和list本身
help(list)
class list(object)
| list(iterable=(), /)
| list为可变的序列,也就是可以被赋值
| Built-in mutable sequence.
list列表可使用的方法有11种,以 list_var1 = list(“123”)举例,直接打印可以得到
[‘1’, ‘2’, ‘3’]
append(self, object, /) # 追加,在原列表末尾增加一个值
| Append object to the end of the list.
list_var1 = list("123")
list_var1.append("456")
print(list_var1)
可以得到[‘1’, ‘2’, ‘3’, ‘456’]
clear(self, /) # 清空
| Remove all items from list.
list_var1.clear()
copy(self, /) # 浅拷贝
| Return a shallow copy of the list.
浅拷贝创建新的对象指向相同的地址也就是拷贝了一份指针。如果原地址内存中的内容发生改变,那么浅拷贝的内容也会发送改变。
| count(self, value, /) # 计数
| Return number of occurrences of value.
list_var1.count('3')
| extend(self, iterable, /) # 延申,和追加类似但结果不同
| Extend list by appending elements from the iterable.
list_var1.extend("456")
print(list_var1)
可以得到[‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’]
| index(self, value, start=0, stop=9223372036854775807, /) # 索引,默认从0开始索引到最后(不超过最大值)返回找到的第一个值的下标
| Return first index of value.
| Raises ValueError if the value is not present.# 无法找到不存在的值,会报错
| insert(self, index, object, /) # 插入,必须要输入插入的下标位置
| Insert object before index.
list_var1.insert(0, '0')
print(list_var1)
可以得到[‘0’, ‘1’, ‘2’, ‘3’]
| pop(self, index=-1, /) # 弹出,有返回值,返回弹出的值,默认弹出后进的值
| Remove and return item at index (default last).
|
| Raises IndexError if list is empty or index is out of range.
return_value = list_var1.pop(1)
print(return_value)
print(list_var1)
可以得到2 [‘1’, ‘3’]
| remove(self, value, /) # 删除遇见的第一个值()
| Remove first occurrence of value.
|
| Raises ValueError if the value is not present.
| reverse(self, /) # 倒序1,3,2->2,3,1
| Reverse *IN PLACE*.
|
| sort(self, /, *, key=None, reverse=False) # 默认排序(升序)1,3,2,4->1,2,3,4,*之后的参数必须以参数名 = 的方式传输
| Sort the list in ascending order and return None.
|
| The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
| order of two equal elements is maintained).
|
| If a key function is given, apply it once to each list item and sort them,
| ascending or descending, according to their function values.
|
| The reverse flag can be set to sort in descending order.
总结
tuple和list的区别在于tuple元组不可变不可被赋值,而list列表可以被赋值。本文仅仅简单介绍了对应函数的使用,因为list可变,所以可通过直接转化的方式。至此,我们总共学习了9种数据基本类型,其中是序列的有字节型b’',字符串string,元组tuple,列表list共四种。
这里主要是需要学会如何利用帮助读懂并学会使用各个数据类型的方法。
(本文为初学者学习笔记,若有错误之处,感谢大佬指正!)