python之数据结构的那些事儿

python之数据结构的那些事儿

Reference: Kaggle Notebook Lists! Click me!

本节主要学习python中最基础的数据结构 - lists 和 tuples

1. Lists(数列)

Lists是指一组有序的元素;

例子:

primes = [3, 7, 12, 4]

但Lists中不是只可以放数字,还可以放字符串;

例子:

planets = [‘Mercury’, ‘Venus’, ‘Earth’, ‘Mars’, ‘Jupiter’, ‘Saturn’, ‘Uranus’, ‘Neptune’]

当然还可以列出一个列表:

example = [
[‘3’, ‘5’, ‘23’],
[‘a’, ‘b’, ‘c’]
]

另一种写法:

example = [ [‘3’, ‘5’, ‘23’], [‘a’, ‘b’, ‘c’] ]

很明显,第一种写法更加清晰明了。

但是一个list里面也可以存在不同的数据类型哦:

look_at_me = [13, ‘hey’, help]
#有int、str,还可以是function

2. Indexing(索引)

Indexing是指可以使用方括号访问Lists中的各个元素。

看看例子应该就懂了:

>>> example[0]
['3', '5', '23']

>>> example[1]
['a', 'b', 'c']

但有一个很神奇的事情是:

>>> example[-1]
['a', 'b', 'c']

>>> example[-2]
['3', '5', '23']

竟然还有-1???
仔细发现,其实是有规律的;数列[0]是第一个值,数列[-1]是最后一个值了,以此类推,数列[-2]就是倒数第二个值。

3. Slicing(切片)

切片,换言之就是截取数列的某些元素

例子:

>>> example = [1, 2, 3, 4, 5]
>>> example[0:3] #截取[0]到[3]的元素,start = [0], end = [3]
[1, 2, 3]

注意:example[0:3]是截取索引为0-3的元素,但不包括索引3;

其他用法:

>>> example[:3] 
[1, 2, 3]

解析:如果去除起始索引,则会默认起始索引为0;

>>> example[2:] 
[3, 4, 5]

解析:如果去掉结束索引,则会默认截取到数列的最后一个元素;

还有其他有趣的表达:

>>> example[1:-1] #截取除了第一个和最后一个元素
[2, 3, 4]
>>> example[-3:]#截取最后三位
[3, 4, 5]

4. Changing Lists(更改数列)

数列并不是固定的,也就是说可以通过某些方式进行修改哦;

例子:

>>> example = [1, 2, 3, 4, 5]
>>> example[2] = 12
>>> example
[1, 2, 12, 4, 5]

解析:可以通过给索引赋值,改变改索引下的值;

另一个例子:

>>> example = [1, 2, 3, 4, 5]
>>> example[:3] = ['a', 'b', 'c']
['a', 'b', 'c', 4, 5]

解析:也可以通过切片的方式,对数列进行修改;

5.与数列有关的函数(List Function)

a. len() 函数
len函数可以返回数列的长度

>>> example = [1, 2, 3, 4, 5]
>>> len(example)
5

b. sorted() 函数
sorted函数可以返回正确排序的数列(按数字大小排序或者按字母顺序排序,但如果数列中有2种或以上的数据类型,则不能调用此函数)

>>> example = [102, 2, 13, 44, 5]
>>> sorted(example)
[2, 5, 13, 44, 102]

c. sum() 函数
sum函数可以返回数列中数值的总和(仅适用于数值)

>>> example = [102, 2, 13, 44, 5]
>>> sum(example)
166

d. max() 最大值、min() 最小值

>>> example = [102, 2, 13, 44, 5]
>>> max(example)
102
>>> min(example)
2

6. Objects(对象)

随身携带着一些东西的物体,称为对象;
在python中,可以用点 “.“去访问该对象所携带的内容。

例子:

>>> x = 13
>>> x.imag # .imag用于查看虚部
0
>>> y = 13 + 3j
>>> y.imag
3.0

imag属于是数字的属性,因此可以直接调用;

但是,对象所携带的东西除了属性,还有函数;而这类型的函数称为method;

先看个例子:

>>> x = 12
>>> x.bit_length() # x.bit_length()返回该数值的字节长度
4

注意:因为bit_length是一种method,因此需要在最后加小括号,也就是bit_length();

如果碰到一个新函数,但是不知道怎么用的时候,别忘了help函数哦

7. List Method

a. list.append
这个method可用于在数列的最后加一个元素;

>>> example = [1, 2, 3, 4, 5]
>>> example.append(6)
>>> example
[1, 2, 3, 4, 5, 6]

注意:append这个是method哦,这个名称仅在数列中存在;因此在使用help函数的时候,不能只查看append - help(append);正确的方式是help(list.append); 而像max()、len()这些属于内置函数,可以直接调用help函数查看该用法。

b. list.pop
此method用于去除并返回数列中最后一位元素;

>>> example = [1, 2, 3, 4, 5]
>>> example.pop()
5
>>> example
[1, 2, 3, 4]

c. list.index
此method可返回某元素的索引号;

>>> example = [1, 2, 3, 4, 5]
>>> example.index(2)
1

d. in
此method可用于确定该元素是否在数列中存在

>>> 1 in example
True
>>> 10 in example
False

除了以上列举的method,还有很多很多。想知道更多的用法?可以用help()去查询哦。

用得最多的method有append、clear、copy等。

8. Tuples(元祖)

Tuple其实和list差不多,下面会列举他们的不同点:

a. 创建list的时候,用的是中括号;但是创建tuple的时候,用的是小括号:

t = (1, 2, 3)

b. tuple不能被篡改

tuple还有很多有趣的函数哦

例如

>>> x = 0.125
>>> x.as_integer_ratio()
(1, 8)

as_integer_ratio() 可用于将浮点数转换为分数,以tuple的形式返回分子和分母;
我们来检验一下吧

>>> numerator. denominator = x.as_integer_ratio()
>>> numerator/denominator
0.125

最后来个funny code吧~

a = 1
b = 0
a, b = b, a
print(a, b)

输出是啥呢?

hh拜

原创文章 8 获赞 17 访问量 254

猜你喜欢

转载自blog.csdn.net/weixin_46995523/article/details/105873993