#πŸ”’ python writing question

58 messages Β· Page 1 of 1 (latest)

sharp widget
#
board = []

for i in range(8):
    row = [EMPTY for i in range(8)]
    board.append(row)

board = [[EMPTY for i in range(8)] for j in range(8)]

weary mapleBOT
#

@sharp widget

Python help channel opened

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.

sharp widget
#

they both say the same thing, which one do most programmers use

#

@agile sparrow

heavy ether
#

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.

sharp widget
#

you mean like for i is ..

#

oh is that whole thing called a loop?

heavy ether
#

When I say regular for loop, I mean a for not in comprehension syntax.

sharp widget
#

the bottom one is a comprehension syntax right?

heavy ether
#

Because they're both for loops.

#

Yes.

sharp widget
#

when its squished into less lines

heavy ether
#

Yes.

sharp widget
#

its getting too complicated

heavy ether
#

Mm. Simple is better than complex. Complex is better than complicated. Readability counts.

#

!zen

weary mapleBOT
#
The Zen of Python, by Tim Peters

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!

lean cipher
#

both of those methods are using a comprehension, with the latter one being more compact (and perhaps confusing if they're new to you)

heavy ether
#

!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)

weary mapleBOT
heavy ether
#

and dtype=object, because None

#

But definitely practice those for loops.

#

!e py arr = [[0] * 8 for _ in range(8)] print(arr)

weary mapleBOT
sharp widget
#
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

heavy ether
#

Python is zero-indexed. Python indices begin at zero.

#

Lua, by comparison, is 1-indexed.

faint sentinel
#

!e - or even

arr = [[0] * 8] * 8
print(arr)
weary mapleBOT
heavy ether
#

ids

sharp widget
tardy hemlock
#

!e

arr = [[0] * 8] * 8
arr[1][2] = 10
print(arr)
weary mapleBOT
tardy hemlock
#

^ each list is the same object here. Hence they all change.

faint sentinel
#

haha, right, just remembered that now

#

so, no good

tardy hemlock
#

I wonder if it's worse than that...
Eh, nevermind. Thought I might finagle something to change every int at the same time.

heavy ether
sharp widget
#

guys

heavy ether
#

Index 11 being the twelfth index.

faint sentinel
heavy ether
#

!e py import numpy as np arr = np.arange(9).reshape(3, 3) print(arr) print(arr[:, 1]) print(arr[:, 1].mean())

weary mapleBOT
heavy ether
#

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.

faint sentinel
#

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

sharp widget
#
rooms = [[[False for r in range(20)] for f in range(15)] for t in range(3)]

#

why is it starting out with false

faint sentinel
weary mapleBOT
#
Python help channel closed for inactivity

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.