Python语法小结

Python语法小结:

1.rstrip()方法
返回删除 string 字符串末尾的指定字符后生成的新字符串。

str = "     this is string example....wow!!!     ";
print str.rstrip();  #默认是删除字符串末尾的空格
str = "88888888this is string example....wow!!!8888888";
print str.rstrip('8');

输出:

     this is string example....wow!!!
88888888this is string example....wow!!!

2.输入两行数据

Q = int(input("请输入要输入的行数:"))
# 先输入一个数字代表需要输入几行,比如Q=4,那么就需要再输入4行数据
q = []
for i in range(Q):
    q.append(list(map(int, input().rstrip().split())))
print(q)

输出

请输入要输入的行数:2
10 4
1 2 3 4 5 6 7 8 9 10
[[10, 4], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]

3.map函数

>>> map(square, [1,2,3,4,5])   # 计算列表各个元素的平方
[1, 4, 9, 16, 25]
>>> map(lambda x: x ** 2, [1, 2, 3, 4, 5])  # 使用 lambda 匿名函数
[1, 4, 9, 16, 25]
 
# 提供了两个列表,对相同位置的列表数据进行相加
>>> map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])
[3, 7, 

# 标题

11, 15, 19]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值