我有这种类型的字典:
dic = {
'blahAA,
AA': [
u'cat'
],
'blahBB,
BB': [
u'dog',
u'bird'
],
'blahCC,
CC': [
u'fish',
u'horse',
u'elephant'
]
blahXX 是互联网链接,XX 是 id。 如何将其转换为具有这样的列表:
list = [['cat AA'], ['dog bird BB'], ['fish horse elephant CC']]
可选: 理想情况下,我想在 Gtk 界面(组合框)中显示每个元素(猫、狗、鸟等),而不显示 XX,并希望在选择时找到相应的互联网地址:
例子:
cat --Gtk--> blahAA
dog bird --Gtk--> blahBB
fish horse elephant --Gtk--> blahCC
这可能吗? 谢谢
我实际显示结果的代码是:
choice = self.liste.get_active_text() # choice from the user
for url in self.links:
if id in url:
adress = url
self.champ.set_text(adress) # adress display
最佳答案
[' '.join(map(str, x[1] + x[0].split(',')[1:])) for x in dic.items()]
Out[115]: ['cat AA', 'fish horse elephant CC', 'dog bird BB']
(map(str, seq) 的优点是如果列表中的某些元素是 int
,它不会抛出 TypeError
异常 - 你不能连接Python 中的整数和字符串,因为它是强类型语言,如果仍然是动态类型语言的话)
关于python字典和列表: how to convert it?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23695763/