【AI达人养成营】学习笔记
收藏
通过这一段时间的python 学习,收获了很多,如下是我在学习中认为比较重要的东西,不足之处请大家指教。
字符串的格式化输出
%
重点内容!
另
list常用函数
添加新的元素
In [ ]
list1 = ['a','b','c','d','e','f']
list1.append('g') # 在末尾添加元素
print(list1)
list1.insert(2, 'ooo') # 在指定位置添加元素,如果指定的下标不存在,那么就是在末尾添加
print(list1)
In [ ]
list2 = ['z','y','x']
list1.extend(list2) #合并两个list list2中仍有元素
print(list1)
print(list2)
count 计数 和 index查找
In [ ]
list1 = ['a','b','a','d','a','f']
print(list1.count('a'))
In [ ]
list1 = ['a','b','a','d','a','f']
print(list1.index('a'))
In [ ]
print('a' in list1)
删除元素
In [ ]
list1 = ['a','b','a','d','a','f']
print(list1.pop(3))
print(list1)
list1.remove('a')
print(list1)
定义一个函数;函数调用
In [ ]
def student_name(name):
"打印学生的名字"
print('姓名:', name)
return {'姓名':name}
In [ ]
rst = student_name('Alice')
rst
In [ ]
# 返回多个值
def student_name_and_age():
"记录学生的名字和年龄"
name = input('请输入姓名\n')
age = int(input('请输入年龄\n'))
print(f'姓名:{name};年龄:{age}')
return name,age
In [ ]
rst = student_name_and_age()
In [ ]
type(rst)
In [ ]
name, age = student_name_and_age()
In [ ]
冒泡排序:
# 随机生成
import numpy as np
#请把冒泡排序算法补充完整
def bubble_sort(nums):
for i in range(1, len(nums)):
for j in range(len(nums) - i):
if nums[j] > nums[j+1]:
nums[j],nums[j+1] =nums[j+1], nums[j]
return nums
# 打乱顺序并随机生成
if __name__ == '__main__':
nums=np.random.permutation(20)
print(bubble_sort(nums))
希尔排序:
# 随机生成
import numpy as np
#请把希尔排序算法补充完整
def shellSort(nums):
step=len(nums)//2
while step:
for i in range(step,len(nums)):
temp=nums[i]
index=i-step
while index >=0 and nums[index]>temp:
nums[index+step]=nums[index]
index-=step
nums[index+step]=temp
step=step//2
return nums
if __name__ == '__main__':
nums=np.random.permutation(20)
print(shellSort(nums))
插入排序:
# 随机生成
import numpy as np
#请把插入排序算法补充完整
def insertionSort(nums):
if len(nums) < 2:
return nums
for i in range(1, len(nums)):
temp = nums[i]
index = i-1
while index >= 0 and temp < nums[index]:
nums[index+1] = nums[index]
index -= 1
nums[index+1] = temp
return nums
if __name__ == '__main__':
nums=np.random.permutation(20)
print(insertionSort(nums))
0
收藏
请登录后评论
常用排序:*****,实用排序:sorted(),哈哈哈。