Python 实现把两个排好序的的列表合并成一个排序列表

列表是生序的

# -*- coding: utf-8 -*-

# 合并两个排序的数组

def merge_list(a, b):
    if not a:
        return b
    if not b:
        return a
    a_index = b_index = 0
    ret = []
    while a_index < len(a) and b_index < len(b):
        if a[a_index] <= b[b_index]:
            ret.append(a[a_index])
            a_index += 1
        else:
            ret.append(b[b_index])
            b_index += 1
    if a_index < len(a):
        ret.extend(a[a_index:])
    if b_index < len(b):
        ret.extend(b[b_index:])
    return ret

if __name__ == ‘__main__‘:
    a = [1, 2, 3, 5, 7, 9]
    b = [1, 2, 2, 4, 5, 6, 8, 10]
    print merge_list(a, b)

原文地址:https://www.cnblogs.com/qiaojushuang/p/8525863.html

时间: 2024-08-29 18:28:39

Python 实现把两个排好序的的列表合并成一个排序列表的相关文章

将两个排好序的数组,合并到另外一个数组中,并且合并之后的数组也是有序的。

int a[3] = {12, 15, 17}; int b[4] = { 2, 8, 16, 22}; int c[7] = {0}; int i = 0, j = 0, k = 0; while (i < 3 && j < 4 ) { if (a[i] > b[j]) { c[k++] = b[j++]; } else { c[k++] = a[i++]; } } while (i < 3) { c[k++] = a[i++]; } while (j <

【LeetCode-面试算法经典-Java实现】【021-Merge Two Sorted Lists(合并两个排好序的单链表)】

[021-Merge Two Sorted Lists(合并两个排好序的单链表)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 题目大意 合并两个排序链表并返回一个新的列表.新的链表的

将两个排好序的序列合并成一个(指针和数组分别实现)

1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { 12 List

python面试题之--打乱排好序的list

例如: >>> a = range(10) >>> a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> import random >>> random.shuffle(a) >>> a [7, 8, 9, 4, 6, 2, 0, 1, 3, 5] random.shuffle的函数原型为:random.shuffle(x[, random]),用于将一个列表中的元素打乱

[Python练习]对比两个目录里面的文件差异,并给出差异列表

#!/usr/bin/env python import os import sys def get_Curlist(path):         cur_Files = os.listdir(path)         all_Files = []         for file_Name in cur_Files:                 full_Name = os.path.join(path,file_Name)                 all_Files.appen

合并两个已经排好序的不等长的数组

给两个已经排好序的数组,这两个数组的长度可能不相等,如何将他们合并? package airth; public class TestMergeArray { /**  * 功能:  * 作者: jiangfuqiang  * 创建日期:2014-10-13  * 修改者: mender  * 修改日期: modifydate  * @param args  */ public static void main(String[] args) { // TODO Auto-generated me

【算法导论学习-016】两个已排过序的等长数组的中位数(median of two sorted arrays)

问题来源 <算法导论>P223 9.3-8: Let X[1..n] and Y[1..n] be two arrays, each containing nnumbers already in sorted order. Give an O(lgn)-time algorithm to find themedian of all 2n elements in arrays X and Y. 翻译过来即:求两个等长(n个元素)的已排序数组A和B的中位数 方案1:对两个数组进行归并直到统计到第n

合并两个己排好序的链表及数组

唉,这么简单的东西,说简单是简单,关键是要把这东西写得好,老少兼知.应对所有测试用例,那就有点难了吧. 话说天下之事,作于细. 我们用图来说说吧: 看合并的步骤: (1) (2) (3) (4) 源代码: #ifndef COMBINE_TWO_LIST_H #define COMBINE_TWO_LIST_H #include"reverseList.h" ListNode *combineTwoList(ListNode *alist,ListNode *blist){ if(al

[经典面试题]输入一个排好序的数组的一个旋转,输出旋转数组的最小元素。

[题目] 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个排好序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3, 4, 5, 1, 2}为{1, 2, 3, 4, 5}的一个旋转,该数组的最小值为1. [分析] 这道题最直观的解法并不难.从头到尾遍历数组一次,就能找出最小的元素,时间复杂度显然是O(N).但这个思路没有利用输入数组的特性,我们应该能找到更好的解法. 我们注意到旋转之后的数组实际上可以划分为两个排序的子数组,而且前面的子数组的元素都大于或者等于后面