day7

day7
1.把字符串中的所有数字去掉
s = "a1b2c3b4d5dddddd"
def move_num(s):
result = ''
for i in s:
if i not in '1234567890':
result += i
return result
方法2:print(''.join([v for v in s if v.isapha()]))
利用isappha()方法判断字符串是否是字母,如果是字母返回true
方法3:print(''.join(filter(lambda x: x not in '1234567890',s)))
 
2.三个数排序
def sort(a,b,c):
if a>b:
a,b = b,a
if a>c:
a,c = c,a
if b>c:
b,c = c,b
print(a,b,c)
return a,b,c
 
3.求一个列表中最大的值
方法1:冒泡排序,返回最后一个值
def sort(s):
for i in range(len(s)):
for m in range(i+1):
if s[i] < s[m]:
s[i],s[m] = s[m],[i]
return s[-1]
方法2:逐个比较,拿第一个值当做基数,如果后面有值大于基数,就改变基数
def max(s):
max_num = s[0]
for i in s:
if i >max_num:
max_num = i
return max_num
 
4.生成9位数字的密码
import random
password = ''
for i in range(9):
password += str(random.randint(0,9))
 
5.生成9位字母的密码
import string,random
password = ''
for i in range(9):
password += random.choice(string.ascii_letters)
 
6.生成9位数字和字母的密码
import string,random
data_list = []
list1 = list(string.ascii_letters)
list2 = [v for v in range(0,10)]
list1.extend(list2)
for i in range(9):
password = random.choice(list1)

posted @ 2019-12-23 08:18  绝世老中医  阅读(312)  评论(0编辑  收藏  举报