不同数据类型在内存中的存址方式
我们还记得,我们学字符串,学列表的时候:字符串的方法,字符串的功能一般都是在生成一个新的值;而列表一般都是本身进行了一个变化;那这是为什么呢?
内存是什么?
- 一个格子,一个格子,可以存东西的地址;
那我们跟着jacky的思路来想,字符串跟列表是怎样在内存中存放的,如果我们来设计这样一套规则应该怎样来设计?
- 拿列表举例
li = [55,66]
列表有插入,删除等等的功能,那我们列表有append功能是吧,那我们给列表插入元素的时候是不是要在内存中给列表预留一些小格子,那我们怎么预留?直接在,55,66,后连续预留可以吗?是不是不可以,这太浪费空间了,再说列表插入多少元素也不可预知;那我们应该怎样设计?
字符串:一次性创建,不能被修改;如果要修改,只能重新创建;
列表:列表的底层有链表,可以记录下一个元素的位置,也可以记录上一个元素的位置