Python create array of certain depth - python

So basically what i want my script to do is output something like
somelist = ([0,0],[0,0])
given x = 2.
so if x were 3 it would be
somelist = ([0,0,0],[0,0,0],[0,0,0])
what I've come up with so far is
somelist = ([0] * x) * x
but this simply returns
somelist = [0,0,0,0]
Anybody know of a simple way to do this?

The old singleton tuple issue. Add a comma or else the parentheses will be meaningless (interpreted merely as precedence/grouping markers):
somelist = ([0] * x,) * x
# ^--- makes all the difference

Related

List comprehension slower when set is declared inside

I tested two snippets of code and found out that declaring a set ahead of using it in a list comprehension was much faster than declaring it inside the list comprehension. Why does this happen? (Using python 3.9.13)
import time
# Setup
a = [x for x in range(10000)]
b = [x for x in range(8000)]
t = time.time()
b = set(b)
[x for x in a if x in b]
print(time.time() - t)
# 0.0010492801666259766
t = time.time()
[x for x in a if x in set(b)]
print(time.time() - t)
# 1.0515294075012207
I didn't expect there to be orders of magnitude of a difference...
Actually set([iterable]) function returns object of type set and inside the list comprehension you are repeating the execution of the function in each iteration, while in first case you only reference its result to b variable and execute the list comprehension on the referenced object.

Understanding Specific Python List Comprehension

I am trying to use this code that produces all possible outcomes of a dice roll, given the number of dice and number of sides. This codes works (but I do not quite understand how the list comprehension is working.
def dice_rolls(dice, sides):
"""
Equivalent to list(itertools.product(range(1,7), repeat=n)) except
for returning a list of lists instead of a list of tuples.
"""
result = [[]]
print([range(1, sides + 1)] * dice)
for pool in [range(1, sides + 1)] * dice:
result = [x + [y] for x in result for y in pool]
return result
Therefore, I am trying to re-write the list comprehension
result = [x + [y] for x in result for y in pool]
into FOR loops to try to make sense of how it is working, but am currently unable to properly do it. Current failed code:
for x in result:
for y in pool:
result = [x + [y]]
2nd Question: If I wanted to make this into a generator (because this function is a memory hog if you have enough dice and sides), would I simply just yield each item in the list as it is being produced instead of throwing it into the result list?
EDIT: I came up with a way to break the list comprehension into loops after getting great responses and wanted to capture it:
def dice_rolls(dice, sides):
result = [[]]
for pool in [range(1, sides + 1)] * dice:
temp_result = []
for existing_values in result: # existing_value same as x in list comp.
for new_values in pool: # new_value same as y in list comp.
temp_result.append(existing_values + [new_values])
result = temp_result
return result
My first instinct for this problem (and list comprehensions in general) would be to use recursion. Though YOU have asked for LOOPS, which is surprisingly challenging.
This is what I came up with;
def dice_rollsj(dice, sides):
result = [[]]
for num_dice in range(dice):
temp_result = []
for possible_new_values in range(1, sides+1):
for existing_values in result:
new_tuple = existing_values + [possible_new_values]
temp_result.append(new_tuple)
result = temp_result
I think that you'll get the same correct answers, but the numbers will be differently ordered. This may be due to the way the values are appended to the list. I don't know.... Let me know if this helps.
I tried to add as many lines as I could, because the goal was to expand and understand the comprehension.
you are redefining result with every iteration of the for y in pool loop. I believe what you want is to append the results:
result = []
for x in result:
for y in pool:
result.append(x + [y])

Difference In List - Python - Syntax Explanation [duplicate]

This question already has answers here:
Explanation of how nested list comprehension works?
(11 answers)
Closed 6 years ago.
Can someone please explain the meaning the syntax behind the following line of code:
temp3 = [x for x in temp1 if x not in s]
I understand it's for finding the differences between 2 lists, but what does the 'x' represent here? Each individual element in the list that is being compared? I understand that temp1 and s are lists. Also, does x for x have to have the same variable or could it be x for y?
[x for x in temp1 if x not in s]
It may help to re-order it slightly, so you can read the whole thing left to right. Let's move the first x to the end.
[for x in temp1 if x not in s yield x]
I've added a fake yield keyword so it reads naturally as English. If we then add some colons it becomes even clearer.
[for x in temp1: if x not in s: yield x]
Really, this is the order that things get evaluated in. The x variable comes from the for loop, that's why you can refer to it in the if and yield clauses. But the way list comprehensions are written is to put the value being yielding at the front. So you end up using a variable name that's not yet defined.
In fact, this final rewrite is exactly how you'd write an explicit generator function.
def func(temp1, s):
for x in temp1:
if x not in s:
yield x
If you call func(temp1, s) you get a generator equivalent to the list. You could turn it into that list with list(func(temp1, s)).
It iterates through each element in temp1 and checks to see if it is not in s before including it in temp3.
It is a shorter and more pythonic way of writing
temp3 = []
for item in temp1:
if item not in s:
temp3.append(item)
Where temp1 and s are the two lists you are comparing.
As for your second question, x for y will work, but probably not in the way you intend to, and certainly not in a very useful way. It will assign each item in temp1 to the variable name y, and then search for x in the scope outside of the list comprehension. Assuming x is defined previously (otherwise you will get NameError or something similar), the condition if x not in s will evaluate to the same thing for every item in temp1, which is why it’s not terribly useful. And if that condition is true, your resulting temp3 will be populated with xs; the y values are unused.
Do not take this as saying that using different variables in a list comprehension is never useful. In fact list comprehensions like [a if condition(x) else b for x in original_sequence] are often very useful. A list comprehension like [a for x in original_sequence if condition(x)] can also be useful for constructing a list containing exactly as many instances of a as the number of items in original_sequence that satisfy condition().
Try yourself:
arr = [1,2,3]
[x+5 for x in arr]
This should give you [6, 7, 8] that are the values on the [1,2,3] list plus 5. This syntax is know as list comprehension (or mapping). It applies the same instructions to all elements on a list. Would be the same as doing this:
for x in arr:
arr += 5
X is same variable and it is not y. It works same as below code
newList = []
for x in temp1:
if x not in s:
newList.append(x)
So x for x, here first is x which is inside append in code and x after for is same as for x in temp1.

Declare array with "where"

There is in Python option to declare array like this?
arr = [x for x in vec where x < 2] - (Edit: sorry for the 'from'. my common mistake (the eclipse always repair me))
(or another statement without real loop)
Use if instead:
arr = [x for x in vec if x < 2]
And note that it is a for loop, the from keyword does not exist.

Appending a list to itself in Python [duplicate]

This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 5 months ago.
I want to attach a list to itself and I thought this would work:
x = [1,2]
y = x.extend(x)
print y
I wanted to get back [1,2,1,2] but all I get back is the builtin None. What am I doing wrong? I'm using Python v2.6
x.extend(x) does not return a new copy, it modifies the list itself.
Just print x instead.
You can also go with x + x
x.extend(x) modifies x in-place.
If you want a new, different list, use y = x + x.
or just:
x = [1,2]
y = x * 2
print y
x.extend(x) will extend x inplace.
>>> print x
[1, 2, 1, 2]
If you want a new copy of the list try:
x = [1,2]
y = x + x
print y # prints [1,2,1,2]
The difference is that extend modifies the list "in place", meaning it will always return None even though the list is modified.

Categories