# # 1: 构建一个列表(使用列表推导式,和list(iterable)
# # 和列表相加的方式完成
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 'j', 'q', 'k', 'a']
l1 = [i for i in range(1,10)+list('jqka')]
print(l1)
# 2:使用列表推导式完成找
# 找到嵌套列表中名字含有两个‘e’的所有名字
names = [['Tom', 'Billy', 'Jefferson', 'Andrew', 'Wesley', 'Steven', 'Joe'],
['Alice', 'Jill', 'Ana', 'Wendy', 'Jennifer', 'Sherry', 'Eva']]
l = []
for i in names[0]:
l.append(i)
for j in names[1]:
l.append(j)
print(l)
re = [i for i in l if i.count('e') == 2]
print(re)
l = [j for i in names for j in i if j.count('e') == 2 ]
print(l)
# 3:将一个字典的key和value对调
mcase = {'a': 10, 'b': 34}
l2 = {v:k for k,v in mcase.items()}
print(l2)
# 4:合并大小写对应的value值,将k统一成小写
mcase = {'a': 10, 'b': 34, 'A': 7, 'Z': 3}
re = {k.lower() : mcase.get(k.lower(),0) + mcase.get(k.upper(),0) for k in mcase.keys()}
print(re)
# # 集合推导式
# # 5 :squared = {x ** 2 for x in [1, -1, 2]}
# # print(squared)
#
#
#
# # 6:去重
l = [1, 2, 3, 4, 5, 6, 3, 3, 3, 3, 3, 3]
# 用两种方式,第一种,给一个空list,判断添加
# 第二种,list - --->set
l1 = []
for i in l:
if i not in l1:
l1.append(i)
print(l1)
se = set(l)
print(se)
# 7: 集合练习:
# 经理有: 曹操,刘备,周瑜
# 技术员有: 曹操,周瑜,张飞,赵云
# 用集合求:
jl = {'曹操','刘备','周瑜'}
jsy = {'曹操','周瑜','张飞','赵云'}
# 1
# 即是经理也是技术员的有谁? # 交集
print(jl&jsy)
# 2
# 是经理,但不是技术员的有谁? # 差集
print(jl-jsy)
# 3
# 是技术人员,但不是经理的都有谁? # 差集
print(jsy-jl)
# 4
# 张飞是经理吗? #
while '张飞' in jl:
print('是')
else:
print('不是')
# 5
# 身兼一职的人都有谁?
print(jl^jsy)
# 6
# 经理和技术员共有几个人?
jl = set(jsy)
print(len(jl))
# 8
# 输入一段字符串,打印所有输入过的字符串,但重复的只打印一次,(不要求打印的顺序与输入顺序一致)
# 输入: abcdabcaba
# 打印:
re = str(input('请输入》》》》'))
for i in set(re):
print(i)
# a
# b
# c
# d
# 9
# 输入一段字符串,打印出这个字符串中出现过的字符及出现过的次数:
# 如:
# 输入:abcdabcaba
# 打印如下:
l = 'abcdabcaba'
le = dict()
for i in l:
if i not in le:
le[i] = 1
else:
le[i] = le[i]+1
print(le)
for k,v in le.items() :
print(f'{k}:{v} 次' )
s = 'python,java,pjp,go,c,python,java,pjp,go,c,python,java,pjp,go,c'
re = s.split(',')
di = {}
for i in re:
if i not in di:
di[i] = 1
else:
di[i] = di[i]+1
print(di)
for k,v in di.items():
print(f'{k} : {v} 次')
import collections
s = 'python,java,pjp,go,c,python,java,pjp,go,c,python,java,pjp,go,c'
re = s.split(',')
print(collections.Counter(re))
for word,count in collections.Counter(re).items():
print(word,count,sep=':')
# collections.Counter 统计次数
s = 'python,java,pjp,go,c,python,java,pjp,go,c,python,java,pjp,go,c'
re = s.split(',')
le = {}
for i in re:
if i not in le:
le[i] = re.count(i)
print(le)
for k,v in le.items():
print(f'{k} : {v} 次 ')
# # a: 4
# # 次
# # b: 3
# # 次
# # d: 1
# # 次
# # c: 2
# # 次
# # 注: 不要求打印顺序