Comments (2)
写法不够简洁
from python_interview_question.
写法不够简洁
没明白写法不够简洁是什么意思。。。
这是第一个案例~
def get_lines(fp):
with open(fp,'rb') as f:
for line in f:
yield line
这是第二个案例 用mmap~
from mmap import mmap, ACCESS_READ
def get_lines(fp):
with open(fp,"r") as f:
m = mmap(f.fileno(), 0, access=ACCESS_READ)
tmp = 0
for i, char in enumerate(m):
if char==b"\n":
yield m[tmp:i+1].decode()
tmp = i+1
if tmp<i:
yield m[tmp:i+1].decode()
if __name__=="__main__":
for i in get_lines("fp_some_huge_file"):
print(i)
而且你第一题的代码漏了个单引号~
修改了下 原来的代码把最后一行没有换行符的情况漏了~
from python_interview_question.
Related Issues (20)
- GIL锁不是只有CPython才有 HOT 3
- 22题疑问
- 29题空格的问题
- 想转载怎么联系作者 HOT 3
- 18.反转一个整数,例如-123 --> -321
- 2.补充缺失的代码
- 13.请写出一段python代码实现删除list里面的重复元素? HOT 2
- 67-print c 输出是否有误
- 88一行代码有误,结果多另一个空list,可以更改为:
- @ 28 转换 “123”到123
- 遍历文件,可以考虑pathlib HOT 1
- 一般面试的话写单例最好写出线程安全的模式 HOT 1
- py 面试 HOT 1
- ### 29.Given an array of integers 第一个例子死循环 HOT 1
- ### 29.Given an array of integers 第一个解决方案死循环 HOT 1
- 72 题是错误的 HOT 1
- Python3基本数据类型描述有误 HOT 1
- 1.有一个jsonline格式的文件file.txt大小约为10K HOT 1
- 53题解释不全
- 请写出打印结果和存在的问题
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python_interview_question.