通过索引更改字典的字典键

发布于 2025-01-11 22:24:46 字数 970 浏览 0 评论 0 原文

我有一个字典 dico,是我用以下代码创建的:

dico = {}
for index, row in data.iterrows():
    
    tup = (row['OsId'], row['BrowserId'])
    
   
    if tup not in dico:
        dico[tup] = []
    dico[tup].append(row['PageId'])
[print(f"{key} : {value}") for key, value in dico.items()]

这里是 dico 的示例:

 combination : list of pages :

(99, 14) : [789615, 1158132, 789615, 789615, 1109643, 789615, 1184903]
(33, 16) : [955761, 955764, 955767, 955761, 955764, 955764, 1154705, 955761]
(12, 99) : [1068379, 1184903, 955764, 955761, 1184903, 955764]
(11, 99) : [1187774]

我正在寻找一种方法来更改 dico 以通过其在组合列表中的索引替换组合值

例如我有组合列表: (99, 14), (33, 16), (12, 99), (11, 99)

预期结果应该是:

0 : [789615, 1158132, 789615, 789615, 1109643, 789615, 1184903]
1 : [955761, 955764, 955767, 955761, 955764, 955764, 1154705, 955761]
2 : [1068379, 1184903, 955764, 955761, 1184903, 955764]
3 : [1187774]

有什么想法吗?谢谢

I have a dictionary dico that i created it with this code :

dico = {}
for index, row in data.iterrows():
    
    tup = (row['OsId'], row['BrowserId'])
    
   
    if tup not in dico:
        dico[tup] = []
    dico[tup].append(row['PageId'])
[print(f"{key} : {value}") for key, value in dico.items()]

here a sample of dico :

 combination : list of pages :

(99, 14) : [789615, 1158132, 789615, 789615, 1109643, 789615, 1184903]
(33, 16) : [955761, 955764, 955767, 955761, 955764, 955764, 1154705, 955761]
(12, 99) : [1068379, 1184903, 955764, 955761, 1184903, 955764]
(11, 99) : [1187774]

I am looking for a way to change the dico to replace the combination value by it's index in the list of combinations

For example i have the list of combination : (99, 14), (33, 16), (12, 99), (11, 99)

The expected result should be :

0 : [789615, 1158132, 789615, 789615, 1109643, 789615, 1184903]
1 : [955761, 955764, 955767, 955761, 955764, 955764, 1154705, 955761]
2 : [1068379, 1184903, 955764, 955761, 1184903, 955764]
3 : [1187774]

Any idea please to do it? thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

黑白记忆 2025-01-18 22:24:46

使用键列表 key_list = [(99, 14), (33, 16), (12, 99), (11, 99)]

dict(enumerate(dico[k] for k in key_list))

With a list of keys key_list = [(99, 14), (33, 16), (12, 99), (11, 99)]:

dict(enumerate(dico[k] for k in key_list))
公布 2025-01-18 22:24:46

这是您想要的吗:

{i: value for i, value in enumerate(dico.values())}

Is this what you want:

{i: value for i, value in enumerate(dico.values())}

祁梦 2025-01-18 22:24:46

您无法重命名键。您可以迭代键列表(不是直接),并将键 old 的值插入另一个键下,然后删除 old 一个:

keys = (99, 14), (33, 16), (12, 99), (11, 99)

d = {}
# iterates the LIST of keys, not dict.keys - that would not work
for idx, k in enumerate(keys,1):
    d[k] = 1111*idx

# before
print(d)

for idx,old in enumerate(keys):
    d[idx] = d[old]    # copy value
    del d[old]         # delete old key from dict

# after
print(d)

输出之前:

{(99, 14): 1111, 
 (33, 16): 2222, 
 (12, 99): 3333, 
 (11, 99): 4444}

输出之后:

{0: 1111, 
 1: 2222, 
 2: 3333, 
 3: 4444}

或者您创建一个全新的字典。

You cannot rename keys. You can iterate the list of keys (not directly) and insert the value of key old under another key and delete the old one:

keys = (99, 14), (33, 16), (12, 99), (11, 99)

d = {}
# iterates the LIST of keys, not dict.keys - that would not work
for idx, k in enumerate(keys,1):
    d[k] = 1111*idx

# before
print(d)

for idx,old in enumerate(keys):
    d[idx] = d[old]    # copy value
    del d[old]         # delete old key from dict

# after
print(d)

Output before:

{(99, 14): 1111, 
 (33, 16): 2222, 
 (12, 99): 3333, 
 (11, 99): 4444}

Output after:

{0: 1111, 
 1: 2222, 
 2: 3333, 
 3: 4444}

Or you create a fully new dict from it.

浅忆流年 2025-01-18 22:24:46

这是一个可能的解决方案:

dico = dict(zip(range(len(dico)), dico.values()))

但是,请记住您是从头开始创建一本新字典。

This is a possible solution:

dico = dict(zip(range(len(dico)), dico.values()))

However, keep in mind that you're creating a new dictionary from scratch.

内心旳酸楚 2025-01-18 22:24:46

最简单的解决方案是:

dico = dict(enumerate(dico.values()))

enumerate(dico.values()) 相当于 zip(range(len(dico)), dico.values())。将该元组序列传递给 dict() 会创建一个新字典,该字典使用每个元组的第一个元素作为键,使用第二个元素作为值。

The simplest solution is:

dico = dict(enumerate(dico.values()))

enumerate(dico.values()) gives you the equivalent of zip(range(len(dico)), dico.values()). Passing that sequence of tuples to dict() creates a new dictionary that uses the first element of each tuple as the key and the second element as the value.

裂开嘴轻声笑有多痛 2025-01-18 22:24:46

用组合列表中的索引替换组合值

如果列表中没有这样的组合怎么办?
也许是这样的?:

comb_list = [(99, 14), (33, 16), (12, 99), (11, 99)]
dico = {(99, 14) : [789615, 1158132, 789615, 789615, 1109643, 789615, 1184903],
        (33, 16) : [955761, 955764, 955767, 955761, 955764, 955764, 1154705, 955761],
        (12, 99) : [1068379, 1184903, 955764, 955761, 1184903, 955764],
        (11, 99) : [1187774],
        (77, 77) : [7777777]}  # not in the list

dico = {comb_list.index(k) if k in comb_list else k:v for k,v in dico.items()}
print(dico)
'''
{0: [789615, 1158132, 789615, 789615, 1109643, 789615, 1184903],
 1: [955761, 955764, 955767, 955761, 955764, 955764, 1154705, 955761],
 2: [1068379, 1184903, 955764, 955761, 1184903, 955764],
 3: [1187774],
 (77, 77): [7777777]}

replace the combination value by it's index in the list of combinations

what about if there is no such combination in the list?
maybe something like this?:

comb_list = [(99, 14), (33, 16), (12, 99), (11, 99)]
dico = {(99, 14) : [789615, 1158132, 789615, 789615, 1109643, 789615, 1184903],
        (33, 16) : [955761, 955764, 955767, 955761, 955764, 955764, 1154705, 955761],
        (12, 99) : [1068379, 1184903, 955764, 955761, 1184903, 955764],
        (11, 99) : [1187774],
        (77, 77) : [7777777]}  # not in the list

dico = {comb_list.index(k) if k in comb_list else k:v for k,v in dico.items()}
print(dico)
'''
{0: [789615, 1158132, 789615, 789615, 1109643, 789615, 1184903],
 1: [955761, 955764, 955767, 955761, 955764, 955764, 1154705, 955761],
 2: [1068379, 1184903, 955764, 955761, 1184903, 955764],
 3: [1187774],
 (77, 77): [7777777]}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文