No flaming please, asking as a community wiki so nobody gets reputation here.
I know python is not the first language to have list comprehension.
I'm just interest in the history of the name.
I'm particularly interested in why it's called comprehension
...
EDIT: Clarified the question a bit
How can I get a string from a dictionary with the format
key1 = value1
key2 = value2
in a relatively fast way ? (relative to plain concatenation)
...
What is a way to implement similar functionality in Haskell of List comprehensions with guards in F#
for example:
factors :: Int -> [Int]
factors = [x | x <-[1 .. n], n 'mod' x == 0]
factors 15
[1,3,5,15]
and
posInt :: Int -> [Int]
posInt = [n | n > 0]
posInt 5
[5]
posInt 0
[]
...
I'm a python newb and am having trouble groking nested list comprehensions. I'm trying to write some code to read in a file and construct a list for each character for each line.
so if the file contains
xxxcd
cdcdjkhjasld
asdasdxasda
The resulting list would be:
[
['x','x','x','c','d']
['c','d','c','d','j','k','h','j','a','s','l',...
I probably didn't ask correctly: I would like a list value that can match any list: the "inverse" of (None,)
but even with (None,) it will match item as None (which I don't want)
The point is I have a function working with: [x for x in my_list if x[field] not in filter_list]
and I would like to filter everything or nothing without maki...
I got bored during the holiday season this year and randomly decided to write a simple list comprehension/filtering library for Java (I know there are some great ones out there, I just wanted to write it my self for the hell of it).
For this list:
LinkedList<Person> list = new LinkedList<Person>();
list.add(new Person("Jac...
Hi, I'm a Matlab user needing to use Python for some things, I would really appreciate it if someone can help me out with Python syntax:
(1) Is it true that lists can be indexed by tuples in Python? If so, how do I do this? For example, I would like to use that to represent a matrix of data.
(2) Assuming I can use a list indexed by t...
If have a list of dictionary items like so:
L = [{"a":1, "b":0}, {"a":3, "b":1}...]
I would like to split these entries based upon the value of "b", either 0 or 1.
A(b=0) = [{"a":1, "b":1}, ....]
B(b=1) = [{"a":3, "b":2}, .....]
I am comfortable with using simple list comprehensions, and i am currently looping through the list L t...
Why is print(x) here not valid (SyntaxError) in the following list-comprehension?
my_list=[1,2,3]
[print(my_item) for my_item in my_list]
To contrast - the following doesn't give a syntax error:
def my_func(x):
print(x)
[my_func(my_item) for my_item in my_list]
...
I was trying to find the quickest way to count the number of items in a list matching a specific filter.
In this case, finding how many odd numbers there are in a list.
While doing this, I was surprised by the results of comparing a list comprehension vs the equivalent generator expression:
python -m timeit -s "L = xrange(1000000)" "su...
Using Python, I'm trying to convert a sentence of words into a flat list of all distinct letters in that sentence.
Here's my current code:
words = 'She sells seashells by the seashore'
ltr = []
# Convert the string that is "words" to a list of its component words
word_list = [x.strip().lower() for x in words.split(' ')]
# Now conver...
I have a nested dictionary of people and item ratings, with people as the key. people may or may not share items.
Example:
{
'Bob' : {'item1':3, 'item2':8, 'item3':6},
'Jim' : {'item1':6, 'item4':7},
'Amy' : {'item1':6,'item2':5,'item3':9,'item4':2}
}
I'm looking for the simplest way to flip these relations, and have a new nested d...
I've started working on some Project Euler problems, and have solved number 4 with a simple brute force solution:
def mprods(a,b):
c = range(a,b)
f = []
for d in c:
for e in c:
f.append(d*e)
return f
max([z for z in mprods(100,1000) if str(z)==(''.join([str(z)[-i] for i in range(1,len(str(z))+1)]))])
After solving, I tried t...
I am curious if it is possible to dynamically build a list comprehension in Haskell.
As an example, if I have the following:
all_pows (a,a') (b,b') = [ a^y * b^z | y <- take a' [0..], z <- take b' [0..] ]
I get what I am after
*Main> List.sort $ all_pows (2,3) (5,3)
[1,2,4,5,10,20,25,50,100]
However, what I'd really like is to ha...
I'm trying to build a small testing app with erlang+mnesia.
I have a user table build from the #user record, as defined here:
-record(user_details, {name, password}).
-record(user, {id, details}).
then I insert a user with that function:
add_sample_data() ->
Mat = #user{
details = #user_details{
name = "mat", password ...
I have a list of users in local store that I need to update from a remote list of users every once in a while. Basically:
If a remote user already exists locally, update its fields.
If a remote user doesn't already exist locally, add the user.
If a local user doesn't appear in the remote list, deactivate or delete.
If a local user also...
I have a function to pick out lumps from a list of strings and return them as another list:
def filterPick(lines,regex):
result = []
for l in lines:
match = re.search(regex,l)
if match:
result += [match.group(1)]
return result
Is there a way to reformulate this as a list comprehension? Obviously...
I need to generate a set of coordinates in Erlang. Given one coordinate, say (x,y) I need to generate (x-1, y-1), (x-1, y), (x-1, y+1), (x, y-1), (x, y+1), (x+1, y-1), (x+1, y), (x+1, y+1). Basically all surrounding coordinates EXCEPT the middle coordinate (x,y). To generate all the nine coordinates, I do this currently:
[{X,Y} || X<-li...
Say I have a function func(i) that creates an object for an integer i, and N is some nonnegative integer. Then what's the fastest way to create a list (not a range) equal to this list
mylist = [func(i) for i in range(N)]
without resorting to advanced methods like creating a function in C? My main concern with the above list comprehens...
I have a generator (numbers) and a value (number). I would like to iterate over these as if they were one sequence:
i for i in tuple(my_generator) + (my_value,)
The problem is, as far as I undestand, this creates 3 tuples only to immediately discard them and also copies items in "my_generator" once.
Better approch would be:
def con(...