集合
# 集合 : 无序不重复
a = {11, 2, 33, "222", 11, 11}
print(a)
print(type(a)) #set
#
# print(a[0])
a.add("哈哈")
a.add(11)
print(a)
b = set() # 空集合
# 删除
print(a.pop())
print(a.pop())
# 集合可以被遍历
for x in a:
print(x)
#
# 去重
from random import randint
ls = [randint(20, 30) for _ in range(10)]
print(ls)
print(set(ls))
print(list(set(ls)))
函数
def calSum(n):
mySum = 0
for i in range(1, n+1):
mySum += i
print(mySum)
函数分类
# 无参无返回
def func1():
print("func1()")
print(func1()) # None
# 无参有返回
def func2():
return "hello"
res1 = func2()
print(res1)
print(func2())
# 有参无返回
def func3(x, y):
print(x**y)
func3(3, 3)
# 有参有返回
# return的作用 结束这个函数, 返回对应的值
def calSum(n):
if n <=1:
return -1
mySum = 0
for i in range(1, n+1):
mySum += i
return mySum, n
print(calSum(100)) # (5050, 100)
print(calSum(-100))
from random import randint
函数的参数
# 参数
# 必须参数: 函数声明是和调用时顺序要一致
def func(name, age, addr):
age += 1
print(f'姓名: {name}, 年龄: {age}, 地址: {addr}')
func("eric", 23, "铁岭的")
# func(23, "铁岭的", "eric")
# 关键字参数 函数声明是和调用时顺序可以不一致
func(addr='沈阳的', age=29, name='bob')
func("miller", addr='沈阳的', age=36)
# 默认参数 函数声明是和调用时个数可以不一致
print("+++++++++++++++++++++++++++++++++++++++++")
def func2(name, age=0, addr=None):
age += 1
print(f'姓名: {name}, 年龄: {age}, 地址: {addr}')
func2("eric", 23, "铁岭的")
func2("james")
func2("james", addr="beijing")
func2("james", 12)
递归
### 递归函数: 函数自己调用自己
### 把一个问题分解成更小的单元
# 要有终止条件
# 1 ~ n累加
def f(n):
if n == 1:
return 1
return f(n-1) + n
print(f(10))
print(f(100))
print(f(5))
# n 的阶乘
# 斐波那契数列 1, 1, 2, 3, 5, 8, 的第n项和
def func1(n):
if n==1:
return 1
return func1(n-1)*n
print(func1(5))
def fibo(n):
if n in [1, 2]:
return 1
return f(n-1)+f(n-2)
print(fibo(6))
print("++++++++++++++++++++++++++")
#
from random import randint
ls = [randint(0, 20) for _ in range(10)]
print(ls)
print(sum(ls))
def calList(ls):
if len(ls) == 0:
return 0
if len(ls) == 1:
return ls[0]
return ls[0] + calList(ls[1:])
print(calList(ls))
print(calList([]))
def f(ls):
if ls==[]:
return 0
a=ls.pop()
return f(ls)+a
print(f([]))
print(f(ls))
匿名函数和高阶函数
#系统高阶函数
# map()函数接收两个参数,一个是函数,一个是Iterable,
# map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回
from random import randint
print(list(map(lambda x: x**2, [i for i in range(10)])))
ls = [randint(-20, 20) for i in range(10)]
print(ls)
print(list(map(abs, ls)))
print(list(map(lambda x: x if x > 0 else -x, ls)))
print(list(map(str, ls)))
# reduce
# reduce把一个函数作用在一个序列[x1, x2, x3, ...]上,这个函数必须接收两个参数,
# reduce把结果继续和序列的下一个元素做运算
from functools import reduce
print(reduce(lambda x, y: x * y, [i for i in range(1, 4)]))
print(reduce(lambda x, y: x - y, [i for i in range(1, 5)]))
# filter()也接收一个函数和一个序列。和map()不同的是,filter()把传入的函数依次作用于每个元素,
# 然后根据返回值是True还是False决定保留还是丢弃该元素
print("------------------filter------------------------------")
ls = [randint(-20, 20) for i in range(10)]
print(ls)
print(list(filter(lambda x: x >= 0, ls)))
print(list(filter(lambda x: x%2==0, ls)))
# 回文数 从左往右和从右往左都是一样的 1, 121,
ls = [i for i in range(1, 200)]
print(list(filter(lambda x: x == int(str(x)[::-1]), ls)))
print(set(map(lambda x: x**2, {i for i in range(10)})))
print(list(map(lambda x: x + '_', "abcdef")))
sorted
from random import shuffle
ls = [i for i in range(10)]
shuffle(ls)
print(ls)
# print(sorted(ls))
# print(sorted(ls, reverse=True))
# print(ls)
def bubbleSort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-1-i):
if arr[j] > arr[j+1]:
# 交换
arr[j], arr[j + 1] = arr[j + 1], arr[j]
print(arr)
bubbleSort(ls)
print(ls)