python基础篇 10-三元表达式、列表生成式、集合

三元表达式:

age = 18

if age<18:
    v = '未成年人'
else:
    v = '成年人'

v = '未成年人' if age < 18 else '成年人'

列表生成式:

a = [1,2,3,4,5,6]
b = []
for i in a:
    b.append(str(i))

c = [ str(i) for i in a]
d = [ str(i) for i in a if i%2!=0 ]

集合:天生去重,无序

#set
#天生去重,无序的 没有下标
from collections import Iterable

l = [1,2,11,1,1,3,5,7]
s = set(l)
print(s)



# 定义集合
l2 = {1,2,3,4,5,1,1}
print(l2)
l3 = set()              #定义空集合




#交集 并集 差集
l3.add(1)       #新增元素
l3.remove(1)    #删除元素
l3.update(l2)   #把一个集合加入到另外一个集合里面,去重加入


stu1 = ['fd','wxl','zjr','lhy']#自动化
stu2 = ['fd','wxl','dsx','cc']#性能

stu1_set = set(stu1)
stu2_set = set(stu2)

print(stu1_set.intersection(stu2_set))  #取交集
print(stu1_set & stu2_set)  #取交集


# 使用场景 例子
import string
password = 'abc123A'
password_set = set(password)
if password_set & set(string.digits) and password_set & set(string.ascii_lowercase) \
    and password_set & set(string.ascii_uppercase):
    print('密码合法')
else:
    print('不合法')


#并集,把两个集合合并到一起
s1 = {1,2,3,4}
s2 = {4,5,6,7}
print(s1.union(s2))
print(s1 | s2)


#差集
print(s1.difference(s2)) #在一个集合里面存在,在另外一个集合中不存在的
print(s1 - s2)

#对称差集
print(s1 ^ s2)
print(s1.symmetric_difference(s2))      #去除交集后,剩下的元素

for s in s1:        #可迭代对象
    print(s)
print(isinstance(s1,Iterable))

 

posted @ 2021-09-02 20:39  捞铁  Views(41)  Comments(0Edit  收藏  举报