相关疑难解决方法(0)

如何将键值元组列表转换为字典?

我有一个看起来像这样的列表:

[('A', 1), ('B', 2), ('C', 3)]
Run Code Online (Sandbox Code Playgroud)

我想把它变成一个看起来像的字典:

{'A': 1, 'B': 2, 'C': 3}
Run Code Online (Sandbox Code Playgroud)

最好的方法是什么?

编辑:我的元组列表实际上更像是:

[(A, 12937012397), (BERA, 2034927830), (CE, 2349057340)]
Run Code Online (Sandbox Code Playgroud)

python dictionary list

118
推荐指数
5
解决办法
12万
查看次数

通过列表理解将元组列表转换为具有重复键的字典?

我有一个包含重复的元组列表,我已经使用我在此处找到的代码将它们转换为字典:

/sf/answers/4284079411/

mylist = [(a,1),(a,2),(b,3)]    
result = {}
for i in mylist:  
   result.setdefault(i[0],[]).append(i[1])
print(result)
>>> result = {a:[1,2], b:[3]}
Run Code Online (Sandbox Code Playgroud)

我记得了解到大多数 for 循环都可以重写为推导式,所以我想练习,但在过去的一个小时里我未能完成一项工作。

我读了这个:https : //stackoverflow.com/a/56011919/2415706,现在我还没有找到另一个这样做的库,但我也不确定我想写的这种理解是否是一个坏主意因为 append 会改变事物。

python dictionary-comprehension

5
推荐指数
1
解决办法
105
查看次数

将元组列表附加到现有字典中

我有一个现有的字典,我想在这个字典中添加一个元组列表.

现有字典结构:

myD = {'key1': 123 , 'key2': 456}
Run Code Online (Sandbox Code Playgroud)

元组结构列表:

myL = [('fkey1',321),('fkey2',432),('fkey3',543)]
Run Code Online (Sandbox Code Playgroud)

添加元组列表后的预期字典

myD = {'key1': 123 ,'key2': 456 ,'fkey': 321 ,'fkey2': 432 ,'fkey3': 543}
Run Code Online (Sandbox Code Playgroud)

我怎么能在python中实现这个?

python

4
推荐指数
1
解决办法
86
查看次数

将元组元组转换为字典

所以我在元组中有元组,我想把它们变成一个键:值对.

((1L, 'I.T.'), (2L, 'Project Management'), (3L, 'Creative'), (4L, 'Programming'), (5L, 'Sales'), (6L, 'Administration'), (7L, 'AV'), (8L, 'Human Resources'), (9L, 'Conference Rooms'), (10L, 'Testing'), (11L, 'none'))
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?

python

3
推荐指数
1
解决办法
286
查看次数

如何将元组列表转换为字典

我有一个元组列表,如下所示:

lst_of_tpls = [(1, 'test2', 3, 4),(11, 'test12', 13, 14),(21, 'test22', 23,24)]
Run Code Online (Sandbox Code Playgroud)

我想将它转换为字典,使它看起来像这样:

mykeys = ['ones', 'text', 'threes', 'fours']
mydict = {'ones': [1,11,21], 'text':['test2','test12','test22'], 
          'threes': [3,13,23], 'fours':[4,14,24]}
Run Code Online (Sandbox Code Playgroud)

我试图列举lst_of_tpls这样的:

mydict = dict.fromkeys(mykeys, [])
for count, (ones, text, threes, fours) in enumerate(lst_of_tpls):
    mydict['ones'].append(ones)
Run Code Online (Sandbox Code Playgroud)

但是这使得我希望在'ones'中看到的值也在其他"类别"中:

{'ones': [1, 11, 21], 'text': [1, 11, 21], 'threes': [1, 11, 21], 'fours': [1, 11, 21]}
Run Code Online (Sandbox Code Playgroud)

另外,我想保持mykeys灵活性.

python

3
推荐指数
1
解决办法
76
查看次数

如何在保留冗余值的同时将元组列表转换为字典?

我得到的数据集格式化为键值对列表.关键是数据源,值是数据元素.例如:

[('a', 3), ('b', 5), ('a', 7), ('c', 15), ('d', 12)]
Run Code Online (Sandbox Code Playgroud)

我想把这个列表变成字典.我可以使用Python的内置功能dict(),但它会抛弃冗余值并仅保留与给定键关联的最后一个值.我想将冗余值放入列表中,如下所示:

{'a': [3, 7],
'b': [5],
'c': [15],
'd': [12]}
Run Code Online (Sandbox Code Playgroud)

有一个简单的方法来做上述事情吗?我认为必须有,但我似乎无法通过谷歌找到正确的提示.

python dictionary tuples list

2
推荐指数
2
解决办法
201
查看次数

将元组列表转换为 python 字典而不会丢失顺序

我尝试将元组列表转换为字典,但是当我使用 dict() 函数执行此操作时,元素的顺序发生了变化:

l = [('id', 2), ('osm_id', 3), ('sourceid', 4), ('notes', 5), ('ref', 6), ('rtenme', 7), ('ntlclass', 8), ('fclass', 9), ('numlanes', 10), ('srftpe', 11), ('srfcond', 12), ('isseasonal', 13), ('curntprac', 14), ('gnralspeed', 15), ('rdwidthm', 16), ('status', 17), ('bridge', 18), ('iso3', 19), ('country', 20), ('last_updat', 21)]

 dict_l = dict(l)
 print (dict_l)

{'fclass': 9, 'status': 17, 'isseasonal': 13, 'bridge': 18, 'sourceid': 4, 'country': 20, 'notes': 5, 'rtenme': 7, 'iso3': 19, 'last_updat': 21, 'gnralspeed': 15, 'osm_id': 3, 'srfcond': 12, 'numlanes': 10, 'srftpe': 11, …
Run Code Online (Sandbox Code Playgroud)

python dictionary

2
推荐指数
1
解决办法
3870
查看次数

将元组列表更改为字典(pythonic方式)

这更像是一个最佳实践问题.我创建的是完美的工作,但我很好奇是否有一个更短的方法来创建我当前的数据结构中的字典.

我正在从SQLite数据库中读取表格,数据作为元组列表返回.例如

[(49, u'mRec49', u'mLabel49', 1053, 1405406806822606L, u'1405406906822606'), 
(48, u'mRec48', u'mLabel48', 1330, 1405405806822606L, u'1405405906822606'), 
(47, u'mRec47', u'mLabel47', 1220, 1405404806822606L, u'1405404906822606')...
] 
Run Code Online (Sandbox Code Playgroud)

我想获取list-tuple结构的每一列,使其成为一个列表,从数据库中获取列名,并将其用作保存列表的键.后来我把字典变成了JSON.

这是我的功能,我抓了,它完成了工作,我不禁想知道是否有更好的方法来做到这一点.

def make_dict(columns, list_o_tuples):
    anary = {}
    for j, column in enumerate(columns):
        place = []
        for row in list_o_tuples:
            place.append(row[j])
        anary[column] = place
    return anary

make_dict(mDBTable.columns, mDBTable.get_table())
Run Code Online (Sandbox Code Playgroud)

注意:该函数不应关心其呈现的表,或表中的数字或行和列.

python sqlite dictionary tuples list

1
推荐指数
1
解决办法
819
查看次数