python字典和列表: how to convert it?

标签 python list dictionary

我有这种类型的字典:

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/

相关文章:

python - 简单的python继承

python - 带有单引号字符的 csv.writer

python - 如何更改字典值中字符串中的数字?

Python:从另一个类调用方法的最佳方式?

python - 按数组python中的字典值排序

python - 如何更新 django 中的模型对象?

Python 3.4.3 - 按字母顺序排序统计列表

Python 查询最旧日期列表

python - 根据另一个时间戳值的月份,对列表元素的值进行分组和求和

python - 遍历重新获取的元素列表