python003——数据类型列表、元组、字典、集合

1.列表——方法insert()、extend()
append() 方法是将参数作为一个元素增加到列表的末尾。
extend() 方法则是将参数作为一个列表去扩展列表的末尾。

>>> name = ['F', 'i', 's', 'h']
>>> name.append('C')
>>> name
['F', 'i', 's', 'h', 'C']
>>> name.extend(['.', 'c'])
>>> name
['F', 'i', 's', 'h', 'C', '.', 'c']
>>> name.append(['o', 'm'])
>>> name
['F', 'i', 's', 'h', 'C', '.', 'c', ['o', 'm']]

2.列表——方法pop()、remove()
pop() 方法移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。

>>> list1 = ['Google', 'Runoob', 'Taobao','cat','dog','baidu']
>>> list1.pop()
'baidu'
>>> print ("列表现在为 : ", list1)
列表现在为 :  ['Google', 'Runoob', 'Taobao', 'cat', 'dog']
>>> list1.pop(1)
'Runoob'
>>> print ("列表现在为 : ", list1)
列表现在为 :  ['Google', 'Taobao', 'cat', 'dog']


>>> list1.remove('cat')
>>> list1
['Google', 'Taobao', 'dog']

3.列表推导式\列表解析式

list1 = [(x, y) for x in range(10) for y in range(10) if x%2==0 if y%2!=0]
相当于
list1 = []
for x in range(10):
    for y in range(10):
        if x%2 == 0:
            if y%2 != 0:
                list1.append((x, y))

元组推导式是个生成器,字典生成器、集合生成器请看:
链接: link

4.将左边列表的内置方法与右边的注释连线,并圈出元组可以使用的方法列表和元组的方法
5.Python 直接赋值、浅拷贝和深度拷贝解析

  • 直接赋值:其实就是对象的引用(别名)。
  • 浅拷贝(copy):拷贝父对象,不拷贝对象的内部的子对象
  • 深拷贝(deepcopy): copy 模块的 deepcopy 方法,完全拷贝了父对象及其子对象
>>>a = {1: [1,2,3]}
>>> b = a.copy()
>>> a, b
({1: [1, 2, 3]}, {1: [1, 2, 3]})
>>> a[1].append(4)
>>> a, b
({1: [1, 2, 3, 4]}, {1: [1, 2, 3, 4]})
>>>import copy
>>> c = copy.deepcopy(a)
>>> a, c
({1: [1, 2, 3, 4]}, {1: [1, 2, 3, 4]})
>>> a[1].append(5)
>>> a, c
({1: [1, 2, 3, 4, 5]}, {1: [1, 2, 3, 4]})

6.切片问题
python中[-1]、[:-1]、[::-1]、[n::-1]使用方法
(a[-1:] == a[-1]虽数值相等,但一个是列表,一个是元素)

import numpy as np
a=np.random.rand(5)
print(a)
[ 0.64061262  0.8451399   0.965673    0.89256687  0.48518743]

print(a[-1]) ###取最后一个元素
[0.48518743]

print(a[:-1])  ### 除了最后一个取全部
[ 0.64061262  0.8451399   0.965673    0.89256687]

print(a[::-1]) ### 取从后向前(相反)的元素
[ 0.48518743  0.89256687  0.965673    0.8451399   0.64061262]

print(a[2::-1]) ### 取从下标为2的元素翻转读取
[ 0.965673  0.8451399   0.64061262]

详细: link

5.字典的fromkeys()方法

  • dict1.fromkeys()只是创建新的字典,对原数组无影响
  • 不要试图使用它来修改一个原有的字典,它会直接把整个字典给覆盖掉
  • 不要指望分别给键对应的值
dict1 = {}
a=dict1.fromkeys((1, 2, 3))
b=dict1.fromkeys((4,5))
c= dict.fromkeys((1, 2, 3), ('one', 'two', 'three'))
print(dict1,'\n',a,'\n',b,'\n',c)

{} 
{1: None, 2: None, 3: None} 
{4: None, 5: None} 
{1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}

6.字典的get和setdefault()方法

  • 可以在get中为不存在的项输出相应的提示
  • setdefault()用法与get()类似,只是如果找不到对应的键,会自动添加,值默认为None,也可以给值
>>> dict1 = {1:'one', 2:'two', 3:'three', 4:'four', 5:'five'}
>>> dict1.get(5,  '不存在')
'five'
>>> dict1.get(6, '不存在')
'不存在'
>>> dict1
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five'}

>>>dict1.setdefault(2)
'two'
>>> dict1.setdefault(6, 'six')
'six'
>>> dict1
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six'}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值