英文:
Iterating through a dictionary for a specific outcome
问题
以下是您要翻译的内容:
我有一个字典,其中键表示类别,它们的值表示每个类别的项目列表,如下所示:
```python
d = {category_a: [item_a, item_b, item_c], category_b: [item_d, item_e]}
我想要遍历字典以获得以下结果:
category_a
· item_a
· item_b
· item_c
category_b
· item_d
· item_e
换行和其他格式似乎可以工作。我的问题是我只能获得以下结果:
category_a
· item_a
· item_b
· item_c
· item_d
· item_e
category_b
· item_a
· item_b
· item_c
· item_d
· item_e
我了解到我的代码(请参见下面)对所有键的所有项目进行迭代,这可能导致了问题。我尝试过其他一切都导致错误或每行一个字母的结果。我想将结果放在PySimpleGUI的多行文本框中。这就是为什么我打算将它放在一个字符串中。
以下是我的基本代码结构:
def display():
d = {category_a: [item_a, item_b, item_c], category_b: [item_d, item_e]}
output = ""
for k in d.keys():
output += k + "\n"
for key, val in d.items():
for v in val:
output += " · " + v + "\n"
return output
在第二个for循环中,我尝试只访问上面使用的键的值(第一个循环),但无法使其正常工作:要么我只得到项目列表 - 而不是其中的项目 - 要么我会产生错误。
英文:
I have a dictionary in which the keys represent the categories and their values the item-lists for each category, like so:
d = {category_a: [item_a, item_b, item_c], category_b: [item_d, item_e]}
I want to iterate through the dictionary for the following result:
category_a
· item_a
· item_b
· item_c
category_b
· item_d
· item_e
The formatting with break-lines and such seem to work. My issue is that I only get to the following result:
category_a
· item_a
· item_b
· item_c
· item_d
· item_e
category_b
· item_a
· item_b
· item_c
· item_d
· item_e
I understand that my code (see below) iterates though all items for all keys, which probably causes the issue. Everything else have I tried leads to errors or one-letter-per-line outcome. I want to place the outcome in a PySimpleGUI Multiline. That's why I intend to place it in a string.
Here is my basic code-structure:
def display():
d = {category_a: [item_a, item_b, item_c], category_b: [item_d, item_e]}
output = ""
for k in d.keys():
output += k + "\n"
for key, val in d.items():
for v in val:
output += " · " + v + "\n"
return output
I tried, in the second for loop, to only access the values of the key used above (first loop) but can't get it working: Either I only get the item-list - not the items in it - or I produce an error.
答案1
得分: 1
您可以直接调用 `Multiline` 元素的 `print` 方法。
英文:
You can call method print
of Multiline
element directly.
import PySimpleGUI as sg
def display(ml):
d = {
"category_a": ["item_a", "item_b", "item_c"],
"category_b": ["item_d", "item_e"],
}
for key, items in d.items():
ml.print(key)
for item in items:
ml.print(f". {item}")
sg.set_options(font=("Courier New", 12, "bold"))
layout = [[sg.Multiline("", size=(40, 10), key='-ML-')]]
window = sg.Window('Title', layout, finalize=True)
display(window['-ML-'])
window.read(close=True)
答案2
得分: 0
以下是翻译好的代码部分:
def display():
d = {'category_a': ['item_a', 'item_b', 'item_c'], 'category_b': ['item_d', 'item_e']}
output = ""
for k in d.keys(): # for k in d: also works
output += k + "\n"
for v in d[k]:
output += " • " + v + "\n"
return output
请让我知道如果您需要任何其他的帮助。
英文:
Here is the corrected code:
def display():
d = {'category_a': ['item_a', 'item_b', 'item_c'], 'category_b': ['item_d', 'item_e']}
output = ""
for k in d.keys(): # for k in d: also works
output += k + "\n"
for v in d[k]:
output += " · " + v + "\n"
return output
答案3
得分: 0
def display():
d = {"category_a": ["item_a", "item_b", "item_c"], "category_b": ["item_d", "item_e"]}
output = ""
for k, v in d.items():
output += k + "\n"
for val in v:
output += " • " + val + "\n"
return output
英文:
def display():
d = {"category_a": ["item_a", "item_b", "item_c"], "category_b": ["item_d", "item_e"]}
output = ""
for k,v in d.items():
output += k + "\n"
for val in v:
output += " · " + val + "\n"
return output
The value of the dict
is a list
, so just iterate though the list
答案4
得分: -1
希望这有所帮助。
保持好工作!
英文:
Hope this helps.
Keep the good work!
def display():
d = {"category_a": ["item_a", "item_b", "item_c"], "category_b": ["item_d", "item_e"]}
output = ""
for key, val in d.items():
output += key + "\n"
for v in val:
output += " · " + v + "\n"
return output
if __name__ == '__main__':
print(display())
答案5
得分: -1
而不是多次调用 print(),最好构建一个需要打印的内容列表。类似这样:
def display(d):
out = []
for k, v in d.items():
out.append(k)
out += [f' · {_v}' for _v in v]
print(*out, sep='\n')
d = {'category_a': ['item_a', 'item_b', 'item_c'], 'category_b': ['item_d', 'item_e']}
display(d)
英文:
Instead of making multiple calls to print() rather build a list of what needs to be printed. Something like this:
def display(d):
out = []
for k, v in d.items():
out.append(k)
out += [f' · {_v}' for _v in v]
print(*out, sep='\n')
d = {'category_a': ['item_a', 'item_b', 'item_c'], 'category_b': ['item_d', 'item_e']}
display(d)
Output:
category_a
· item_a
· item_b
· item_c
category_b
· item_d
· item_e
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论