python学习笔记(1)

  1. 输入输出
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 -- 如果指定键的值不存在时,返回该默认值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值