Python3无法根据另一本字典对列表进行正确排序

时间:2019-03-05 14:02:13

标签: python-3.x sorting

我有以下代码:

#include <memory>
#include <mutex>
#include <condition_variable>
#include <deque>

template <typename T>
class BlockingQueue {
public:
    void push(std::unique_ptr<T>&& item)
    {
        std::unique_lock<std::mutex> mlock(mutex_);
        queue_.push_back(std::move(item));
        cond_.notify_one();
    }

    std::unique_ptr<T> pop()
    {
        std::unique_lock<std::mutex> mlock(mutex_);
        if (queue_.empty()) {
            cond_.wait(mlock, [this] { return !queue_.empty(); });
        }
        std::unique_ptr<T> ret = std::unique_ptr<T>(queue_.front().release());
        queue_.pop_front();
        return ret;
    }

private:
    std::deque<std::unique_ptr<T>> queue_;
    std::mutex mutex_;
    std::condition_variable cond_;
};

应该根据refseq_dict = {'NM': 0, 'NC': 1, 'NG': 2, 'NP': 3, 'NR': 4, 'XM': 5, 'XP': 6, 'XR': 7, 'WP': 8} ensembl_dict = {'transcript_ablation': 0, 'splice_acceptor_variant': 1, 'splice_donor_variant': 2, 'stop_gained': 3, 'frameshift_variant': 4, 'stop_lost': 5, 'start_lost': 6, 'transcript_amplification': 7,'inframe_insertion': 8, 'inframe_deletion': 9, 'missense_variant': 10, 'protein_altering_variant': 11, 'splice_region_variant': 12, 'incomplete_terminal_codon_variant': 13, 'start_retained_variant': 14, 'stop_retained_variant': 15, 'synonymous_variant': 16, 'coding_sequence_variant': 17, 'mature_miRNA_variant': 18, '5_prime_UTR_variant': 19, '3_prime_UTR_variant': 20, 'non_coding_transcript_exon_variant': 21, 'intron_variant': 22, 'NMD_transcript_variant': 23, 'non_coding_transcript_variant': 24, 'upstream_gene_variant': 25, 'downstream_gene_variant': 26, 'TFBS_ablation': 27,'TFBS_amplification': 28, 'TF_binding_site_variant': 29, 'regulatory_region_ablation': 30, 'regulatory_region_amplification': 31,'feature_elongation': 32, 'regulatory_region_variant': 33, 'feature_truncation': 34, 'intergenic_variant': 35} def get_index(s): by_tabs = s.split("\t") if "_" in by_tabs[4]: by_underscore = by_tabs[4].split("_") return refseq_dict[by_underscore[0]] elif "," not in by_tabs[6]: return ensembl_dict[by_tabs[6]] elif "," in by_tabs[6]: by_commas = by_tabs[6].split(",") by_commas = sorted(by_commas, key = lambda x: list(ensembl_dict.keys()).index(x))[0] return ensembl_dict[by_commas] d = {'rs570876887': ['rs570876887\tchr17:20501765-20501766\t-\tENSG00000214822\tENST00000439127\tTranscript\tsplice_acceptor_variant,intron_variant,non_coding_transcript_variant\t-\t-\t-\t-\t-\trs570876887\tTG\tHIGH\t-\t-1\t-\tdeletion\tKRT16P3\tHGNC\tHGNC:37808\ttranscribed_unprocessed_pseudogene\t-\t-\t-\t-\t-\t-\t-\t-\t-\tEnsembl\tTG\tTG\t-\t-\t-\t-\t-\t7/7\t-\tENST00000439127.6:n.1189-3_1189-2del\t-\t-\t0.0140\t0.0272\t0.0086\t0.001\t0.0109\t0.0164\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t0.0272\tAFR\t-\t-\t-\t-\n', 'rs570876887\tchr17:20501765-20501766\t-\tENSG00000214822\tENST00000580113\tTranscript\tdownstream_gene_variant\t-\t-\t-\t-\t-\trs570876887\tTG\tMODIFIER\t773\t-1\t-\tdeletion\tKRT16P3\tHGNC\tHGNC:37808\tprocessed_transcript\t-\t3\t-\t-\t-\t-\t-\t-\t-\tEnsembl\tTG\tTG\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t0.0140\t0.0272\t0.0086\t0.001\t0.0109\t0.0164\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t0.0272\tAFR\t-\t-\t-\t-\n', 'rs570876887\tchr17:20501765-20501766\t-\tENSG00000214822\tENST00000580621\tTranscript\tsplice_acceptor_variant,intron_variant,non_coding_transcript_variant\t-\t-\t-\t-\t-\trs570876887\tTG\tHIGH\t-\t-1\t-\tdeletion\tKRT16P3\tHGNC\tHGNC:37808\tretained_intron\tYES\t1\t-\t-\t-\t-\t-\t-\t-\tEnsembl\tTG\tTG\t-\t-\t-\t-\t-\t6/6\t-\tENST00000580621.5:n.1689-3_1689-2del\t-\t-\t0.0140\t0.0272\t0.0086\t0.001\t0.0109\t0.0164\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t0.0272\tAFR\t-\t-\t-\t-\n', 'rs570876887\tchr17:20501765-20501766\t-\tENSG00000214822\tENST00000584759\tTranscript\tnon_coding_transcript_exon_variant\t428-429\t-\t-\t-\t-\trs570876887\tTG\tMODIFIER\t-\t-1\t-\tdeletion\tKRT16P3\tHGNC\tHGNC:37808\tretained_intron\t-\t5\t-\t-\t-\t-\t-\t-\t-\tEnsembl\tTG\tTG\t-\t-\t-\t-\t2/2\t-\t-\tENST00000584759.1:n.428_429del\t-\t-\t0.0140\t0.0272\t0.0086\t0.001\t0.0109\t0.0164\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t0.0272\tAFR\t-\t-\t-\t-\n', 'rs570876887\tchr17:20501765-20501766\t-\t644945\tNR_029393.1\tTranscript\tsplice_acceptor_variant,intron_variant,non_coding_transcript_variant\t-\t-\t-\t-\t-\trs570876887\tTG\tHIGH\t-\t-1\t-\tdeletion\tKRT16P3\tEntrezGene\tHGNC:37808\ttranscribed_pseudogene\t-\t-\t-\t-\t-\t-\t-\t-\t-\tRefSeq\tTG\tTG\tOK\t-\t-\t-\t-\t6/6\t-\tNR_029393.1:n.1706-3_1706-2del\t-\t-\t0.0140\t0.0272\t0.0086\t0.001\t0.0109\t0.0164\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t0.0272\tAFR\t-\t-\t-\t-\n']} for key, value in d.items(): ll = sorted(value, key = get_index) for i in ll: print(i) valuerefseq_dict列表进行排序。我将其保存在ensembl_dict下,并使用script.py启动它。但是,我注意到输出是随机的。预期得到以下输出:

