Initialize variables in one line? [duplicate] - python

This question already has answers here:
Assigning multiple variables in one line
(3 answers)
Closed 8 years ago.
Is it possible do the following initializations in one line? As I am quite curious whether a shorter code is possible.
X = []
Y = []

You can initialize them using sequence unpacking (tuple unpacking in this case)
X, Y = [], []
because it's equivalent to
(X, Y) = ([], [])
You can also use a semicolon to join lines in your example:
X = []; Y = []

You can use tuple unpacking (or multiple assignment):
X, Y = [], []

Related

Why do variables sometimes bind both directions in Python? [duplicate]

This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 3 years ago.
Say I have
x = [[0,0]]
y = x[-1]
Then why does
y[1] += 1
give
x = [[0,1]]
y = [0,1]
That is, I'm confused as to why it also changes what x is, even though I only specified something to do with y?
This makes sense if you think of a list as a mutable object in memory and you consider that you're modifying that list.
The variables are simply different names for that same list.
That code is equivalent to:
list_obj = [0, 0]
x = [ list_obj ] # a list with list_obj as
# its single element
y = list_obj # same as x[-1], just
# another name for list_obj
Then it's natural that you're simply modifying list_obj[1] in both cases.

2D array in python changes the whole row when I only want to change 1 item(Is there a way around it?) [duplicate]

This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 3 years ago.
I wanted to create a 2D array that would have a number in each cell.
I need to access specific cells and be ablt to change them.
def print_matrix(matrix):
#just to print it nicer
for x in range(0, len(matrix)):
print(matrix[x])
matrix = []
list = []
for x in range(0,10):
list.append(0)
for x in range(0,10):
matrix.append(list)
matrix[1][2] = 9
print_matrix(matrix)
You actually created only one list and added it 10 times to the matrix (by reference).
matrix = []
for x in range(0,10):
row = []
for x in range(0,10):
row.append(0)
matrix.append(row)
Also, it's not advisable to call variables list, for this is a builtin python function.
Use list.copy():
def print_matrix(matrix):
#just to print it nicer
for x in range(0, len(matrix)):
print(matrix[x])
matrix = []
list = []
for x in range(0,10):
list.append(0)
for x in range(0,10):
matrix.append(list.copy())
matrix[1][2] = 9
print_matrix(matrix)
If you don't use copy, matrix simply holds 10 references to the original list. Changing any one of those references simply changes the original list.
When you use .copy(), new lists are created which are copies of the original one, this allows you to change each one independently.
Your problem is that you have list references in each row in your matrix. Check here for a possible solution.

Accelerating list concatenation in python 3 [duplicate]

This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 4 years ago.
I'm running the following code:
x = []
for i in c:
x = x+i
The result has about 50-100 million elements.
This takes several minutes to run on my PC. How can I accelerate this?
Already compared in join list of lists in python
with Python 2 being faster with .extend than with itertools.chain
An exotique method
l = []
for x in c: l[0:0] = x
among the faster according to
stackoverflow.com/questions/12088089/…
For python 3.5 and latter, even more exotic
l = []
for x in c:
l = [l, *x]
Of course sum(c, []) is among worst on all the measurements.

Getting elements from iterable like a shiftregister [duplicate]

This question already has answers here:
How can I iterate over overlapping (current, next) pairs of values from a list?
(12 answers)
Iterating over every two elements in a list [duplicate]
(22 answers)
Closed 5 years ago.
Is there an idiomatic way of taking two elements from an iterable like from a shift register? I have seen this, but it's another case. For the following example:
for i, j in something_i_need(range(5)):
print("%d %d" % (i, j))
I expect the output:
0 1
1 2
2 3
3 4
I know I can perform this using:
def shift_register(it):
it = iter(it)
x = next(it)
for y in it:
yield x, y
x = y
But i started wondering whether the python standard library contains something for this common use case so I don't have to reinvent the wheel.
In my usecase the iterator is infinite.

Create variable of unknown type automatically if it doesn't exist? [duplicate]

This question already has answers here:
Python one-line "for" expression [duplicate]
(5 answers)
Closed 6 years ago.
I have different variables of an unknown type (in ABAQUS, it says "Sequence") and want to combine them through a loop:
a = [[unknown type], [unknown type], ...]
x = []
for i in a:
x.append(i)
The problem now is that when I initialize x with = [] I get the error message
TypeError: Can only concatenate list (not "Sequence") to list.
Is there another (simple/efficient) way, e.g. to automatically create x in the first loop?
Use a list comprehension:
x = [v for v in a]

Categories

Resources