Last active
September 7, 2019 13:35
-
-
Save caoya171193579/5f9aac0b5fbf2b118737f4690519fa2e to your computer and use it in GitHub Desktop.
列表、元祖、字典
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
列表:list [] | |
>>>s = [213,2312,'aaa'] | |
>>>s | |
[213,2312,'aaa'] | |
>>>type(s) | |
list | |
遍历列表: 同字符串操作相同。 | |
>>>for i in s: | |
print(i) | |
213 | |
2312 | |
aaa | |
##### | |
In [99]: s = [231,23123,'aaa'] | |
In [100]: s | |
Out[100]: [231, 23123, 'aaa'] | |
In [101]: type(s) | |
Out[101]: list | |
In [102]: for i in s: | |
...: print(i) | |
...: | |
231 | |
23123 | |
aaa | |
In [103]: s[0] | |
Out[103]: 231 | |
In [104]: s[1] | |
Out[104]: 23123 | |
In [105]: s[2] | |
Out[105]: 'aaa' | |
In [106]: s[:2] | |
Out[106]: [231, 23123] | |
In [107]: s*3 | |
Out[107]: [231, 23123, 'aaa', 231, 23123, 'aaa', 231, 23123, 'aaa'] | |
In [108]: a = ['sss',"ddd",123] | |
In [109]: s+a | |
Out[109]: [231, 23123, 'aaa', 'sss', 'ddd', 123] | |
【列表的可变性】 | |
 | |
列表存于系统内的id()值是可以改变的,而字符串不能改变, 所有列表更能运用于现实环境中。 | |
【列表的使用方法;增删改查】 | |
1 list.append(obj) | |
在列表末尾添加新的对象 | |
2 list.count(obj) | |
统计某个元素在列表中出现的次数 | |
3 list.extend(seq) | |
在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表) | |
4 list.index(obj) | |
从列表中找出某个值第一个匹配项的索引位置 | |
5 list.insert(index, obj) | |
将对象插入列表 | |
6 list.pop([index=-1])(-2、-3、-4、-5) | |
移除列表中的一个元素(默认最后一个元素),并且弹出该元素的值 | |
7 list.remove(obj) | |
移除列表中某个值的第一个匹配项 | |
8 list.reverse() | |
反向列表中元素 | |
9 list.sort( key=None, reverse=False) | |
###sortad()系统自带排序函数,但是它只是排序完之后,展示出来一遍,而列表本身的元素未做改变,list.sort()排序也改变元素位置。 | |
加上(reverse=False)为正序(reverse=True)为反序 | |
>>>xs = [1,5,6,7,4,2,3,9,8] | |
>>>xs.sort(reverse=False) | |
>>>xs | |
[1,2,3,4,5,6,7,8,9] | |
>>>xs.sort(reverse=True) | |
>>>xs | |
[9,8,7,6,5,4,3,2,1] | |
对原列表进行排序,数字,英文都可从前往后排齐。 | |
10 list.clear() | |
清空列表 | |
11 list.copy() | |
复制列表 | |
 | |
【二元列表取值】取列表内的小列表中的值,用索引 | |
 | |
#列表解析 | |
1、sum : py内置函数,可以秒求列表内元素之和。 | |
 | |
使用列表解析更简单的求列表内元素之和: | |
 | |
但是做列表解析一般有以下建议是,不需要使用。 | |
 | |
dir(dict):快速查看字典的用法。 | |
##字典 dict{} key(键) value(值) 键值对 | |
字典是除列表以外最灵活的内置数据结构类型。 | |
区别在于字典当中的元素是通过键来存取,而不是通过偏移存取。 | |
dict = {key:value} | |
 | |
 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment