for循环排序,注意考虑算法的合理性
for i in range():
if A>B:
elif
else
dafd fas
区间左闭右开;默认从0开始
import re
str="Each human being is born As something new, Something that never existed? before"
#print(str)
str1=str.lower() #统一大小写
word=re.sub('[,?]',"",str1)#运用正则表达式替换掉句子中的特殊符号
words=word.split(" ") #以空格作为分隔符,将字符串转换为以单词为元素的列表
set1=set(words)
dic={}# 空字典
for set1 in words: #在列表中遍历集合中元素
if set1 in dic.keys():#判断集合中元素是否在字典中作为键存在
dic[set1] += 1
else:
dic[set1] = 1
res=sorted(dic.items(),key=lambda x:x[1], reverse=True)
print(res)
f=open("hello.txt","r") 只读
txt=f.read(参数) 限制获取的字符的数量
f.close()
print(txt)
import os
print(os.getcwd())
f.readlines() 以列表形式返回,一行为一个元素
import re
str="Each human being is born As something new, Something that never existed? before"
print(str)
str1=str.lower() #统一大小写
word=re.sub('[,?]',"",str1)#运用正则表达式替换掉句子中的特殊符号
words=word.split(" ") #以空格作为分隔符,将字符串转换为以单词为元素的列表
set1=set(words) #将列表转化为不包含重复元素的集合
dict={i:words.count(i) for i in set1}
print(dict)
1.字典中键不能重复,值可重复,重复的键后面覆盖前面。列表不可哈希,即列表可变,不可作为字典中的键。
2.字典中用updatea()方法进行批量新增
1,字符串类型元素不可变.
string.split() 用split()方法对字符串进行分割,并将结果以数组形式返回。
split(sep=' ') 以空格作为分隔符。
2 .列表可变,可增删改
求sinx 在0-2pi上和x轴围成的面积
import math
n=100
s=sum([abs(math.sin(i*(2*math.pi/n)))*(2*math.pi/n) for i in range(n)])
print(s)
冒泡排序
x=[4,3,2,1]
for i in range(len(x)):
for j in range(i):
if x[i]<x[j]:
x[i],x[j]=x[j],x[i]
print(x)
i=0,j=0
i=1,j=0 [3,4,2,1]
i=2,j=0,1 [2,4,3,1] [2,3,4,1]
i=3,j=0,1,2 [1,3,4,2] [1,2,4,3] [1,2,3,4]
选择排序
x=[5,4,3,2]
k=0
for i in range(len(x)-1):
for j in range(i+1,len(x)):
if x[j]<x[k]:
k=j #保证x[k]始终为最小值
x[k],x[i]=x[i],x[k]
print(x)
i=0,j=1,2,3 [2,4,3,5]
i=1,j=2,3 [2,3,4,5]
i=2,j=3 [2,3,4,5]
str
算术符号
Li=[2.1,'hello',print('hehe'),Ture,[0.0,1]]
res=Li[0]
res=Li[-2]
res=Li[]
内建函数68个
def function(x,y)
return 'result'
def oddcount(x,y=0)
for i in x:
i%2==1
y+=1
return y
import math
math.sin(0)
string='hello world'
f = open('hello world.txt','w')
f . write(string)
f . close()
变量不需要声明,严格缩进,不要乱加
机器学习 = ['决策树','神经网络']
for i in 机器学习:
print (i)
print (i)
Python固定语法
机器学习=['决策树','神经网络','聚类分析']
for 算法 in 机器学习:
print(算法)
string='hello world'
f = open('hello world.txt','w')
f . write(string)
f . close()
Python 3源码文件以
print('helloworld')
https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014316090478912dab2a3a9e8f4ed49d28854b292f85bb000
https://www.python.org/downloads/windows/