(Python) Counting lines in a huge (>10GB) file as fast as possible [duplicate]

Ignacio’s answer is correct, but might fail if you have a 32 bit process. But maybe it could be useful to read the file block-wise and then count the \n characters in each block. def blocks(files, size=65536): while True: b = files.read(size) if not b: break yield b with open(“file”, “r”) as f: print sum(bl.count(“\n”) … Read more

Does Kotlin have an “enumerate” function like Python?

Iterations in Kotlin: Some Alternatives Like already said, forEachIndexed is a good way to iterate. Alternative 1 The extension function withIndex, defined for Iterable types, can be used in for-each: val ints = arrayListOf(1, 2, 3, 4, 5) for ((i, e) in ints.withIndex()) { println(“$i: $e”) } Alternative 2 The extension property indices is available … Read more

Only index needed: enumerate or (x)range?

I would use enumerate as it’s more generic – eg it will work on iterables and sequences, and the overhead for just returning a reference to an object isn’t that big a deal – while xrange(len(something)) although (to me) more easily readable as your intent – will break on objects with no support for len…

How can I limit iterations of a loop?

How can I limit iterations of a loop in Python? for index, item in enumerate(items): print(item) if index == limit: break Is there a shorter, idiomatic way to write the above? How? Including the index zip stops on the shortest iterable of its arguments. (In contrast with the behavior of zip_longest, which uses the longest … Read more