第7次全天课笔记-20180826
首先做10道题目
1 随机生成一个1-10的整数,然后你输入一个值去比对,如果大了,打印大了,小了打印小了,等于则打印。
2 基于第一题,限定一下猜的次数不超过3次
import random rand_num = random.randint(1,10) for i in range(3): num = int(input("请输入一个数字:")) if num > rand_num : print ("大了!") elif num == rand_num: print ("等于" , num) break else: print ("小了") print ("生成的随机数是:",rand_num)
3 基于第二题,打印一下一共猜了多少次。
import random rand_num = random.randint(1,10) guess_times = 0 for i in range(3): num = int(input("请输入一个数字:")) guess_times+=1 if num > rand_num : print ("大了!") elif num == rand_num: print ("等于" , num) break else: print ("小了") print ("生成的随机数是:",rand_num) print ("猜的次数是:",guess_times)
4 生成一个列表["1a","2b","3c","4d","5e"]
result = [] for i in range(1,6): result.append(str(i)+chr(97+i-1)) print (result)
5 生成一个列表["z1","y2","x3","w4","v5"]
result = [] for i in range(1,6): result.append(chr(122-i+1)+str(i)) print (result)
6 将一个字符串的奇数坐标的字母拼成一个字符串。
s = "abcdefghig" result = [] for i in range(len(s)): if i % 2 == 1: result.append(s[i]) print ("".join(result))
7 将一个字符串首字母、最后一个字母和中间字母,三个字符串拼成一个字符串。
s = "abcdefghi" result = [] result.append(s[0]) middle_position = int(len(s)/2) if len(s) % 2 ==1: result.append(s[middle_position]) else: result.append(s[middle_position-1]) result.append(s[middle_position]) result.append(s[-1]) print(result)
8 将一个列表[1,2,3,4,5]每个元素值扩大10倍后,在每个元素后面加上“abc”三个字母放到列表里面。
result = [] for i in range(1,6): result.append(str(i*10)+"abc") print(result)
9 两个列表[1,2,3,4,5],["a","b","c","d","e"],讲两个列表元素拼成一个字典,第一个列表元素做key,第二个做value
result = {} list1= [1,2,3,4,5] list2=["a","b","c","d","e"] for i in range(5): result[list1[i]] = list2[i] print (result)
10 一个字典{1:"a",2:"b",3:"c"},拼成一个列表[1,"a",2,"b",3,"c"]
d = {1:"a",2:"b",3:"c"} result = [] for k,v in d.items(): result.append(k) result.append(v)
切片赋值的测试,比较灵活
>>> s = "a"
>>> s[5:]
''
>>> s[5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>>
>>> a = []
>>> a.append(1)
>>> a
[1]
>>> a[0:1]=[1,2,3,4]
>>> a
[1, 2, 3, 4]
>>> a[0:2]=[1,2,3,4]
>>>
>>> a = [1]
>>> a = [1,2]
>>> a[0:2]=[1,2,3,4]
>>> a
[1, 2, 3, 4]
>>> a[0:5] = [1,2,3,4]
>>> a
[1, 2, 3, 4]
将s = "I am a boy" 换成s = "I am a m"
方法1:
s = "I am a boy" replace_letter = 'm' letter_list =list(s) for index in range(len(letter_list)): if s[index:index+3] == "boy": letter_list[index:index+3]= 'm' print ("".join(letter_list))
方法2:
s = "I am a boy" replace_letter = 'm' letter_list =list(s) for index in range(len(letter_list)): if "".join(letter_list[index:index+3]) == "boy": letter_list[index:index+3]= 'm' print ("".join(letter_list))
正式开讲:
递归
def fib(n): if n <=0 or not isinstance(n,int): return [] result = [] a1=1 a2 =1 if n ==1: result.append(a1) return result if n ==2: result.append(a1) result.append(a2) return result result =[1,1] for i in range(n-2): temp = a1+a2 result.append(temp) a1=a2 a2=temp return result
print(fib(1))
print(fib(2))
print(fib(3))
print(fib(4))
print(fib(5))
def fib(n): result = [] if n <= 0 or not isinstance(n,int): return result for i in range(n): if i < 2: result.append(1) else: result.append(result[i-1]+result[i-2]) return result
代码大全2,异类
将[1,2,[3,4,[5,6,7,[8,9,[10,11]]]]] 改为 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
def iter_list(l): for i in l: if isinstance(i,list): iter_list(i) else: print (i) a = [1,2,[3,4,[5,6,7,[8,9,[10,11]]]]] print(iter_list(a))
将result 做成列表,result需要作为参数传入
def iter_list(l,result): for i in l: if isinstance(i,list): iter_list(i,result) else: result.append(i) return result a = [1,2,[3,4,[5,6,7,[8,9,[10,11]]]]] result = [] print (iter_list(a,result))
这样也可以,但是不推荐这样,不是函数内部的变量
list_new=[]
def iter_list(l):
for i in l:
if isinstance(i,list):
iter_list(i)
else:
list_new.append(i)
return list_new
辗转相除法
def gain(small, big): if small > big: small, big = big, small if small == 0: return big return gain(small, big % small)
吴老师的想法:
def greatest_common_divisor(a,b,c=0,result=[]): if c ==0: if a<b: c=a else: c=b if a%c ==0 and b%c ==0: result.append(c) else: greatest_common_divisor(a,b,c-1) return result[-1] print(greatest_common_divisor(10,8)) print(greatest_common_divisor(10,5))
exec和eval函数 (exec没有返回值,eval有返回值)
>>> a=eval("2*4")
>>> a
8
>>> a=exec("2*4")
>>> a
函数的引用:
>>> abs(-1)
1
>>> a=abs
>>> type(a)
<class 'builtin_function_or_method'>
>>> a(-1)
1
>>> abs(-1)
1
>>> def func(a):
... return a(-1)
...
>>> func(abs)
1
参数列表的分拆
>>> def add(a,b):return a+b
...
>>> add(*(1,2)) 或 add(*[1,2])
3
>>> def sub(a,b):return a-b
...
>>> sub(**{"a":2,"b":1})
1
def draw_triangle(n):
print ("*")
for i in range(n-2):
print("*"+" "*i+"*")
print ("*"*((n-2)*3))
draw_triangle(10)
显示有哪些类型,然后显示每种类型有几种
l = [1,2,"s",[1,23],{1:2},(1,2),set([1,2]),"b"] d = {"int":0,"str":0,"list":0,"tuple":0,"set":0,"dict":0} for i in l: if isinstance(i,str): d["str"]+=1 if isinstance(i,int): d["int"]+=1 if isinstance(i,list): d["list"]+=1 if isinstance(i,set): d["set"]+=1 if isinstance(i,tuple): d["tuple"]+=1 if isinstance(i,dict): d["dict"]+=1 print(d)
把列表中所有的数字求和,字符串数字也要加上。
a = [1,2,3,[4,5,6],{1:6,2:8,"a":"12"}] sum = 0 for i in a: if isinstance(i,int): sum+=i if isinstance(i,list): for j in i: sum+=j if isinstance(i,dict): for k in i.keys(): if isinstance(k,int): sum+= k elif isinstance(k,str): if k.isdigit(): print(k) sum+= int(k) for v in i.values(): if isinstance(v,int): sum+= v elif isinstance(v,str): if v.isdigit(): print(v) sum+= int(v) print(sum)