将列表转换为特定词典

时间:2019-12-17 10:52:08

标签: python list dataframe dictionary

我正在尝试将列表转换为python字典。我有一个清单:

[('buin83',                                                 following
  buin83  [AkuDutaDamai, FarlinaYaz, Mom_isback, yosiago...),
 ('killjoyyadi',                                                      following
  killjoyyadi  [susipudjiastuti, PetrusBun, DidiSupriatna1, B...),
 ('w_ridwan01',                                                     following
  w_ridwan01  [Ita6620, o_dhany, Kadrun_Lucu, FPIgarislucu, ...),
 ('jumianto_rk',                                                      following
  jumianto_rk  [Dedensyah13, AphroditeDewi02, yon_egoist, vit...),
 ('hilmy_elhasan',
                                                         following
  hilmy_elhasan  [alvaroe_lee_, MFirevox, cintank47800660, ndor...)]

我想将其转换为字典

{buin83, AkuDutaDamai},{buin83, FarlinaYaz}, {buin83, Mom_isback}, {buin83, yosiago}, {killjoyyadi, susipudjiastuti}, {killjoyyadi, PetrusBun},...

我尝试了一些代码,但是没有用。感谢您的帮助。

我尝试过将以下代码转换为字典:

d = {}

for l in df_following:
    username = l[0]
    following = l[1]

    if username in d: 
        d[username][following] = [following,following,following]
    else: 
        d[username] = {following:[following,following,following]}

但是我明白了

TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed

实际上,我仍然对List的逻辑感到困惑

0 个答案:

没有答案