#π python writing question
58 messages Β· Page 1 of 1 (latest)
@sharp widget
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Sometimes neither, depending. A list comprehension is often a good choice if using one simplifies the look of your code. For more complicated operations, a regular for loop may be a good option, because while you can spread comprehensions out, a regular for loop handles that a bit better from a visual standpoint.
There are other ways of creating grid-like structures in short-lengthed calls. Numpy offers some options, there.
i dont get the for loop or regular loops
you mean like for i is ..
oh is that whole thing called a loop?
When I say regular for loop, I mean a for not in comprehension syntax.
i see
the bottom one is a comprehension syntax right?
when its squished into less lines
Yes.
so then i guess i'll just keep practicing non comprehended syntax
its getting too complicated
Mm. Simple is better than complex. Complex is better than complicated. Readability counts.
!zen
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
both of those methods are using a comprehension, with the latter one being more compact (and perhaps confusing if they're new to you)
!e py import numpy as np arr = np.full([8, 8], 0, dtype=int) print(arr)Flat is better than nested. (I did it with None instead of 0 before)
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | [[0 0 0 0 0 0 0 0]
002 | [0 0 0 0 0 0 0 0]
003 | [0 0 0 0 0 0 0 0]
004 | [0 0 0 0 0 0 0 0]
005 | [0 0 0 0 0 0 0 0]
006 | [0 0 0 0 0 0 0 0]
007 | [0 0 0 0 0 0 0 0]
008 | [0 0 0 0 0 0 0 0]]
and dtype=object, because None
But definitely practice those for loops.
!e py arr = [[0] * 8 for _ in range(8)] print(arr)
:white_check_mark: Your 3.13 eval job has completed with return code 0.
[[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
yea i'm learning from the basics so when the format changes to print the same result, it gets confusing
temps = [[0.0 for h in range(24)] for d in range(31)]
#
# The matrix is magically updated here.
#
total = 0.0
for day in temps:
total += day[11]
average = total / 31
print("Average temperature at noon:", average)
Note: the day variable used by the for loop is not a scalar β each pass through the temps matrix assigns it with the subsequent rows of the matrix; hence, it's a list. It has to be indexed with 11 to access the temperature value measured at noon.
why does it have to be indexed by 11
Python is zero-indexed. Python indices begin at zero.
Lua, by comparison, is 1-indexed.
!e - or even
arr = [[0] * 8] * 8
print(arr)
:white_check_mark: Your 3.13 eval job has completed with return code 0.
[[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
ids
gosh theres so many ways to write the same code
!e
arr = [[0] * 8] * 8
arr[1][2] = 10
print(arr)
:white_check_mark: Your 3.13 eval job has completed with return code 0.
[[0, 0, 10, 0, 0, 0, 0, 0], [0, 0, 10, 0, 0, 0, 0, 0], [0, 0, 10, 0, 0, 0, 0, 0], [0, 0, 10, 0, 0, 0, 0, 0], [0, 0, 10, 0, 0, 0, 0, 0], [0, 0, 10, 0, 0, 0, 0, 0], [0, 0, 10, 0, 0, 0, 0, 0], [0, 0, 10, 0, 0, 0, 0, 0]]
^ each list is the same object here. Hence they all change.
I wonder if it's worse than that...
Eh, nevermind. Thought I might finagle something to change every int at the same time.
I got away with using * here because 0 is 0 however you look at it. Were it some other object, such as a list or other mutable, that could get me into trouble.
guys
Index 11 being the twelfth index.
no, the inner one that @heavy ether did should be fine
!e py import numpy as np arr = np.arange(9).reshape(3, 3) print(arr) print(arr[:, 1]) print(arr[:, 1].mean())
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | [[0 1 2]
002 | [3 4 5]
003 | [6 7 8]]
004 | [1 4 7]
005 | 4.0
Again, I still think you ought to continue as you've been doing. This is just showing you that Numpy can be really neat.
A nested list may not be the ideal data structure for what you want to do.
Nor this.
sometimes a tuple with (x, y) or even (x, y, z) as the key of a dictionary can be a nice way to represent a map or a game board, but for the small fixed size of a chess board (8x8) for example i don't know if i think it's the best solution
rooms = [[[False for r in range(20)] for f in range(15)] for t in range(3)]
why is it starting out with false
it's the default value that it will initially initialize every element with
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.