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]