r/Python • u/JohnettaHarned • Sep 22 '15
Most Frequent Python Problems and Solution
http://www.pamno.com/b/most-frequent-python-problems-and-solution-cm578/7
u/A_History_of_Silence Sep 22 '15
How has anyone upvoted this? Pure spam, completely inaccurate information throughout.
3
u/brombaer3000 Sep 23 '15
Agreed. Not only is this the wrong place to post this, the code isn't even syntactically correct (the second line of code misses parentheses etc.).
3
u/TheBlackCat13 Sep 22 '15
How do you split a list into evenly sized chunks ?
I think the itertools grouper recipe is easier:
def grouper(iterable, n, fillvalue=None):
args = [iter(iterable)] * n
return zip_longest(*args, fillvalue=fillvalue)Matrix multiplication in Python?
I think the itertools dotproduct recipe is much easier:
def dotproduct(vec1, vec2):
return sum(map(operator.mul, vec1, vec2))1
u/kdelok Sep 22 '15
The first one you mention is the answer I received on StackOverflow when I asked that very question.
The second one doesn't do matrix multiplication though, since the result should be a matrix itself. That's why they end up with the nested list comprehension (I haven't checked it though). I think it's easier to just use
numpy.
2
Sep 22 '15 edited Sep 24 '15
Binary search is implemented in the bisect module
bisect.bisect_left and bisect.bisect_right give insertion points for a value that will keep a list sorted. Accessing the index returned by bisect_left is equivalent to binary searching.
10
u/dl__ Sep 22 '15
The 11th most frequent python problem... listing primes? Listing primes is something I've never had to do in 30 years of programming.