python3 script.py

但是有时候我会得到:

rs570876887 chr17:20501765-20501766 -   644945  NR_029393.1 Transcript  splice_acceptor_variant,intron_variant,non_coding_transcript_variant    -   -   -   -   -   rs570876887 TG  HIGH    -   -1  -   deletion    KRT16P3 EntrezGene  HGNC:37808  transcribed_pseudogene  -   -   -   -   -   -   -   -   -   RefSeq  TG  TG  OK  -   -   -   -   6/6 -   NR_029393.1:n.1706-3_1706-2del  -   -   0.0140  0.0272  0.0086  0.001   0.0109  0.0164  -   -   -   -   -   -   -   -   -   -   -   0.0272  AFR -   -   -   -
rs570876887 chr17:20501765-20501766 -   ENSG00000214822 ENST00000584759 Transcript  non_coding_transcript_exon_variant  428-429 -   -   -   -   rs570876887 TG  MODIFIER    -   -1  -   deletion    KRT16P3 HGNC    HGNC:37808  retained_intron -   5   -   -   -   -   -   -   -   Ensembl TG  TG  -   -   -   -   2/2 -   -   ENST00000584759.1:n.428_429del  -   -   0.0140  0.0272  0.0086  0.001   0.0109  0.0164  -   -   -   -   -   -   -   -   -   -   -   0.0272  AFR -   -   -   -
rs570876887 chr17:20501765-20501766 -   ENSG00000214822 ENST00000439127 Transcript  splice_acceptor_variant,intron_variant,non_coding_transcript_variant    -   -   -   -   -   rs570876887 TG  HIGH    -   -1  -   deletion    KRT16P3 HGNC    HGNC:37808  transcribed_unprocessed_pseudogene  -   -   -   -   -   -   -   -   -   Ensembl TG  TG  -   -   -   -   -   7/7 -   ENST00000439127.6:n.1189-3_1189-2del    -   -   0.0140  0.0272  0.0086  0.001   0.0109  0.0164  -   -   -   -   -   -   -   -   -   -   -   0.0272  AFR -   -   -   -
rs570876887 chr17:20501765-20501766 -   ENSG00000214822 ENST00000580621 Transcript  splice_acceptor_variant,intron_variant,non_coding_transcript_variant    -   -   -   -   -   rs570876887 TG  HIGH    -   -1  -   deletion    KRT16P3 HGNC    HGNC:37808  retained_intron YES 1   -   -   -   -   -   -   -   Ensembl TG  TG  -   -   -   -   -   6/6 -   ENST00000580621.5:n.1689-3_1689-2del    -   -   0.0140  0.0272  0.0086  0.001   0.0109  0.0164  -   -   -   -   -   -   -   -   -   -   -   0.0272  AFR -   -   -   -
rs570876887 chr17:20501765-20501766 -   ENSG00000214822 ENST00000580113 Transcript  downstream_gene_variant -   -   -   -   -   rs570876887 TG  MODIFIER    773 -1  -   deletion    KRT16P3 HGNC    HGNC:37808  processed_transcript    -   3   -   -   -   -   -   -   -   Ensembl TG  TG  -   -   -   -   -   -   -   -   -   -   0.0140  0.0272  0.0086  0.001   0.0109  0.0164  -   -   -   -   -   -   -   -   -   -   -   0.0272  AFR -   -   -   -

我的代码有什么问题?

1 个答案:

答案 0 :(得分:1)

get_index函数-排序的关键-为列表列表中的2个值返回相同的值

v = list(d.values())
len(v)#returns 1
len(*v)#returns 5
[get_index(x) for x in v[0]]#returns [1, 26, 1, 21, 4]

由于第一和第三项返回相同的key值-结果不一致。您可能需要更新get_index函数以区分2