- 博客(8)
- 收藏
- 关注
原创 【Python3 练习题_08】文件处理(常用函数)
file.read([size]) # 默认读全部file.readline() #读取整行,包括\nfile.readlines() #读取所有行并返回列表file.seek(offset[, whence]) # 设置文件当前位置file.tell() # 返回文件当前位置file.writelines(sequence) # 写入序列字符串列表,如需换行需自己加...
2019-01-15 16:40:06
191
原创 【Python3 练习题_07】文件处理(open的使用)
文件处理# 打开文件(当前目录下的file.txt文件)fobg = open("./file.txt", )'''关于open 模式:r 以只读模式打开(默认)w 以写方式打开a 以追加模式打开 (从 EOF 开始, 必要时创建新文件)r+ 以读写模式打开,可读可写,文件不存在则报错,内容覆盖w+ 以读写模式打开,可读可写,文件不存...
2019-01-15 16:33:55
396
原创 【Python3 练习题_06】验证字符串是否是回文
通过反转字符串来比较#!/usr/bin/env python3s = input("Enter the string: ")z = s[::-1]if s == z: print("Yes, it's a ")else: print("no")
2018-12-12 11:05:56
368
原创 【python3 练习题_05】遍历序列(列表、字符串、元组)、字典
遍历两个序列(字符串、列表、元组):zip(序列a, 序列b)#!/usr/bin/env python3keys = ["Key1", "Key2", "Key3"]values = ["Value1", "Value2", "Value3"]for x,y in zip(keys,values): print("{} 对应 {}".format(x,
2018-12-11 17:52:24
662
原创 【Python3 练习题_04】抽取字符串中的数字重新组合并打印
# 打印为字符串file = open("./String.txt")content = file.read()digit_str = ""for i in content: if i.isdigit(): digit_str += iprint(digit_str)file.close()# 打印为列表file = open("./S
2018-12-11 17:28:03
355
原创 【Python3 练习题_03】列表推导式的应用(二维列表赋值)
# n维列表n = int(input("Enter the values of n: ")) list_n = []for i in range(n): list_n.append([int(x) for x in input().split()])print(list_n)运行结果:Enter the values of n: 32 3 4 53 4 5 64 5...
2018-12-11 16:20:12
1011
原创 【Python3 练习题_01】元素去重(列表、集合)
1–常规方式:list1 = [1,1,2,3,4,5,6,7,7]new_list = []for element in list1: if element not in new_list: new_list.append(element)print(new_list)2–利用集合特性:list1 = [1,1,2,3,4,5,6,7,7]set1 = s...
2018-12-10 17:47:36
340
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人