1.读取txt文件
import codecs
result = []
with codecs.open("文件名.txt", "rb", 'utf-8', errors='ignore') as txtfile:
for line in txtfile:
line = line.replace("\r", "")
line = line.replace("\n", "")
result.append(line)
text = ["".join(result)]
print(text)
2.map函数
map就是方便一堆数据都套一个函数的情景 参数为:map(函数,待此函数处理的数据) map直接出来的结果是一个对象,因此需要list()等转换成我们需要的格式。
l1 = list(map(lambda x: x**2, [2, 3, 4]))
print(l1)
运行结果 匿名函数为平方
def add(x, y):
return x+y
l2 = list(map(add, [1, 2, 3], [1, 1, 1]))
print(l2)
运行结果
3.zip函数
用于打包
a = [1, 2, 3]
b = [4, 5, 6, 7]
c = ["A", "B", "C"]
print(list(zip(a, b, c)))
结果
a = [1, 2, 3]
b = [4, 5, 6, 7]
c = ["A", "B", "C"]
for i, j, k in zip(a, b, c):
print(i, j, k)
结果 交换字典键值对
dic = {}
dic["a"] = 1
dic["b"] = 2
dic["c"] = 3
dic2 = dict(zip(dic.values(), dic.keys()))
print(dic)
print(dic2)
结果
|