zoukankan      html  css  js  c++  java
  • [Python] Optimizing code with numpy intersect1d, and Set

    import time
    import pandas as pd
    import numpy as np
    with open('books_published_last_two_years.txt') as f:
        recent_books = f.read().split('
    ')
        
    with open('all_coding_books.txt') as f:
        coding_books = f.read().split('
    ')
    start
    = time.time() recent_coding_books = [] for book in recent_books: if book in coding_books: recent_coding_books.append(book) print(len(recent_coding_books)) print('Duration: {} seconds'.format(time.time() - start))

    Tip #1: Use vector operations over loops when possible

    Use numpy's `intersect1d` method to get the intersection of the `recent_books` and `coding_books` arrays.

    start = time.time()
    recent_coding_books = np.intersect1d(recent_books, coding_books)
    print(len(recent_coding_books))
    print('Duration: {} seconds'.format(time.time() - start))

    Tip #2: Know your data structures and which methods are faster
    Use the set's `intersection` method to get the common elements in `recent_books` and `coding_books`.

    start = time.time()
    recent_coding_books =  set(recent_books).intersection(coding_books)
    print(len(recent_coding_books))
    print('Duration: {} seconds'.format(time.time() - start))
  • 相关阅读:
    【建站经验】 一个成熟的大型网站系统架构演化之路
    Puppet 安装配置
    SHELL二十篇(读书笔记)
    LINUX常见小问题汇总
    shell eval命令使用
    javaweb三大框架SSH
    Java Web(八) MVC和三层架构
    在CMD中操作mysql数据库出现中文乱码解决方案
    编码与解码
    JSONP
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12943433.html
Copyright © 2011-2022 走看看