列表元组字典与集合

Posted helloworld899

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了列表元组字典与集合相关的知识,希望对你有一定的参考价值。

[ 列表 ]

列表利用位置定位某一元素,列表是可变的,你可以直接对原始列表进行增、删、改,在列表中具有相同值的元素允许出现多次。

使用[]或list()创建列表:

列表可以由零个或多个元素组成,元素之间用逗号分开,整个列表被括号所包裹;

>>> empty_list = []
>>> weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
>>> weekdays
[‘Monday‘, ‘Tuesday‘, ‘Wednesday‘, ‘Thursday‘, ‘Friday‘]
>>> another_empty_list = list()
>>> another_empty_list
[]
>>>

list()函数可以将其它数据类型转换成列表:

>>> list("cat")
[‘c‘, ‘a‘, ‘t‘]
>>> a_tuple = ("one", "two", "three")
>>> list(a_tuple)
[‘one‘, ‘two‘, ‘three‘]

>>> birthday = "26/8/1988"
>>> birthday.split("/")  
[‘26‘, ‘8‘, ‘1988‘]
>>> splitme = "a/b//c/d///e"
>>> splitme.split(‘/‘)
[‘a‘, ‘b‘, ‘‘, ‘c‘, ‘d‘, ‘‘, ‘‘, ‘e‘]
>>> splitme.split(‘//‘)
[‘a/b‘, ‘c/d‘, ‘/e‘]            

#split()可以根据分隔符将字符串切割成若干个子串组成的列表,如果待分割的字符串中包含连续的分割符,那么在返回的列表中会出现空串元素。

 

使用 [offset] 获取元素:

>>> weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
>>> weekdays[0]
‘Monday‘
>>> weekdays[::2]
[‘Monday‘, ‘Wednesday‘, ‘Friday‘]
>>> weekdays[1:3]
[‘Tuesday‘, ‘Wednesday‘]
>>> weekdays[-1]
‘Friday‘
>>> weekdays[6]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> weekdays[-6]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range        #当列表偏移量超出列表元素的范围时会产生异常

 

 

>>> weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
>>> weekdays[1] = "TUESDAY"          #使用[offset]修改元素
>>>
>>> weekdays
[‘Monday‘, ‘TUESDAY‘, ‘Wednesday‘, ‘Thursday‘, ‘Friday‘]

>>> weekdays.pop()        #pop()默认删除最后一个元素,添加参数后删除指定位置的元素
‘Friday‘
>>> weekdays.pop(1)
‘TUESDAY‘
>>> weekdays
[‘Monday‘, ‘Wednesday‘, ‘Thursday‘]
>>> del weekdays[-1]            #del删除指定位置的元素
>>> weekdays
[‘Monday‘, ‘Wednesday‘]
>>>
>>> weekdays.append("TT1")          #append()添加元素至末尾
>>> weekdays
[‘Monday‘, ‘Wednesday‘, ‘TT1‘]
>>> weekdays.insert(1,"hello")  #在指定位置插入元素
>>> weekdays
[‘Monday‘, ‘hello‘, ‘Wednesday‘, ‘TT1‘]
>>>
>>> weekdays.remove("TT1")     #指定指定值的元素
>>> weekdays
[‘Monday‘, ‘hello‘, ‘Wednesday‘]
>>>

>>> weekdays
[‘Monday‘, ‘hello‘, ‘Wednesday‘]
>>> others = ["a", "b", 1]
>>> weekdays.extend(others)                #使用extend()或+=合并列表
>>> weekdays
[‘Monday‘, ‘hello‘, ‘Wednesday‘, ‘a‘, ‘b‘, 1]
>>> t1 = [123, 456]
>>> weekdays += t1
>>> weekdays
[‘Monday‘, ‘hello‘, ‘Wednesday‘, ‘a‘, ‘b‘, 1, 123, 456]

>>> weekdays.index("hello")       #使用index()查询指定值的元素的位置
1
>>> weekdays.index("a")
3

>>> "hello" in weekdays        #使用in判断值是否存在列表中
True
>>> "aaa" in weekdays
False

>>> weekdays
[‘Monday‘, ‘hello‘, ‘Wednesday‘, ‘a‘, ‘b‘, 1, 123, 456, ‘hello‘]
>>> weekdays.count("a")            #count()记录指定值在列表中出现的次数
1
>>> weekdays.count("hello")
2
>>>

>>> l = ["a", "b", "c", "d"]
>>> ‘,‘.join(l)         #使用join()把列表转换为字符串, join()函数的参数是字符串或其他可迭代的包含字符串的序列,它的输出是一个字符串
‘a,b,c,d‘
>>> ‘‘.join(l)
‘abcd‘

join()是split()的逆过程:

>>> friends = ["jacky", "eric", "jerry"]
>>> s = "*"
>>> joined = s.join(friends)
>>> joined
‘jacky*eric*jerry‘
>>> separated = joined.split(s)
>>> separated
[‘jacky‘, ‘eric‘, ‘jerry‘]
>>> friends == separated
True
>>>

>>> friends
[‘jacky‘, ‘eric‘, ‘jerry‘, ‘tina‘, ‘tom‘]
>>> friends.sort()                  #列表中的元素是字符串,则会按照字母表顺序排列;
>>> friends
[‘eric‘, ‘jacky‘, ‘jerry‘, ‘tina‘, ‘tom‘]
>>> numbers = [2, 3, 10, 9, 25]
>>> numbers.sort()     #列表中的元素是数字,会默认排列成从小到大的升序;  列表方法sort()会对原列表进行排序,改变原列表的内容
>>> numbers
[2, 3, 9, 10, 25]
>>>

>>> len(friends)       # len()获取长度
5

 

以上是关于列表元组字典与集合的主要内容,如果未能解决你的问题,请参考以下文章

Python列表,元组,集合,字典的区别和相互

Python面试必考重点之列表,元组和字典第一关——如何定义集合/集合与列表有什么区别/如何去掉列表(元组)中重复的元素

列表元组字典集合与字符串

列表元组字典与集合

列表,元组,集合和字典之间有什么区别? [关闭]

列表 字典 元组 集合