目录
定义一个函数,输入名字列表,输入随机数量,提取相应数量的名字,当次提取的名字不能有重复
水仙花数的程序编写
方法一
for i in range(100,1000):
strs = str(i)
bai = strs[0]
shi = strs[1]
ge = strs[2]
if eval('{}**3+ {}**3+ {}**3'.format(bai,shi,ge))==i:
print(i)
方法二
for s in range(100,1000):
ge = s%10
shi = s%100//10
bai = s//100
if ge**3+shi**3+bai**3==s:
print(s)
方法三
for ge in range(10):
for shi in range(10):
for bai in range(1,10):
if ge**3+shi**3+bai**3==ge+shi*10+bai*100:
print(ge+shi*10+bai*100)
结果
370
371
153
407
求列表中的最大值和最小值
lists = [2,3,5,1,7,9,3,56,2]
max = lists[0]
min = lists[0]
for i in lists[1:]:
if min>i:
min = i
elif max<i:
max = i
print(max,min)
结果
56 1
斐波那契数列的程序编写
def hello():
a,b = 1,0
for x in range(20):
a,b = b,a+b
yield a
x = hello()
for i in x:
print(i)
用自己的代码实现Strip()的功能
strs = 'ttttttttttttdlgjsdklgjslkgjttttttttt'
def hello(strs,h='all',s=' '):
while h=='l' or h=='all':
if strs[0]==s:
strs = strs[1:]
else:
break
while h=='r' or h=='all':
if strs[-1]==s:
strs=strs[:-1]
else:
break
return strs
print(hello(strs,h='r',s='t'))#通过判断选择去左或者右边的字符,或者全部去除
结果
ttttttttttttdlgjsdklgjslkgj
编写程序对列表中的元素去重
方法一
lists = [1,1,1,2,2,2,3,3,3,3,3,4,4]
n_l = []
for i in lists:
if i not in n_l:
n_l.append(i)
print(n_l)
结果
[1, 2, 3, 4]
方法二
print(list(set(lists))) #利用集合的特性去重
统计列表中每个元素出现的个数
lists = [1,1,1,2,2,2,3,3,3,3,3,4,4]
n_l = []
for i in range(len(lists)):
i = lists[i]
flag = 0
n = 0
for a,b in n_l:
if a==i:
n_l[n] = [i,b+1]
flag = 1
n=n+1
if flag==0:
n_l.append([i, 1])
print(n_l)
结果
[[1, 3], [2, 3], [3, 5], [4, 2]]
九九乘法表
strs = ''
for i in range(1,10):
for ii in range(1,i+1):
strs+='{}x{}={} '.format(ii,i,i*ii)
strs = strs+'\n'
print(strs)
结果
1x1=1
1x2=2 2x2=4
1x3=3 2x3=6 3x3=9
1x4=4 2x4=8 3x4=12 4x4=16
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
选出两个列表中对应下标值最大的元素组成新的列表
方法一
lists = []
list1 = [1,4,3,7,2,4]
list2 = [1,2,4,5,2,8]
for a,b in zip(list1,list2):
n = a if a>b else b
lists.append(n)
print(lists)
结果
[1, 4, 4, 7, 2, 8]
方法二
lists = []
list1 = [1,4,3,7,2,4]
list2 = [1,2,4,5,2,8]
for i in range(len(list1)):
if list1[i] < list2[i]:
lists.append(list2[i])
else:
lists.append(list1[i])
print(lists)
结果
[1, 4, 4, 7, 2, 8]
冒泡排序
lists = [4,2,8,7,9,3,6,2]
for ii in range(len(lists)-1):
for i in range(len(lists)-1):
if lists[i]>lists[i+1]:
lists[i],lists[i+1] = lists[i+1],lists[i]
print(lists)
结果
[2, 2, 3, 4, 6, 7, 8, 9]
将列表中的元素依次打印出来
lists = [[1,2],[3,1],[4,2]]
data = [x for y in lists for x in y]
print(data)
结果
[1, 2, 3, 1, 4, 2]
函数调用args接单个值,kwargs用来接键值对
def hello(ok = 123,*args,**kwargs):#ok是形参
print(ok)
print(123)
return 123,123#返回多个值,以元组的形式返回
print(hello('w'))#'w'是实参
hello('w')
结果
w
123
(123, 123)
w
123
random的用法
import random #引入random
lists = [1,2,3,4,5,6,7,8,9]
random.shuffle(lsits)
print(lsits)
print(random.random())#生成0到1之间的随机小数
print(random.randrange(1,10))#1到9之间随机取值
print(random.sample(lists,4))#s随机取4个值
print(random.randint(1,10))#生成一个指定范围内的整数
print(random.choice(lists))#随机取一个值
定义一个函数,输入名字列表,输入随机数量,提取相应数量的名字,当次提取的名字不能有重复
方法一
此算法存在缺陷,取出的名字根据集合去重,有可能取到少于提取数量的名字个数
def dianming():
name1 = []
name1_dianming = []
while True:
a = input('请输入学员的名字,退出请按q:')
if a == 'q':
break
else:
name1.append(a)
print(name1)
b = int(input('请输入你想提取的名字个数:'))
while True:
for i in range(b):
name1_dianming.append(random.choice(name1[b]))
print(set(name1_dianming))
break
dianming()
方法二
#创造一定数量的名字
lists = ['admin{}'.format(x) for x in range(1,100)]
print(lists)
#random.shuffle方法
def get_random_name(m,num):
random.shuffle(m)
if len(m)>=num:
return m[:num]
else:
return '名字数量不足'
print(get_random_name(lists,99))
#random.choice方法
def get_random_name(m,num):
if len(m) >= num:
lists = set([])
while True:
if len(lists)==num:
break
lists.add(random.choice(m))
return lists
else:
return '名字数量不足'
#random.randrange方法
def get_random_name(m,num):
if len(m) >= num:
lists = set([])
while True:
if len(lists) == num:
break
data = random.randrange(len(m))
m.pop(data)
lists.add(m.pop(data))
return lists
else:
return '名字数量不足'
打印出指定范围内的数字,奇数为负,偶数为正
lists = [x if x %2==0 else -x for x in range(1,10)]
print(lists)
结果
[-1, 2, -3, 4, -5, 6, -7, 8, -9]
打印出指定范围内的偶数之和
def oushuhe(n):
if n == 0:
return 0
else:
if n%2 != 0:
n = n-1
return n + oushuhe(n-2)
else:
return n + oushuhe(n-2)
print(oushuhe(99))
结果
2450
编写程序,将字符串中的数字转为整数
对正整数:
from functools import reduce
def get_num(n):
dicts = {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}
return dicts[n]
def get_result(x,y):
return x * 10 + y
def int1(strs):
data = list(map(get_num,strs))
print(data)
result = reduce(get_result,data)
return result
print(int1('123535563645'))
结果
123535563645
对小数
from functools import reduce
from decimal import Decimal
def get_num(n):
dicts = {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}
return dicts[n]
def get_result(x,y):
return x * 10 + y
def int1(strs):
data = list(map(get_num,strs))
print(data)
result = reduce(get_result,data)
return result
dicts = {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}
strs = '134254321421.24314141542'
data = strs.replace('.','')
dian_index = strs.find('.')
chazhi = 10**(len(data)-dian_index)
print(Decimal(int1(data))/100)
结果
[1, 3, 4, 2, 5, 4, 3, 2, 1, 4, 2, 1, 2, 4, 3, 1, 4, 1, 4, 1, 5, 4, 2]
134254321421243141415.42
#直接用int函数
strs = '18514203544'
print(int(strs),type(int(strs)))
结果
18514203544
杨辉三角
lists = [1]
for i in range(9):
print(lists)
n = []
for i in range(len(lists)-1):
n.append(lists[i]+lists[i+1])
lists = [1] + n + [1]
结果
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
[1, 6, 15, 20, 15, 6, 1]
[1, 7, 21, 35, 35, 21, 7, 1]
[1, 8, 28, 56, 70, 56, 28, 8, 1]
递归函数
#方法一
def get_data(n):
if n%2==1:
n = n-1
if n==0:
return 0
else:
return n+get_data(n-2)
print(get_data(99))
#方法二
print(sum(range(2,99,2)))
结果
2450
map,reduce函数的应用
def get_data(x):
return x**2
def data_add(x,y):
return x+y
#map()函数根据提供的函数对指定的序列做映射
lists = [x for x in range(10)]
print(list(map(get_data,lists)))
#reduce()函数先从列表(或序列)中取出2个元素执行指定函数,并将输出结果与第3个元素传入函数,输出结果再与第4个元素传入函数,以此类推
from functools import reduce
last = reduce(data_add,lists)
print(last)
结果
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
45