将循环结果保存到一个变量中?

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

Save Loop result into a variable?

问题

我有一个带有循环的代码:

while j < 25:
    print('#', j, ":", head[head.index(minHead) - j])
    j = j + 1

这将打印出以下结果:

# 0 : 12.148057
# 1 : 15.776696999999999
# 2 : 28.555822
# 3 : 28.89765
# 4 : 28.923944999999996
# 5 : 28.950239
# 6 : 28.950239
# 7 : 28.950239
# 8 : 28.950239
# 9 : 28.923944999999996
# 10 : 28.923944999999996
# 11 : 28.923944999999996
# 12 : 28.923944999999996
# 13 : 28.923944999999996
# 14 : 28.923944999999996
# 15 : 28.923944999999996
# 16 : 28.923944999999996
# 17 : 28.923944999999996
# 18 : 28.923944999999996
# 19 : 28.923944999999996
# 20 : 28.923944999999996
# 21 : 28.950239
# 22 : 28.950239
# 23 : 28.950239
# 24 : 28.950239

我可以保存所有这些 #24 索引值,并为它们命名,以便在 tkinter 小部件中显示它们作为信息文本吗?

英文:

I have a code with a loop:

while j &lt; 25:
    print (&#39;#&#39;,j, &quot;:&quot;, head[head.index(minHead)-j])
    j = j + 1

That gives as result printed:

# 0 : 12.148057
# 1 : 15.776696999999999
# 2 : 28.555822
# 3 : 28.89765
# 4 : 28.923944999999996
# 5 : 28.950239
# 6 : 28.950239
# 7 : 28.950239
# 8 : 28.950239
# 9 : 28.923944999999996
# 10 : 28.923944999999996
# 11 : 28.923944999999996
# 12 : 28.923944999999996
# 13 : 28.923944999999996
# 14 : 28.923944999999996
# 15 : 28.923944999999996
# 16 : 28.923944999999996
# 17 : 28.923944999999996
# 18 : 28.923944999999996
# 19 : 28.923944999999996
# 20 : 28.923944999999996
# 21 : 28.950239
# 22 : 28.950239
# 23 : 28.950239
# 24 : 28.950239

Can I save all this #24 index values with a name in order to show all of them as info text inside a tkinter widget?

答案1

得分: 1

你需要创建一个变量来保存来自while循环的值,例如:

list_of_values = []
j = 0
while j < 25:
    string_to_save = '#', j, ':', j
    print(string_to_save)
    j = j + 1  # 这可以写成 j += 1
    list_of_values.append(string_to_save)

我移除了变量headminHead等,因为您的代码没有定义这些对象。但上面的代码片段应该足够让您走上正确的道路。

如果您对tkinter有单独的问题,请随时提出单独的问题。

英文:

You'll need to create a variable to save the values from your while loop e.g.:

list_of_values = []
j = 0
while j &lt; 25:
    string_to_save = &#39;#&#39;,j, &quot;:&quot;, j
    print (string_to_save)
    j = j + 1 # this can be written as j += 1
    list_of_values.append(string_to_save)

I removed the variables head, minHead etc. as your code doesn't define what these objects are. But the snippet above should be enough to get your on the right track.

If you have a separate problem with tkinter feel free to ask a separate question.

huangapple
  • 本文由 发表于 2020年1月3日 19:27:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577817.html
匿名

发表评论

匿名网友

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

确定