• 合并dict、list的方法


    dict1={1:[1,11,111],2:[2,22,222]}
    dict2={3:[3,33,333],4:[4,44,444]}
    合并两个字典得到类似

    {1:[1,11,111],2:[2,22,222],3:[3,33,333],4:[4,44,444]}
    方法1:

    dictMerged1=dict(dict1.items()+dict2.items())
    方法2:

    dictMerged2=dict(dict1, **dict2)
    方法2等同于:

    dictMerged=dict1.copy()
    dictMerged.update(dict2)

    或者

    dictMerged=dict(dict1)
    dictMerged.update(dict2)
    方法2比方法1速度快很多,用timeit测试如下

    $ python -m timeit -s 'dict1=dict2=dict((i,i) for i in range(100))' 'dictMerged1=dict(dict1.items()+dict2.items())'
        10000 loops, best of 3: 20.7 usec per loop
    $ python -m timeit -s 'dict1=dict2=dict((i,i) for i in range(100))' 'dictMerged2=dict(dict1,**dict2)'
        100000 loops, best of 3: 6.94 usec per loop
    $ python -m timeit -s 'dict1=dict2=dict((i,i) for i in range(100))' 'dictMerged3=dict(dict1)' 'dictMerged3.update(dict2)'
        100000 loops, best of 3: 7.09 usec per loop
    $ python -m timeit -s 'dict1=dict2=dict((i,i) for i in range(100))' 'dictMerged4=dict1.copy()' 'dictMerged4.update(dict2)'
        100000 loops, best of 3: 6.73 usec per loop

    a = {'a': 1}
    b = {'b': 2}
    c = [1]
    d = ['a',2]
    print(dict(a, **b))

    print(c+d)

    输出:

    {'a': 1, 'b': 2}
    [1, 'a', 2]

    除了直接相加(生成新的list),还有两种方法(修改其中一个list):

      1. 用list的extend方法,L1.extend(L2),该方法将参数L2的全部元素添加到L1的尾部,例如:

        1

        2

        3

        4

        5

        >>> L1 = [12345]
        >>> L2 = [203040]
        >>> L1.extend(L2)
        >>> L1
        [12345203040]
      2. 用切片(slice)操作,L1[len(L1):len(L1)] = L2和上面的方法等价,例如:

        1

        2

        3

        4

        5

        6

        >>> L1 = [12345]
        >>> L2 = [203040]
        >>> L1[len(L1):len(L1)] = L2
        >>> 
        >>> L1
        [12345203040]

    但切片方法用起来更灵活,可以插入到头部,或其他任意部位,例如:

    加到开头:

    1

    2

    3

    4

    5

     

    >>> L1 = [12345]
    >>> L2 = [203040]
    >>> L1[0:0= L2
    >>> L1
    [20304012345]
      1. 加到中间:

    1
    2
    3
    4
    5
    6
    >>> L1 = [12345]
    >>> L2 = [203040]
    >>> 
    >>> L1[1:1= L2
    >>> L1
    [12030402345]

    去重合并两个list:

    法一:

    >>> a = [1,3,5,7]
    >>> b = [1,3,4,6,8]
    >>> c = list(set(a+b))
    >>> a
    [1, 3, 5, 7]
    >>> b
    [1, 3, 4, 6, 8]
    >>> c
    [1, 3, 4, 5, 6, 7, 8]

    法二:

    = [1,3,5,7]
    = [1,3,4,6,8]
    c=list(set(a).union(set(b)))
     
     
    法三:
    = [1,3,5,7]
    = [1,3,4,6,8]
    = list(set(a) | set(b))
     
     
     
     
     

    问题就是对一个list中的新闻id进行去重,去重之后要保证顺序不变。

    直观方法

    最简单的思路就是:

    代码如下:

    ids = [1,2,3,3,4,2,3,4,5,6,1]
    news_ids = []
    for id in ids:
        if id not in news_ids:
            news_ids.append(id)

    print news_ids

    这样也可行,但是看起来不够爽。

    用set

    另外一个解决方案就是用set:


    ids = [1,4,3,3,4,2,3,4,5,6,1]
    ids = list(set(ids))

    这样的结果是没有保持原来的顺序。

    按照索引再次排序

    最后通过这种方式解决:


    ids = [1,4,3,3,4,2,3,4,5,6,1]
    news_ids = list(set(ids))
    news_ids.sort(ids.index)

    使用itertools.grouby

    文章一开始就提到itertools.grouby, 如果不考虑列表顺序的话可用这个:


    ids = [1,4,3,3,4,2,3,4,5,6,1]
    ids.sort()
    it = itertools.groupby(ids)

    for k, g in it:
        print k

    关于itertools.groupby的原理可以看这里:http://docs.python.org/2/library/itertools.html#itertools.groupby

    网友补充:用reduce

    网友reatlk留言给了另外的解决方案。我补充并解释到这里:


    In [5]: ids = [1,4,3,3,4,2,3,4,5,6,1]

    In [6]: func = lambda x,y:x if y in x else x + [y]

    In [7]: reduce(func, [[], ] + ids)
    Out[7]: [1, 4, 3, 2, 5, 6]


    上面是我在ipython中运行的代码,其中的 lambda x,y:x if y in x else x + [y] 等价于 lambda x,y: y in x and x or x+[y] 。

    思路其实就是先把ids变为[[], 1,4,3,......] ,然后在利用reduce的特性。reduce解释参看这里:http://docs.python.org/2/library/functions.html#reduce

  • 相关阅读:
    Unknown failure (Failure
    javascript中offsetWidth、clientWidth、width、scrollWidth、clientX、screenX、offsetX、pageX
    Pandas透视表(pivot_table)详解
    机器学习之开源库总结
    VS常用快捷键(2012)
    解决新版Pycharm中Matplotlib图像不在弹出独立的显示窗口问题
    VS中使用C的一些函数报错的问题
    基于 VS2019 配置 opencv4.x
    lena全身像
    机器学习:频率派和贝叶斯派
  • 原文地址:https://www.cnblogs.com/wozijisun/p/6377571.html
Copyright © 2020-2023  润新知