list 和tuple 的经典引用案例
from random import sample,randint,shuffle,uniform
from pyecharts.charts import Scatter
import pyecharts.options as opts
def is_duplicated(lst):
for x in lst:
if lst.count(x) > 1:
return True
return False
one = [1,-2,3,4,5,6,7,8,9,0]
print(is_duplicated(one))
def is_duplicateds(lst):
return len(lst) != len(set(lst))
two = [1,-2,3,4,5,6,7,8,9,0,1]
print(is_duplicateds(two))
def reverse(lst):
return lst[::-1]
three = [1,-2,3,4,5,6,7,8,9,0]
print(reverse(three))
def find_duplicate(lst):
ret = []
for x in lst:
if lst.count(x) > 1 and x not in ret:
ret.append(x)
return ret
fore = [1,-2,3,4,5,6,7,8,9,0,5,3]
print(find_duplicate(fore))
def fibonacci(n):
if n <=1:
return [1]
fib = [1,1]
while len(fib) < n:
fib.append(fib[len(fib) - 1] + fib[len(fib) - 2] )
return fib
fi = fibonacci(8)
print(fi)
def fibonaccis(n):
a,b=1,1
for _ in range(n):
yield a
a,b = b,a+b
fis = list(fibonaccis(10))
print(fis)
def mode(lst):
if not lst:
return None
return max(lst,key=lambda v:
lst.count(v))
lstOne = [1, 3, 3, 2, 1, 1, 2,3,5,3,3,3,3,3]
overLst = mode(lstOne)
print(overLst)
def modes(lst):
if not lst:
return None
max_freq_elem = max(lst,key=lambda v: lst.count(v))
max_freq = lst.count(max_freq_elem)
ret = []
for i in lst:
if i not in ret and lst.count(i)==max_freq:
ret.append(i)
return ret
lstTwo = [1, 3, 3, 2, 1, 1, 2,3,5,3,3,3,3,3,1,1,1,1,1]
overLstt = modes(lstTwo)
print(f' 具有多个相同数量的元素是: {overLstt}')
def max_len(*lst):
return max(*lst,key=lambda v: len(v))
maxl = max_len([1,2,3,4],[4,5,6,7,3],[8,9])
print(f' 更长的列表是:{maxl}')
def head(lst):
return lst[0] if len(lst) > 0 else None
print(f' 列表为空返回None:{head([])}')
print(f' 列表不为空的时候返回第一个值: {head([4,5,6,8])}')
def head(lst):
return lst[-1] if len(lst) > 0 else None
print(f' 列表为空返回None:{head([])}')
print(f' 列表不为空的时候返回最后一个值: {head([4,5,6,8])}')
ls = list(zip([1,2],[2,3]))
print(f' 元素对是:{ls}')
def pair(r):
return list(zip(r[:-1],r[1:]))
pa = pair(range(15))
print(f'生成15个相邻元素对:{pa}')
lstthree = range(150)
print(f'range返回的数据是:{lstthree}')
lstthree = [randint(0,50) for _ in range(100)]
print(f'生成100个整数:{lstthree}')
print(f'输出前5个数:{lstthree[:5]}')
lstthree_sample = sample(lstthree,10)
print(f'随机抽样10个数:{lstthree_sample}')
lstFor = [randint(0,50) for _ in range(100)]
print(f' 重洗数据集生成100个整数:{lstFor}')
shuffle(lstFor)
print(f'重洗牌后的前5个数:{lstFor[:5]}')
print(f'重洗牌后的所有数:{lstFor}')
x,y = [i for i in range(100)],[round(uniform(0,10),2) for _ in range(100)]
print(f'X的坐标点是:{x}')
print(f'Y的坐标点是:{y}')
def draw_uniform_points():
x,y = [i for i in range(100)],[round(uniform(0,10),2) for _ in range(100)]
print(y)
c = (Scatter().add_xaxis(x).add_yaxis('y',y))
c.render()
draw_uniform_points()