comprehension

How do I efficiently filter computed values within a Python list comprehension?

The Python list comprehension syntax makes it easy to filter values within a comprehension. For example: result = [x**2 for x in mylist if type(x) is int] Will return a list of the squares of integers in mylist. However, what if the test involves some (costly) computation and you want to filter on the result? One option is: result...

C++ source code comprehension tools

I'm starting work on a huge C++ codebase, and was wondering if someone could suggest good source code comprehension tools. I usually use doxygen but was curious to see if anything better existed. Thanks. ...

Comprehension for flattening a sequence of sequences?

If I have sequence of sequences (maybe a list of tuples) I can use itertools.chain() to flatten it. But sometimes I feel like I would rather write it as a comprehension. I just can't figure out how to do it. Here's a very construed case: Let's say I want to swap the elements of every pair in a sequence. I use a string as a sequence here...

One liner to replicate lines coming from a file (Python)

I have a regular list comprehension to load all lines of a file in a list f = open('file') try: self._raw = [L.rstrip('\n') for L in f] finally: f.close() Now I'd like to insert in the list each line 'n' times on the fly. How to do it inside the list comprehension ? Tnx ...

List comprehension python

What is the equivalent list comprehension in python of the following Common Lisp code: (loop for x = input then (if (evenp x) (/ x 2) (+1 (* 3 x))) collect x until (= x 1)) ...

Erlang list comprehension with two lists in sequence?

Is it possible to use list comprehension on two lists, item by item, in sequence? Given A = [1,2,3], B = [4,5,6], get some C = [f(1, 4), f(2, 5), f(3, 6)]. In other words, a more direct/efficient way to do [f(U, V) || {U, V} = lists:zip(A, B)]. Similar question goes to binaries, if given A = <<1,2,3>> and B = <<4,5,6>>. This would be ve...

Please help with a dictionary comprehension in LINQ, C#.

Python's equivalent of what I want is: >>> #C#: Dictionary<int, string> tempDict = ... >>> tempDict = {i : str(i) for i in range(200000)} >>> tempDict[5] '5' >>> The example is a bit simplified, but I can modify it myself; do not want to bother you with details of proprietary classes. Got it: var y = (from x in Enumerable.Range(0, ...

Apply function to one element of a list in Python

I'm looking for a concise and functional style way to apply a function to one element of a tuple and return the new tuple, in Python. For example, for the following input: inp = ("hello", "my", "friend") I would like to be able to get the following output: out = ("hello", "MY", "friend") I came up with two solutions which I'm not ...

Python list comprehension to return edge values of a list

If I have a list in python such as: stuff = [1, 2, 3, 4, 5, 6, 7, 8, 9] with length n (in this case 9) and I am interested in creating lists of length n/2 (in this case 4). I want all possible sets of n/2 values in the original list, for example: [1, 2, 3, 4], [2, 3, 4, 5], ..., [9, 1, 2, 3] is there some list comprehension c...

How do you solve this Haskell problem?

I want to define a function replicate to replicate a list of numbers by its value using only list comprehension, for example: replicate [5,1,3,2,8,1,2] output: [5,5,5,5,5,1,3,3,3,2,2,8,8,8,8,8,8,8,8,1,2,2] I know this would be easy to use the 'replicate' built in function but only list comprehension is allow, how can I do this? THAN...

Why is it still so hard to write software?

Writing software, I find, is composed of two parts: the Idea, and the Implementation. The Idea is about thinking: "I have this problem; how do I solve it?" and further, "how do I solve it elegantly?" The answers to these questions are obtainable by thinking about algorithms and architecture. The ideas come partially through analysis ...

Understanding code

What is the best way to get acquainted with C# codebase of approximate size 200K LOC? Are there any tools available? http://www.program-comprehension.org/ It seems there is an event going for a long time for this purpose. Thanks. ...

Program comprehension strategies

If you were assigned to a very large project, with sparse documentation/comments and little access to previous developers, and tasked with fixing some tricky little bugs, how would you go about: Learning how the project works Locating the source/cause of the bugs Fixing them, hopefully without causing (too many) new problems I know i...

Create List of Single Item Repeated n Times in Python

I know a list comprehension will do this, but I was wondering if there is an even shorter (and more Pythonic?) approach. I want to create a series of lists, all of varying length. Each list will contain the same element e, repeated n times (where n = length of the list). How do I create the lists, without doing [e for number in xrange(...

Set comprehensions don't work on Pydev (Python)

{x for x in range(10)} works perfectly on IDLE, but when I try this in eclipse (with Pydev plugin) I get a syntax error: Undefined variable: x Is it because Pydev doesn't support set comprehensions or something? What can I do to make this work? (This was just one example that doesn't work. All set comprehensions don't work for me...

how to use python list comprehensions replace the function invoke inside of "for" stmt?

Sorry, I just found the id = [conn.cursor() for x in range(100) ] also works, so my concern will not be a problem anymore. Thanks for all of your answer, all of you are really fast. ===================== All, id = [(conn.cursor(),x) for x in range(100) ] >>> id [(<sqlite3.Cursor object at 0x01D14DA0>, 0), (<sqlite3.Cursor object ...

Python - List Mapping

Hey, I have several lists that I would like to map together, but I can't quite work my head around how to do it. I am scraping a live feed of Horse Racing results. The feed only lists the course/time once and three horses and their positions (top three) OR four horses and blank (i.e. "") positions IF the race was abandoned. These are t...