gpt4 book ai didi

python - Django - 从二维列表中选择特定的行和列

转载 作者:太空宇宙 更新时间:2023-11-03 16:47:41 24 4
gpt4 key购买 nike

我正在尝试在 Django 中显示维吉尼亚广场,但我想为特定的行和列指定颜色,以便可以从广场的其余部分中识别它。通过这个实现,整个正方形都被着色了。我希望将第一行着色,并将第一列也着色。其余的必须是白色的。

模板:

<div class="hidden-xs">
<table id="show-table">
<tbody>
{% for row in vigenereExample %}
{% if row.0 %}
<tr style="background-color: #98AFC7;">
{% for col in row %}
<td>
{{col}}
</td>
{% endfor %}
</tr>
{% else %}
<tr>
{% for col in row %}
<td>
{{col}}
</td>
{% endfor %}
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
</div>

查看.py

def vigenereHome(request):
object = VigenereCipher()
vigenereSquare = object.getSquaredAlphabet()
vigenereExample = object.geSquareExample()
context = {
'vigenereSquare' : vigenereSquare,
'vigenereExample' : vigenereExample
}
return render(request, "VigenereCipher.html", context)

这是构建 Square 的类维吉尼亚密码.py

class VigenereCipher:

squareAlphabet = []
squareExample = []

exampleKey = "NETWORK"
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
nonCapLetters = "abcdefghijklmnopqrstuvwxyz"

def __init__(self):
# Initialize a list of lists. 27x27 array because of the Grid
self.squaredAlphabet=[ [ '' for x in range(27)] for j in range(27)]
self.squareExample=[['' for x in range(26)] for j in range(len(self.exampleKey) + 1)]

# Create Grid on column
for y in range(len(self.letters)):
#self.squaredAlphabet[0][y+1] = "x"
self.squaredAlphabet[0][y+1] = self.nonCapLetters[y]
self.squareExample[0][y] = self.letters[y]

# Create Grid on row
for y in range(len(self.letters)):
#self.squaredAlphabet[y+1][0] = "y"
self.squaredAlphabet[y+1][0] = self.nonCapLetters[y]

for y in range(len(self.exampleKey)):
self.squareExample[y+1][0] = self.exampleKey[y]

# Fill Vigenere Square
for x in range(len(self.letters)):
j = 0
for y in range(len(self.letters)):
j = y + x
if(j > 25):
j = j - 26
self.squaredAlphabet[x+1][y+1] = self.letters[j]

# For debugging purposes, display the Square.
# for x in range(len(self.squaredAlphabet)):
# for y in range(len(self.squaredAlphabet)):
# print("Position: x", x, "y", y, "=", self.squaredAlphabet[x][y])

# Fills the exampleSquare
for x in range(len(self.exampleKey)):
j = 0
for y in range(len(self.letters)):
j = self.letters.index(self.exampleKey[x]) + y
if(j > 25):
j = j - 26
self.squareExample[x+1][y] = self.letters[j]
print(self.squareExample)

def getSquaredAlphabet(self):
return self.squaredAlphabet

def geSquareExample(self):
return self.squareExample

最佳答案

也许你需要的是

<div class="hidden-xs">
<table id="show-table">
<tbody>
{% for row in vigenereExample %}
<tr>
{% for col in row %}
{% if forloop.counter0 == 0 or forloop.parentloop.counter0 == 0 %}
<td style="background-color: #98AFC7;">
{% else %}
<td>
{% endif %}
{{col}}
</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
</div>

关于python - Django - 从二维列表中选择特定的行和列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36158583/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com