从python

时间:2018-08-02 07:21:43

标签: python dictionary key

我有键的.dict(字典文件)和每个键的多个值(主要是因为某些键只有一个值),我想以特定格式提取它们。

输入文件:

{"OMIM:614373": ["10280"], "OMIM:614370": ["6439", "1439", "6440", "21"], "OMIM:614371": ["30835"], "OMIM:614376": ["57728"]}

输出文件应如下所示:

OMIM:614373 10280
OMIM:614370 6439
OMIM:614370 1439
OMIM:614370 6440
OMIM:614370 21
OMIM:614371 30835
OMIM:614376 57728

这是我第一次处理此类文件,请帮忙吗?

2 个答案:

答案 0 :(得分:1)

通过字典和每个键的值进行简单的迭代(如果python 3.x l.items(),如果python2使用l.iteritems())“

l = {"OMIM:614373": ["10280"], "OMIM:614370": ["6439", "1439", "6440", "21"], "OMIM:614371": ["30835"], "OMIM:614376": ["57728"]}
for k,v in l.items():
    for item in v:
        print(k, item)

OMIM:614373 10280
OMIM:614370 6439
OMIM:614370 1439
OMIM:614370 6440
OMIM:614370 21
OMIM:614371 30835
OMIM:614376 57728

答案 1 :(得分:1)

connect(listWidget_dirList, & QListWidget::currentRowChanged,[listWidget_dirList,directory,listWidget_main,this](int currentRow)

{
    //listWidget_dirList->setEnabled(false);
    listWidget_main->show();
    listWidget_main->clear();

    QListWidgetItem *item = listWidget_dirList->item(currentRow);
    QDir dir = directory.absolutePath() + '/'+item->text()  ;
    dir.setNameFilters({"*.mp4" , "*.avi" , "*.flv" , "*.mwv"});

    for(const QFileInfo & finfo: dir.entryInfoList()){
        QMediaPlayer *mediaPlayer = new QMediaPlayer();
        mediaPlayer->setMedia(QUrl::fromLocalFile(finfo.absoluteFilePath()));
        videoItem = new QGraphicsVideoItem;
        videoItem->setSize(QSize(320,240));
        QGraphicsScene *scene = new QGraphicsScene(this);
        QGraphicsView *graphicsView = new QGraphicsView(scene);
        mediaPlayer->setVideoOutput(videoItem);

        QPushButton *m_playButton = new QPushButton();
        m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));

        connect(m_playButton, &QAbstractButton::clicked, [mediaPlayer]() {
            switch (mediaPlayer->state()) {
            case QMediaPlayer::PlayingState:
                mediaPlayer->pause();
                break;
            default:
                mediaPlayer->play();
                break;
            }
        });


        connect(mediaPlayer, &QMediaPlayer::stateChanged, [m_playButton, this](QMediaPlayer::State state) {
            switch(state) {
            case QMediaPlayer::PlayingState:
                m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
                break;
            default:
                m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
                break;
            }
        });



        QSlider *m_positionSlider = new QSlider(Qt::Horizontal,this);
        m_positionSlider->setRange(0,mediaPlayer->duration() / 1000);


        connect(mediaPlayer, &QMediaPlayer::positionChanged ,[m_positionSlider, this](qint64 position){

            m_positionSlider->setValue(position);
        });

        connect(mediaPlayer, &QMediaPlayer::durationChanged ,[m_positionSlider, this](qint64 duration){

            m_positionSlider->setRange(0,duration);
        });


        connect(m_positionSlider ,&QAbstractSlider::sliderMoved, [mediaPlayer ,this] (int position){

            mediaPlayer->setPosition(position);
        });

    }
});
});

这也可以帮助您,大致上与阿曼的答案相同。