如何将不等长的列表压缩到字典中?

时间:2019-03-20 21:52:11

标签: python pandas

我有三个清单。

import pandas as pd

author = ['mccoy.robert']
coauthors = [
    'hola.lubica', 'kundu.subiman', 'ntantu.ibula', 
    'fletcher.peter', 'jain.tanvi', 'jindal.varun', 'bankston.paul',
    'di-maio.giuseppe', 'dickman.raymond-f-jun', 'holy.dusan',
    'slover.rebecca', 'curtis.douglas-w', 'duvall.paul-f', 'fogelgren.j-r',
    'hammer.s-t', 'haworth.r-c', 'lutzer.david-j', 'okuyama.akihiro', 
    'pelant.jan', 'porter.jack-ray', 'raha.asit-baran', 'rubin.leonard-roy',
    'santoro.grazia', 'smith.jerome-c', 'todd.aaron-r'
]
frequency = [
    '10', '8', '6', '5', '3', '3', '2', '2', '2', '2', '2', 
    '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'
]

我想将这些列表放入一个数据框中,其中每一行都是作者与每个合著者以及合作的频率配对。基本上,如果我可以将这三个列表压缩在一起,并在每一行中一遍又一遍地复制“作者”列表,那就太好了。

这似乎是一个非常简单的熊猫任务...但是我很乐意提供一些帮助!

1 个答案:

答案 0 :(得分:0)

您可以使用嵌套的for循环来执行此操作,循环遍历作者以及压缩的共同作者和频率。像这样:

authors = ['mccoy.robert']
coauthors = [
    'hola.lubica', 'kundu.subiman', 'ntantu.ibula', 
    'fletcher.peter', 'jain.tanvi', 'jindal.varun', 'bankston.paul',
    'di-maio.giuseppe', 'dickman.raymond-f-jun', 'holy.dusan',
    'slover.rebecca', 'curtis.douglas-w', 'duvall.paul-f', 'fogelgren.j-r',
    'hammer.s-t', 'haworth.r-c', 'lutzer.david-j', 'okuyama.akihiro', 
    'pelant.jan', 'porter.jack-ray', 'raha.asit-baran', 'rubin.leonard-roy',
    'santoro.grazia', 'smith.jerome-c', 'todd.aaron-r'
]
frequencies = [
    '10', '8', '6', '5', '3', '3', '2', '2', '2', '2', '2', 
    '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'
]

combined = []

for author in authors:
    for coauthor, frequency in zip(coauthors, frequencies):
        combined.append((author, coauthor, frequency))

带输出:

[
    ('mccoy.robert', 'hola.lubica', '10'), ('mccoy.robert', 'kundu.subiman', '8'), ('mccoy.robert', 'ntantu.ibula', '6'),
    ('mccoy.robert', 'fletcher.peter', '5'), ('mccoy.robert', 'jain.tanvi', '3'), ('mccoy.robert', 'jindal.varun', '3'),
    ('mccoy.robert', 'bankston.paul', '2'), ('mccoy.robert', 'di-maio.giuseppe', '2'), ('mccoy.robert', 'dickman.raymond-f-jun', '2'),
    ('mccoy.robert', 'holy.dusan', '2'), ('mccoy.robert', 'slover.rebecca', '2'), ('mccoy.robert', 'curtis.douglas-w', '1'),
    ('mccoy.robert', 'duvall.paul-f', '1'), ('mccoy.robert', 'fogelgren.j-r', '1'), ('mccoy.robert', 'hammer.s-t', '1'),
    ('mccoy.robert', 'haworth.r-c', '1'), ('mccoy.robert', 'lutzer.david-j', '1'), ('mccoy.robert', 'okuyama.akihiro', '1'),
    ('mccoy.robert', 'pelant.jan', '1'), ('mccoy.robert', 'porter.jack-ray', '1'), ('mccoy.robert', 'raha.asit-baran', '1'),
    ('mccoy.robert', 'rubin.leonard-roy', '1'), ('mccoy.robert', 'santoro.grazia', '1'), ('mccoy.robert', 'smith.jerome-c', '1'),
    ('mccoy.robert', 'todd.aaron-r', '1')
]