diff --git a/README.md b/README.md index 276e67b..d9edb8d 100644 --- a/README.md +++ b/README.md @@ -276,7 +276,7 @@ # Python基础 ## 1.1 有一个jsonline格式的文件file.txt 大小约为10K -``` +```python def get_lines(): with open('file.txt','rb') as f: return f.readlines() @@ -286,14 +286,14 @@ process(e) #处理每一行数据 ``` 现在要处理一个大小为10G的文件,但是内存只有4G,如果在只修改get_lines 函数而其他代码保持不变的情况下,应该如何实现?需要考虑的问题都有那些? -``` +```python def get_lines(): with open('file.txt','rb') as f: for i in f: yield i ``` Pandaaaa906提供的方法 -``` +```python from mmap import mmap @@ -313,7 +313,7 @@ if __name__=="__main__": 要考虑的问题有:内存只有4G无法一次性读入10G文件,需要分批读入分批读入数据要记录每次读入数据的位置。分批每次读取数据的大小,太小会在读取操作花费过多时间。 https://stackoverflow.com/questions/30294146/python-fastest-way-to-process-large-file ## 1.2 补充缺失的代码 -``` +```python def print_directory_contents(sPath): """ 这个函数接收文件夹的名称作为输入参数 @@ -330,7 +330,7 @@ https://stackoverflow.com/questions/30294146/python-fastest-way-to-process-large ``` # 模块与包 ## 2.1 输入日期, 判断这一天是这一年的第几天? -``` +```python import datetime def dayofyear(): year = input("请输入年份: ") @@ -341,7 +341,7 @@ https://stackoverflow.com/questions/30294146/python-fastest-way-to-process-large return (date1-date2).days+1 ``` ## 2.2 打乱一个排好序的list对象alist? -``` +```python import random alist = [1,2,3,4,5] random.shuffle(alist) @@ -349,19 +349,19 @@ https://stackoverflow.com/questions/30294146/python-fastest-way-to-process-large ``` # 数据类型 ## 3.1 现有字典 d= {'a':24,'g':52,'i':12,'k':33}请按value值进行排序? -``` +```python sorted(d.items(),key=lambda x:x[1]) ``` ## 3.2 字典推导式 -``` +```python d = {key:value for (key,value) in iterable} ``` ## 3.3 请反转字符串 "aStr"? -``` +```python print("aStr"[::-1]) ``` ## 3.4 将字符串 "k:1 |k1:2|k2:3|k3:4",处理成字典 {k:1,k1:2,...} -``` +```python str1 = "k:1|k1:2|k2:3|k3:4" def str2dict(str1): dict1 = {} @@ -371,23 +371,23 @@ https://stackoverflow.com/questions/30294146/python-fastest-way-to-process-large return dict1 ``` ## 3.5 请按alist中元素的age由大到小排序 -``` +```python alist = [{'name':'a','age':20},{'name':'b','age':30},{'name':'c','age':25}] def sort_by_age(list1): return sorted(alist,key=lambda x:x['age'],reverse=True) ``` ## 3.6 下面代码的输出结果将是什么? -``` +```python list = ['a','b','c','d','e'] print(list[10:]) ``` 代码将输出[],不会产生IndexError错误,就像所期望的那样,尝试用超出成员的个数的index来获取某个列表的成员。例如,尝试获取list[10]和之后的成员,会导致IndexError。然而,尝试获取列表的切片,开始的index超过了成员个数不会产生IndexError,而是仅仅返回一个空列表。这成为特别让人恶心的疑难杂症,因为运行的时候没有错误产生,导致Bug很难被追踪到。 ## 3.7 写一个列表生成式,产生一个公差为11的等差数列 -``` +```python print([x*11 for x in range(10)]) ``` ## 3.8 给定两个列表,怎么找出他们相同的元素和不同的元素? -``` +```python list1 = [1,2,3] list2 = [3,4,5] set1 = set(list1) @@ -396,26 +396,26 @@ https://stackoverflow.com/questions/30294146/python-fastest-way-to-process-large print(set1 ^ set2) ``` ## 3.9 请写出一段python代码实现删除list里面的重复元素? -``` +```python l1 = ['b','c','d','c','a','a'] l2 = list(set(l1)) print(l2) ``` 用list类的sort方法: -``` +```python l1 = ['b','c','d','c','a','a'] l2 = list(set(l1)) l2.sort(key=l1.index) print(l2) ``` 也可以这样写: -``` +```python l1 = ['b','c','d','c','a','a'] l2 = sorted(set(l1),key=l1.index) print(l2) ``` 也可以用遍历: -``` +```python l1 = ['b','c','d','c','a','a'] l2 = [] for i in l1: @@ -424,7 +424,7 @@ https://stackoverflow.com/questions/30294146/python-fastest-way-to-process-large print(l2) ``` ## 3.10 给定两个list A,B ,请用找出A,B中相同与不同的元素 -``` +```python A,B 中相同元素: print(set(A)&set(B)) A,B 中不同元素: print(set(A)^set(B)) ``` @@ -442,7 +442,7 @@ c. 字典 dict 、 集合 set ## 4.3 python如何实现单例模式?请写出两种实现方式? 第一种方法:使用装饰器 -``` +```python def singleton(cls): instances = {} def wrapper(*args, **kwargs): @@ -459,7 +459,7 @@ c. 字典 dict 、 集合 set ``` 第二种方法:使用基类 New 是真正创建实例对象的方法,所以重写基类的new 方法,以此保证创建对象的时候只生成一个实例 -``` +```python class Singleton(object): def __new__(cls,*args,**kwargs): if not hasattr(cls,'_instance'): @@ -475,14 +475,14 @@ New 是真正创建实例对象的方法,所以重写基类的new 方法,以 print foo1 is foo2 #True ``` 第三种方法:元类,元类是用于创建类对象的类,类对象创建实例对象时一定要调用call方法,因此在调用call时候保证始终只创建一个实例即可,type是python的元类 -``` +```python class Singleton(type): def __call__(cls,*args,**kwargs): if not hasattr(cls,'_instance'): cls._instance = super(Singleton,cls).__call__(*args,**kwargs) return cls._instance ``` -``` +```python class Foo(object): __metaclass__ = Singleton @@ -492,7 +492,7 @@ New 是真正创建实例对象的方法,所以重写基类的new 方法,以 ``` ## 4.4 反转一个整数,例如-123 --> -321 -``` +```python class Solution(object): def reverse(self,x): if -10