Which is more pythonic?
list.append(1)
or
list += [1]
|
I'm using a list of lists to store a matrix in python. I tried to initialise a 2x3 Zero matrix as follows.
mat=[[0]*2]*3
However, when I change the value of one of the ... |
I found many different ways of getting the last element from a list in Python:
alist[-1]
alist[len(alist) -1]
How would you do this?
|
guys. I'm trying to find the most elegant solution to a problem and wondered if python has anything built-in for what I'm trying to do.
What I'm doing is this. I ... |
This seems like a common task, alter some elements of an array, but my solution didn't feel very pythonic. Is there a better way to build urls with list comprehension?
links = ...
|
Is there a better way of doing this? I don't really need the list to be sorted, just scanning through to get the item with the greatest specified attribute. I care ... |
OK, I have this simple function that finds the element of the list that maximizes the value of another positive function.
def get_max(f, s):
# f is a function ...
|
|
Suppose i have a function like this:
def getNeighbors(vertex)
which returns a list of vertices that are neighbors of the given vertex. Now i want to create a list with all the neighbors ... |
I want to check if any of the items in one list are present in another list. I can do it simply with the code below, but I suspect there ... |
Is there an analog of setattr() that allows for appending an arbitrary list property of an instantiated class object? If not, is there a recommended way of doing so?
This is a ... |
I am making a small program and at some point from each row of a matrix I need to subtract the average of the row itself. Quite a standard renormalization procedure.
Note ... |
from random import randrange
data = [(randrange(8), randrange(8)) for x in range(8)]
And we have to test if the first item equals to one of a tail. I am curious, how we would ... |
I have a list that will always contain only ones and zeroes.
I need to get a list of the non-zero indices of the list:
a = [0, 1, 0, 1, 0, 0, ...
|
I have a list of 2d points, and would like to find the one which is closest to a given point. The code (get_closest_point()) below does what I want. But is ... |
Basically, wondering what the best / most python-y way of doing this is;
class thing(collectionclass):
for onething in super(collectionclass,listofthings):
if onething != ...
|
From PEP 20, The Zen of Python:
Long time Pythoneer Tim Peters succinctly channels the BDFL's
guiding principles for Python's design into 20 aphorisms, only 19
... |
I have a list of Account objects in self.accounts, and I know that only one of them will have a type attribute equal to 'equity'. What is the best (most ... |
I have a list of results that I need to pull out various other lists from. For example, all owners that are male, all cars between 5 and 10 years old.
def ...
|
I'm returning a list of lists, but the following seems far more convoluted than it should be:
new_list = []
for key, value in group.items():
new_list.extend([['%s%s%s%s%s' % (
...
|
What first pops into my head is a dictionary with keys acting as list values and dictionary values defaulting to None, but this feels sub-optimal to me. Can anyone propose a ... |
Here is an example of what I mean:
a = 0
b = 1
c = range(3)
so I would like to find the missing number in the list which in this case would be ... |
Think about a function that I'm calling for it's side effects, not return values(like printing to screen, updating gui, printing to a file, etc.).
def fun_with_side_effects(x):
...side effects...
...
|
Normal list comprehensions occur this way:
new_list = [f(x) for x in l]
What is the most succinct and readable way to create new list in Python similar to this:
new_list = [f(x) while ...
|
I have a list of objects representing widgets. Each widget object has a manufactured DateTime field that holds the date and time when the widget was made. All the widgets in ... |
I want to return true from the if statement only if all of the elements from list 1 also exist in list 2 (list 2 is a superset of list 1). ... |
Possible Duplicate:
Making a flat list out of list of lists in Python
I have a list of lists:
[['a word'], ['another world'], '['some more','words'], etc]
And I ... |
I have two lists of length n and n+1:
[a_1, a_2, ..., a_n]
[b_1, b_2, ..., b_(n+1)]
I want a function giving as a result a list with alternate elements from the two, that ... |
|
I am looking to analyze a list of files. I have a program that more or less uses os.walk() (I say more or less because it is actually a modified python version of the DOS tree command) that descends through a directory structure and lists the folders and the files that they contain. Now the files in these folders are in ... |