将嵌套字典转换为一个不变的唯一数字

huangapple go评论75阅读模式
英文:

Converting nested dict into a single unchanging unique number

问题

I have translated the code section you provided. Here's the translated code:

我正在进行极小化极大算法的项目我正在尝试找到一种方法来将棋盘值保存在文本文件中以便在每次测试程序时不必一遍又一遍地进行计算我将棋盘存储为嵌套字典

```python
rows = {
    4:{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0},
    3:{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0},
    2:{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0},
    1:{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0},
}

我尝试了以下方法,它可以得到期望的结果,但并不是最优化的方法,我相信有更好的方法来做这个。

rows = {
    4:{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0},
    3:{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0},
    2:{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0},
    1:{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0},
}

e = []
for key in rows:
    e.append(list(rows[key].values()))
e = str(e)
e = e.replace("[", "")
e = e.replace("]", "")
e = e.replace(" ", "")
e = e.replace(",", "")
print(e)

Please note that the translated code remains the same, and I've retained the code formatting and comments from the original code.

<details>
<summary>英文:</summary>

I&#39;m working on a minimax algorithm project and I am trying to find a way to save board values in a text file so they don&#39;t need to be calculated over and over again each time the program is tested. I have the board stored as a nested dictionary.

rows = {
4:{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0},
3:{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0},
2:{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0},
1:{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0},
}


I tried doing this, which gives the desired result but is not at all optimized and I&#39;m sure there is a way to do this better.

rows = {
4:{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0},
3:{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0},
2:{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0},
1:{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0},
}

e = []
for key in rows:
e.append(list(rows[key].values()))
e=str(e)
e=e.replace ("[",""); e=e.replace ("]","")
e=e.replace (" ","")
e=e.replace (",","")
print(e)


</details>


# 答案1
**得分**: 1

你可以使用[`str.join()`][1],`map`用于将整数转换为字符串:

```python
res = ''.join(''.join(map(str, r.values())) for r in rows.values())
print(res)

输出结果:

00000000000000000000000000000000
英文:

You could make use of a str.join(), map is used to convert integers to strings:

res = &#39;&#39;.join(&#39;&#39;.join(map(str, r.values())) for r in rows.values())
print(res)

Out:

00000000000000000000000000000000

huangapple
  • 本文由 发表于 2023年2月7日 02:32:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365269.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定