本文主要是介绍pytho学习旅途,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
`# merge sort
归并排序
def merge_sort(lst):
# 长度小于一则返回自身
if len(lst) <= 1 :
return lst
middle = int(len(lst)/2)
left = merge_sort(lst[:middle])
right = merge_sort(lst[middle:])
merged = []
while left and right:
merged.append(left.pop(0) if left[0]<=right[0] else right.pop(0))
merged.extend(right if right else left)
return merged
data_lst = [6,202,100,301,38,8,1]
print(merge_sort(data_lst))
import datetime
dtstr = "20181206"
# 转换当前格式日期
dt = datetime.datetime.strptime(dtstr,"%Y%m%d")
#print(dt)
# 切片获取年份
another_dtstr = dtstr[:4] + '0101'
#print(another_dtstr)
another_dt = datetime.datetime.strptime(another_dtstr,"%Y%m%d")
print(int((dt-another_dt).days)+1)
# 输入一行字符,分别统计其中英文字母,空格,数,和其他字符数
import strings='ww-m/23 4j)'# 初始化个数
letter = 0
space = 0
digit = 0
other = 0
for c in s:if c.isalpha():letter+=1elif c.isspace():space+=1elif c.isdigit():digit+=1else:other+=1
print('There are %d letters,%d spaces,%d digits and %d other characters in your string.'%(letter,space,digit,other))
# 打印杨辉三角的前十行triange = [1]
print(triange)triange.append(0)
n = 10
for i in range(1,n):newline = []for j in range(i+1):value = triange[j]+triange[-j-1]newline.append(value)print(newline)triange = newlinetriange.append(0)
这篇关于pytho学习旅途的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!