import math C = 50 H = 30 value = [] #D = input().split(',') D = [x for x in input().split(',')] for d in D: value.append(str(int(round(math.sqrt(2*C*float(d)/H))))) print(','.join(value))
输入一段序列,根据方程得出结果
注意点:
1. 用list来接收和储存数据,这样对接下来的操作很是方便
2. D = [x for x in input().split(',')] 这种赋值列表的方法
3. 强制类型转换的应用
4. round()函数的用法,返回四舍五入值
--------------------------------------------------------------------------------------------------------------------------------------------------------
accept = input().split(',') x = int(accept[0]) y = int(accept[1]) array = [[0 for i in range(y)] for j in range(x)] print(array) for i in range(x): for j in range(y): array[i][j] = i*j print(array)
根据输入的数字生成二维数组
注意点:
1. array的初始化定义
array = [[0 for i in range(y)] for j in range(x)] 全0,x行y列
>>> array = [[0 for i in range(5)] for j in range(6)] >>> array [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] >>> array = [[i for i in range(5)] for j in range(6)] >>> array [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]] >>> array = [[j for i in range(5)] for j in range(6)] >>> array [[0, 0, 0, 0, 0], [1, 1, 1, 1, 1], [2, 2, 2, 2, 2], [3, 3, 3, 3, 3], [4, 4, 4, 4, 4], [5, 5, 5, 5, 5]]
理解:
最里面的中括号是一行,外面的中括号是循环每一行
------------------------------------------------------------------------------------------------------------------------------------------------------
items = [x for x in input().split(',')] items.sort() print(','.join(items))
根据输入字符串的首字母进行排序
注意点:
1. sort()函数和sorted()函数的区别