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

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

Converting nested dict into a single unchanging unique number

问题

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

  1. 我正在进行极小化极大算法的项目我正在尝试找到一种方法来将棋盘值保存在文本文件中以便在每次测试程序时不必一遍又一遍地进行计算我将棋盘存储为嵌套字典
  2. ```python
  3. rows = {
  4. 4:{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0},
  5. 3:{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0},
  6. 2:{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0},
  7. 1:{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0},
  8. }

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

  1. rows = {
  2. 4:{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0},
  3. 3:{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0},
  4. 2:{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0},
  5. 1:{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0},
  6. }
  7. e = []
  8. for key in rows:
  9. e.append(list(rows[key].values()))
  10. e = str(e)
  11. e = e.replace("[", "")
  12. e = e.replace("]", "")
  13. e = e.replace(" ", "")
  14. e = e.replace(",", "")
  15. print(e)
  1. Please note that the translated code remains the same, and I've retained the code formatting and comments from the original code.
  2. <details>
  3. <summary>英文:</summary>
  4. 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},
}

  1. 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)

  1. </details>
  2. # 答案1
  3. **得分**: 1
  4. 你可以使用[`str.join()`][1],`map`用于将整数转换为字符串:
  5. ```python
  6. res = ''.join(''.join(map(str, r.values())) for r in rows.values())
  7. print(res)

输出结果:

  1. 00000000000000000000000000000000
英文:

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

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

Out:

  1. 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:

确定