![]() ![]() You use pipes to pass information forward instead of nested parentheses, and then use your normal python string and list methods.Can one machine consume all available bandwidth? Ex. It's pretty similar to the -c way of executing python, but it imports common modules and has its own preset variable that help with splitting/joining, line counter, etc. Sony's Open Source command-line tool for performing python one-liners using unix-like pipes This script generates a "one-liner" from make's point of view. sub( ' ', ' \\ \ g ', line) 9 # grab leading white space (should be multiples of 4) and makes them into 10 # tabs 11 wh_spc_len = len( re. readlines() 6 print ' \t python2.2 -c " `printf \\ " if 1: \\ n \\ ' 7 for line in lines: 8 line = re. In this one-liner, you’re looking for element 'Alice' in the list lst = so it even works if the element is not in the list (unlike the list.index() method).ġ import sys, re 2 3 def main(): 4 fh = open( sys. Say, you want to do the same as the list.index(element) method but return all indices of the element in the list rather than only a single one. py 18 19 # Power set Python One-Liner 20 lambda l: reduce( lambda z, x: z + for y in z], l, ]) 21 22 # Fibonacci Python One-Liner 23 lambda x: x if x= L]) 27 28 # Sieve of Eratosthenes Python One-liner 29 reduce( ( lambda r, x: r- set( range( x** 2, n, x)) if ( x in r) else r), range( 2, int( n** 0.5)), set( range( 2, n))) find( phrase) 3 4 # Swap Two Variables Python One-Liner 5 a, b = b, a 6 7 # Sum Over Every Other Value Python One-Liner 8 sum( stock_prices) 9 10 # Read File Python One-Liner 11 12 13 # Factorial Python One-Liner 14 reduce( lambda x, y: x * y, range( 1, n+ 1)) 15 16 # Performance Profiling Python One-Liner 17 python - m cProfile foo. Some day, I will add a detailed explanation here - but for now, you can read this blog article to find explanations.ġ # Palindrome Python One-Liner 2 phrase. Thanks for creating this awesome resource, JAM, and RJW!īecause I learned a lot from studying the one-liners, I thought why not revive the page (after almost ten years since the last change happened)?Īfter putting a lot of effort into searching the web for inspiration, I created the following ten one-liners. I visited this page oftentimes and I loved studying the one-liners presented above. Overview: 10 one-liners that fit into a tweet Github '''Python One-Liners''' - Share your own one-liners with the community Python One-Line X - How to accomplish different tasks in a single line Interesting Quora Thread ''Python One-Liner'' So, use your one-liner superpower wisely!įree Python One-Liners Learning Resourcesįree ''Python One-Liners'' videos & book resourcesĬollection of ''One-Liners'' with interactive shell However, if you use well-established one-liner tricks such as list comprehension or the ternary operator, they tend to be Pythonic. As a rule of thumb: if you use one-liners that are confusing, difficult to understand, or to show off your skills, they tend to be Unpythonic. Of course, there is debate on whether one-liners are even Pythonic. The source code is contributed from different Python coders - Thanks to all of them! Special thanks to the early contributor JAM. Please use a "sorted insert" for your new one-liner.) ( Edit: The one-liners are now sorted more or less by ease-of-understanding - from simple to hard. It would be awesome if this page expanded to the point where it needs some sort of organization system. The trick is to think of something that will "do a lot with a little." Most importantly, reading and writing about Python one-liners (e.g., in this post) is a lot of fun! There's even a whole subculture around who can write the shortest code for a given problem. In other languages (think: Java) this would be nearly impossible, but in Python, it's a lot easier to do. You may ask: why should I care? The answer is profound: if you cannot read and write one-liner code snippets, how can you ever hope to read and write more complicated codebases? Python one-liners can be just as powerful as a long and tedious program written in another language designed to do the same thing. This page is devoted to short programs that can perform powerful operations called Python One-Liners. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |