- 输入输出
lst=eval(input())
x, y = input().split()#分隔输入
print(x,y,sep=',')#分隔符输出
print(x):print(y)#换行输出
for i in range(1,5):
print(i,end=' ')
函数式编程
lst=[3,2,5,2,1]
list(map(lambda x:x*2,lst))#映射
lst=[1,2,3,4,5,6]
list(filter(lambda x:x%2==0,lst))#筛选
from functools import reduce
lst=[1,2,3,4,5,6]
reduce(lambda x,y:x+y,lst)#递归计算
lst1 = [12, 3.45, 647]
list(map(str, lst1))#映射字符
lst2 = ['abc', 'sss']
# list(map(upper,lst2))错误写法
list(map(lambda word: word.upper(), lst2)) # 等价于str2.upper()
lst3 = [1, 2, 3, 4, 5]
# 字符串是不可迭代对象,在数组得后面可以加[:]
for x in lst3:
if x % 2 == 0:
lst3.remove(x)
print(lst3)
拷贝
# 浅拷贝 深拷贝
x = [1, 2, 3]
y = x
# y=x.copy()
y[0] = 4 # 深
z = x[:] # 浅
z[0] = 8
import copy
z = copy.deepcopy(x)#浅(默认)
字典`
aDict = {}.fromkeys(('1', '2', '3', '4'), 3000)
names = ['a', 'b', 'c', 'd', 'e']
salary = [3000, 2000, 2200, 3300, 5000]
dict(zip(names, salary))
词频统计
poem_EN = 'Life can be good, life can be sad. Life is mostly cheerful, But sometimes sad.'
poem_list = poem_EN.split() # 默认用空格分开
# print(poem_list)
p_dict = {}
for item in poem_list:
if item[-1] in ',.\'"':
# print(item[:-1])
item = item[:-1] # 去除标点符号
if item not in p_dict: # 词频统计
p_dict[item] = 1
else:
p_dict[item] += 1
p_dict[item] = p_dict.get(item, 0) + 1 # dict.get(key, default=None) key -- 字典中要查找的键。default -- 如果指定键的值不存在时,返回该默认值。