天池python训练营Day06

集合

set与dict类似,但是set没有键值对

// A code block

agg=set()    #创建一个集合
agg.add('append')    #在集合中添加元素
agg.add('lower')
print(agg)

var foo = 'bar';
// An highlighted block

{'append', 'lower'}

var foo = 'bar';

集合各函数的用法
in ,not in 用于判断属于与不属于,is , not is用于判断相等与不相等
set(value)可以将列表,元组转换为集合

// A code block

list0=['hello','python','world']
tuple0=(1,2,3)
print(set(list0))      #将列表转换为集合
print(set(tuple0))   #将元组转换为集合

var foo = 'bar';
// An highlighted block

{'world', 'hello', 'python'}
{1, 2, 3}

var foo = 'bar';

set.add(value)用于向集合中添加元素

// A code block

set0={'hello','python','world'}
set0.add('lucky')
print(set0)
print(set0.add('tuple'))    #不注意格式会出错

var foo = 'bar';
// An highlighted block

{'lucky', 'hello', 'world', 'python'}
None

var foo = 'bar';

set.update(value)用于向集合中添加元素(只可以,以集合的方式添加)

// A code block

set0={'hello','python','world'}
set1={'lucky','python','tuple'}
set0.update(set1)                     #相同的元素只取一个
print(set0)

set0.update('list')      #格式不对会出错
print(set0)

var foo = 'bar';
// An highlighted block

{'world', 'lucky', 'python', 'hello', 'tuple'}
{'lucky', 'python', 'hello', 's', 'i', 't', 'l', 'world', 'tuple'}

var foo = 'bar';

set.remove(value)用于移除集合中的值

// A code block

set0={'hello','python','world'}
set0.remove('hello')
print(set0)

set0.remove('lucky')       #移除没有的元素会出错
print(set0)

var foo = 'bar';
// An highlighted block

{'python', 'world'}
KeyError: 'lucky'

var foo = 'bar';

set.discard(value) 用于移除指定的集合元素。remove() 方法在移除一个不存在的元素时会发生错误,而 discard() 方法不会。

// A code block

set0={'hello','python','world'}
set0.discard('hello')         #移除指定元素
print(set0)

var foo = 'bar';
// An highlighted block

{'python', 'world'}

var foo = 'bar';

set.pop() 用于随机移除一个元素。

// A code block

set0={'hello','python','world'}
set0.pop()          #随机移除一个元素
print(set0)

var foo = 'bar';
// An highlighted block

{'world', 'python'}

var foo = 'bar';

set.intersection(set1, set2) 返回两个集合的交集。
set1 & set2 返回两个集合的交集。

// A code block

set0={'hello','python','world'}
set1={'lucky','tuple','python'}
x=set.intersection(set0,set1)
print(x)
print(set0 & set1)

var foo = 'bar';
// An highlighted block

{'python'}
{'python'}

var foo = 'bar';

set.union(set1, set2) 返回两个集合的并集。
set1 | set2 返回两个集合的并集。

// A code block

set0={'hello','python','world'}
set1={'lucky','tuple','python'}
x=set.union(set0,set1)
print(x)
print(set0 | set1)

var foo = 'bar';
// An highlighted block

{'python', 'hello', 'lucky', 'world', 'tuple'}
{'python', 'hello', 'lucky', 'world', 'tuple'}

var foo = 'bar';

序列

在 Python 中,序列类型包括字符串、列表、元组、集合和字典,这些序列支持一些通用的操作,但比较特殊的是,集合和字典不支持索引、切片、相加和相乘操作。

划重点:集合和字典不支持索引、切片、相加和相乘操作。

// A code block

list0=list()    #创建列表
print(list0)

list1='Time flies, time is no longer'
print(list(list1))          #转换为列表

tuple0=(1,2,3,4,5)
print(list(tuple0))       #转换为列表

var foo = 'bar';
// An highlighted block

[]
['T', 'i', 'm', 'e', ' ', 'f', 'l', 'i', 'e', 's', ',', ' ', 't', 'i', 'm', 'e', ' ', 'i', 's', ' ', 'n', 'o', ' ', 'l', 'o', 'n', 'g', 'e', 'r']
[1, 2, 3, 4, 5]

var foo = 'bar';

tuple(sub) 把一个可迭代对象转换为元组

// A code block

tuple0=tuple()
print(tuple0)

list1='Time flies, time is no longer'
print(tuple(list1))          #转换为元组

list2=[1,2,3,4,5]
print(tuple(list2))       #转换为元组

var foo = 'bar';
// An highlighted block

()
('T', 'i', 'm', 'e', ' ', 'f', 'l', 'i', 'e', 's', ',', ' ', 't', 'i', 'm', 'e', ' ', 'i', 's', ' ', 'n', 'o', ' ', 'l', 'o', 'n', 'g', 'e', 'r')
(1, 2, 3, 4, 5)

var foo = 'bar';

sorted(iterable, key=None, reverse=False) 对所有可迭代的对象进行排序操作。
iterable – 可迭代对象。
key – 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
reverse – 排序规则,reverse = True 降序 , reverse = False 升序(默认)。
返回重新排序的列表。

// A code block

x = [-8, 99, 3, 7, 83]
print(sorted(x))  # [-8, 3, 7, 83, 99]
print(sorted(x, reverse=True))  # [99, 83, 7, 3, -8]

t = ({"age": 20, "name": "a"}, {"age": 25, "name": "b"}, {"age": 10, "name": "c"})
x = sorted(t, key=lambda a: a["age"])      #a这个值可以随便取,a:["age"] 比较age键的大小
print(x)

var foo = 'bar';
// An highlighted block

[-8, 3, 7, 83, 99]
[99, 83, 7, 3, -8]
[{'age': 10, 'name': 'c'}, {'age': 20, 'name': 'a'}, {'age': 25, 'name': 'b'}]

var foo = 'bar';

zip(iter1 [,iter2 […]])
用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的对象,这样做的好处是节约了不少的内存。
我们可以使用 list() 转换来输出列表。
如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符,可以将元组解压为列表。

// A code block

a = [1, 2, 3]
b = [4, 5, 6]
c = [4, 5, 6, 7, 8]

zipped = zip(a, b)
print(zipped)  # <zip object at 0x000000C5D89EDD88>    #本段可以用c++的方式来理解
print(list(zipped))  # [(1, 4), (2, 5), (3, 6)]
zipped = zip(a, c)
print(list(zipped))  # [(1, 4), (2, 5), (3, 6)]

a1, a2 = zip(*zip(a, b))
print(list(a1))  # [1, 2, 3]
print(list(a2))  # [4, 5, 6]

var foo = 'bar';
// An highlighted block

<zip object at 0x000001F0517E38C8>
[(1, 4), (2, 5), (3, 6)]
[(1, 4), (2, 5), (3, 6)]
[1, 2, 3]
[4, 5, 6]

var foo = 'bar';
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值