替换敏感字符,统计,乘法表,函数

2019-04-11 10:41:41来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

#!/usr/bin/env python
# -*- coding:utf-8 -*-

# 1.替换敏感字符
# keys = ['苍老师', '小泽老师', 'alex']
# with open('file.txt', mode='r', encoding='utf-8') as f1, open('2.txt', mode='w', encoding='utf-8') as f2:
# for line in f1:
# for a in keys:
# if a in line:
# line = line.replace(a, '***')
# f2.write(line)

# 2.统计各省车牌数
# cars = ['鲁A3244','鲁B12333','京B8989M','黑C49678','黑C46555','沪B25041','黑C34567']
# dic = {}
# for i in cars:
# a = i[0]
# if a in list(dic.keys()):
# dic[a] += 1
# else:
# dic[a] = 1
# print(dic)

# 3.数据添加到字典.
# info = []
# with open('data.txt',mode='r',encoding='utf-8') as f1:
# a = f1.readline()
# for lines in f1:
# dic = {}
# i = lines.split(',')
# count = 0
# for k in a.split(','):
# dic[k.strip()] = i[count].strip()
# count += 1
# info.append(dic)
# print(info)

# 4.小九九乘法表
# for a in range(1,10):
# for b in range(1,a+1):
# if a == b:
# print(str(a) + '*' + str(b))
# else:
# print(str(a)+'*'+str(b),end=' ')

# 5.读取文件,将文件的内容构造成指定格式的数据,并返回。
# """
# a.log文件
# alex|123|18
# eric|uiuf|19
# ...
# 目标结构:
# a. ["alex|123|18","eric|uiuf|19"] 并返回。
# b. [['alex','123','18'],['eric','uiuf','19']]
# c. [
# {'name':'alex','pwd':'123','age':'18'},
# {'name':'eric','pwd':'uiuf','age':'19'},
# ]

'''
def func1():
lst = []
with open('a.log', mode='r', encoding='utf-8') as f:
for line in f:
lst.append(line.strip())
return lst

print(func1())
'''

'''
def func2():
lst = []
with open('a.log', mode='r', encoding='utf-8') as f:
for line in f:
a = line.strip().split('|')
lst.append(a)
return lst
'''

'''
def func3():
lst = []
with open('a.log', mode='r', encoding='utf-8') as f:
for line in f:
dic = {}
a, b, c = line.strip().split('|')
dic['name'] = a
dic['pwd'] = b
dic['age'] = c
lst.append(dic)
return lst
print(func3())
'''


# 6.不用max和min找出最大最小值.
'''
def func(*args):
ma = args[0]
mi = args[0]
for i in args:
if i > ma:
ma = i
if i < mi:
mi = i
return {'最大值':ma,'最小值':mi}
print(func(1,6,8,7,1,2,4))
'''

# 传入n,返回n的阶乘.
'''
def func(n):
res = 1
for i in range(1, n+1):
res *= i
return res

print(func(7))
'''







原文链接:https://www.cnblogs.com/zjx1/p/10676469.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:ORM初探(一)

下一篇:类与对象2