列表-元组-字典

type() 函数:如果你只有第一个参数则返回对象的类型


a = [1,2,3]
b= {'k1':'v1','k2':'v2'}
c = (1,2,3,4,5)
d = 4

print(type(a))         #列表
print(type(b))        #字典
print(type(c))        #元组
print(type(d))    #int类型


执行结果:


<class 'list'>
<class 'dict'>
<class 'tuple'>
<class 'int'>

-------------------------------------------------

列表可以用来存储一组对象,可以动态的增加和删除对象元素,通过下标来访问具体的对象。

列表的定义有2种方式:一对中括号、list函数;具体如下:

lb2 = []   #定义一个空列表

lb3 = list()   #定义一个空列表

lb4 = [1,2,3]        #纯数字列表

lb5 = ['a','b','c']  #纯字符串列表

lb6 = [1,'a',True,None,12]   #混合列表

print(lb2)
print(lb3)
print(lb4)
print(lb5)
print(lb6)


执行结果:


[]
[]
[1, 2, 3]
['a', 'b', 'c']
[1, 'a', True, None, 12]

--------------------------------------------------------------------



lb = [1,2,3,4,5,6]        #纯数字列表
#------------------------------------------------
#  通过下标访问,下标从0开始:
print(lb[0]) #1     #索引下标从0开始,第一个元素
print(lb[5]) #6      #第六个元素

print(lb[-1]) #6     #最后一个元素
print(lb[-2]) #5    #倒数第二个元素

#------------------------------------------------

通过切片访问,切片符号是冒号:

冒号前为起始切片下标位置,冒号后为结束切片下标位置;

起始位置下标在切片范围之内,结束位置下标不在切片范围之内;


起始位置下标留白,表示从第一位开始;结束位置下标留白,表示到列表结束;

下标为负数表示从列表尾部开始计数,-1为最后一个元素下标,-2为倒数第二个元素下标

print(lb[0:2])  #[1, 2]
print(lb[0:3])  #[1, 2, 3]
print(lb[1:2])  #[2]

#--------------------------------------------

print(lb[0:]) #[1, 2, 3, 4, 5, 6]
print(lb[1:]) #[2, 3, 4, 5, 6]

#-------------------------------------------

print(lb[:0]) #[]
print(lb[:1]) #[1]
print(lb[:5]) #[1, 2, 3, 4, 5]

print(lb[:-1]) #[1, 2, 3, 4, 5]
#-------------------------------------------

print(lb[:])  #[1, 2, 3, 4, 5, 6]

#-------------------------------------------

#指定步长的切片访问,第二个冒号后为步长

print(lb[::]) #[1, 2, 3, 4, 5, 6]
print(lb[::3]) # [1, 4]
print(lb[::5]) # [1, 6]

---------------------------------------------


lb = [1,2,3,4,5,6]        #纯数字列表

lb[3:] = 'd'  #表示第4个元素包括第4个元素全部覆盖

print(lb)  #[1, 2, 3, 'd']

----------------------------------------------------

lb = [1,2,3,4,5,6]        #纯数字列表

lb[:3] = 'd'  #表示第4个元素之前,不包括第4个元素全部覆盖

print(lb)  #['d', 4, 5, 6]

----------------------------------------------------------------

lb = [1,2,3,4,5,6]        #纯数字列表

print(lb[2:4])  #[3, 4]

lb[2:4] = 'd'  #表示第4个元素之前,不包括第4个元素全部覆盖

print(lb)  #[1, 2, 'd', 5, 6]

======================================================================================

元组定义之后,其内容、长度是不可以修改的。除了内容不可以修改,其它操作和列表一样


元组和列表之间可以相互转化:


列表转换为元组

lb = [1,2]

yz = tuple(lb)

print(yz)  #(1, 2)

--------------------------------------
元组转换为列表

yz = (1,2)

lb = list(yz)

print(lb)  #[1, 2]


=================================================================================


字典的键不能重复,而不同的键对应的值可以重复,字典的键和值都可以支持任意类型


字典的定义有2种方式:大括号、dict函数

zd2 = {}

zd3 = dict()

print(zd2)  #{}   #定义了一个空字典

print(zd3)  #{}   #定义了一个空字典

----------------------------------------------

字典除了可以直接定义,也可以通过列表或元组进行转换,在定义字典的时候,


如果键有重复,则后面的键所对应的值,会覆盖前面的键的值。


注意:通过列表或者元组转换时,需要保证子元素都是一个长度为2的列表或者元组


lb = [('a','b'),('c','d'),('e','f')]

zd = dict(lb)

print(zd)      #{'a': 'b', 'c': 'd', 'e': 'f'}


-------------------------------------------------------

yz = (['a','b'],['c','d'],['e','f'])

zd = dict(yz)

print(zd)   #{'a': 'b', 'c': 'd', 'e': 'f'}


-------------------------------------------------------------

字典的读取方式是通过健来读取对应的值;

字典读取值的方式有2种,通过中括号、get方法,都需要接收一个健然后返回对应的值;


注意:

通过中括号访问字典时,如果给定的键不存在,会抛出异常;

通过get方法访问字典时,如果给定的键不存在,则默认会返回none值,并且还可以指定一个默认的值


zd = {'a': 'b', 'c': 'd', 'e': 'f'}

print(zd['a'])  #b

#print(zd['b'])  #抛出异常

print(zd.get('a'))   #b

print(zd.get('b')   #None

print(zd.get('b','不存在这个键值'))  #不存在这个键值


=======================================================================================

遍历键:返回一个健的列表

zd = {'a': 'b', 'c': 'd', 'e': 'f'}

print(zd.keys())   # dict_keys(['a', 'c', 'e'])

lb = list(zd.keys())   # 通过list函数,把键转换为列表

print(lb)  # ['a', 'c', 'e']

print(lb[0])  # a


------------------------------------------------------------------

遍历值:返回一个值的列表

zd = {'a': 'b', 'c': 'd', 'e': 'f'}

print(zd.values())   # dict_values(['b', 'd', 'f'])

lb = list(zd.values())   # 通过list函数,把值转换为列表

print(lb)  # ['b', 'd', 'f']

print(lb[0])  # b


------------------------------------------------------------------

遍历键-值:返回一个健-值的列表

zd = {'a': 'b', 'c': 'd', 'e': 'f'}

print(zd.items())

x = list(zd.items())

print(x)

for k,v in zd.items():
    print('\n'+k)
    print(v)



执行结果:

dict_items([('a', 'b'), ('c', 'd'), ('e', 'f')])

[('a', 'b'), ('c', 'd'), ('e', 'f')]

a
b

c
d

e
f

--------------------------------------------


zd = {'a': 'b', 'c': 'd', 'e': 'd'}

for i in set(zd.values()):    #对包含重复元素的列表,通过set()找出独一无二的元素
    print(i)


print(set(zd.values()))


执行结果:

d
b
{'d', 'b'}

猜你喜欢

转载自www.cnblogs.com/xiaobaibailongma/p/12074774.html
今日推荐