list-comprehension

Advanced Python list comprehension

Given two lists: chars = ['ab', 'bc', 'ca'] words = ['abc', 'bca', 'dac', 'dbc', 'cba'] how can you use list comprehensions to generate a filtered list of words by the following condition: given that each word is of length n and chars is of length n as well, the filtered list should include only words that each i-th character is in th...

Tips for debugging list comprehensions?

Python list comprehensions are nice, but near impossible to debug. You guys have any good tips / tools for debugging them? ...

Nested generator functions in python

Consider a tuple v = (a,b,c) and a generator function generate(x) which receives an item from the tuple and generates several options for each item. What is the pythonic way of generating a set of all the possible combinations of the result of generate(x) on each item in the tuple? I could do this: v = (a,b,c) for d in generate(v[0]):...

Python beginner confused by a complex line of code

I understand the gist of the code, that it forms permutations; however, I was wondering if someone could explain exactly what is going on in the return statement. def perm(l): sz = len(l) print (l) if sz <= 1: print ('sz <= 1') return [l] return [p[:i]+[l[0]]+p[i:] for i in range(sz) for p in perm(l[1:])]...

Python 3: unpack inner lists in list comprehension

I'm running the following code on a list of strings to return a list of its words: words = [re.split('\\s+', line) for line in lines] However, I end up getting something like: [['import', 're', ''], ['', ''], ['def', 'word_count(filename):', ''], ...] As opposed to the desired: ['import', 're', '', '', '', 'def', 'word_count(filen...

Help with python list-comprehension

A simplified version of my problem: I have a list comprehension that i use to set bitflags on a two dimensional list so: s = FLAG1 | FLAG2 | FLAG3 [[c.set_state(s) for c in row] for row in self.__map] All set_state does is: self.state |= f This works fine but I have to have this function "set_state" in every cell in __map. Every c...

What do you do when you feel you need a variadic list comprehension?

I would like to make a method where I could give it a list of lengths and it would return all combinations of cartesian coordinates up to those lengths. Easier to explain with an example: cart [2,5] Prelude> [ [0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4] ] cart [2,2,2] Prelude> [ [0,0,0],[0,0,1],[0,1,0],[0,1,1],[1,0,0],[...

Recursive list comprehension in Python?

Is it possible to define a recursive list comprehension in Python? Possibly a simplistic example, but something along the lines of: nums = [1, 1, 2, 2, 3, 3, 4, 4] willThisWork = [x for x in nums if x not in self] # self being the current comprehension Is anything like this possible? ...

filtering elements from list of lists in Python?

I want to filter elements from a list of lists, and iterate over the elements of each element using a lambda. For example, given the list: a = [[1,2,3],[4,5,6]] suppose that I want to keep only elements where the sum of the list is greater than N. I tried writing: filter(lambda x, y, z: x + y + z >= N, a) but I get the error: <l...

Python: cleaner list comprehension

Is there a cleaner way to write this: for w in [w for w in words if w != '']: I want to loop over a dictionary words, but only words that != ''. Thanks! ...

Paginating requests to an API

I'm consuming (via urllib/urllib2) an API that returns XML results. The API always returns the total_hit_count for my query, but only allows me to retrieve results in batches of, say, 100 or 1000. The API stipulates I need to specify a start_pos and end_pos for offsetting this, in order to walk through the results. Say the urllib reques...

Getting the indices of all non-None items from a sub-list in Python?

As per the title, I have a nested lists like so (the nested list is a fixed length): # ID, Name, Value list1 = [[ 1, "foo", 10], [ 2, "bar", None], [ 3, "fizz", 57], [ 4, "buzz", None]] I'd like to return a list (the number of items equal to the length of a sub-list from list1), where the sub-...

List comprehension from multiple sources in Python?

Is it possible to replace the following with a list comprehension? res = [] for a, _, c in myList: for i in c: res.append((a, i)) For example: # Input myList = [("Foo", None, [1, 2, 3]), ("Bar", None, ["i", "j"])] # Output res = [("Foo", 1), ("Foo", 2), ("Foo", 3), ("Bar", "i"), ("Bar", "j")] ...

List comprehension for series of deltas

How would you write a list comprehension in python to generate a series of n-1 deltas between n items in an ordered list? Example: L = [5,9,2,1,7] RES = [5-9,9-2,2-1,1-7] = [4,7,1,6] # absolute values ...

Excel CSV into Nested Dictionary; List Comprehensions

heya, I have a Excel CSV files with employee records in them. Something like this: mail,first_name,surname,employee_id,manager_id,telephone_number [email protected],john,smith,503422,503423,+65(2)3423-2433 [email protected],george,brown,503097,503098,+65(2)3423-9782 .... I'm using DictReader to put this into a nested dictionary: import csv g...

Python: Repeat elements in a list comprehension?

I have the following list comprehension which returns a list of coordinate objects for each location. coordinate_list = [Coordinates(location.latitude, location.longitude) for location in locations] This works. Now suppose the location object has a number_of_times member. I want a list comprehension to generate n...

In Python, is it better to use list comprehensions or for-each loops?

Which of the following is better to use and why? Method 1: for k, v in os.environ.items(): print "%s=%s" % (k, v) Method 2: print "\n".join(["%s=%s" % (k, v) for k,v in os.environ.items()]) I tend to lead towards the first as more understandable, but that might just be because I'm new to Python and list comprehensions a...

Show me some cool python list comprehensions

One of the major strengths of python and a few other (functional) programming languages are the list comprehension. They allow programmers to write complex expressions in 1 line. They may be confusing at first but if one gets used to the syntax, it is much better than nested complicated for loops. With that said, please share with me ...

Counting positive elements in a list with Python list comprehensions

I have a list of integers and I need to count how many of them are > 0. I'm currently doing it with a list comprehension that looks like this: sum([1 for x in frequencies if x > 0]) It seems like a decent comprehension but I don't really like the "1"; it seems like a bit of a magic number. Is there a more Pythonish way to do this? ...

Python: Improving long cumulative sum

I have a program that operates on a large set of experimental data. The data is stored as a list of objects that are instances of a class with the following attributes: time_point - the time of the sample cluster - the name of the cluster of nodes from which the sample was taken node - the name of the node from which the sample was ...