• 个人笔记(2)


    记录两个小点

    1、Python中对list中元素的去重

    例如,对于

    a = ['a', 'b', 'c', 'a', 'a', 'c']

    方法一:用内置set去重

    list(set(a))

    输出:['a', 'c', 'b']

    方法二:用dict中key的唯一性去重

    {}.fromkeys(a).keys()

    输出:['a', 'c', 'b']

    据说这种方法更快,没有亲测,不晓得。

    方法一与方法二的输出结果顺序都与原list不一致,若想保持一致,可以这样:

    sorted(set(a), key = a.index)

    输出:['a', 'b', 'c']

    其他方法:

     1 def f1(seq):
     2    # not order preserving
     3    set = {}
     4    map(set.__setitem__, seq, [])
     5    return set.keys()
     6 
     7 def f2(seq): 
     8    # order preserving
     9    checked = []
    10    for e in seq:
    11        if e not in checked:
    12            checked.append(e)
    13    return checked
    14 
    15 def f3(seq):
    16    # Not order preserving
    17    keys = {}
    18    for e in seq:
    19        keys[e] = 1
    20    return keys.keys()
    21 
    22 def f4(seq): 
    23    # order preserving
    24    noDupes = []
    25    [noDupes.append(i) for i in seq if not noDupes.count(i)]
    26    return noDupes
    27 
    28 def f5(seq, idfun=None): 
    29    # order preserving
    30    if idfun is None:
    31        def idfun(x): return x
    32    seen = {}
    33    result = []
    34    for item in seq:
    35        marker = idfun(item)
    36        # in old Python versions:
    37        # if seen.has_key(marker)
    38        # but in new ones:
    39        if marker in seen: continue
    40        seen[marker] = 1
    41        result.append(item)
    42    return result
    43 
    44 def f6(seq):
    45    # Not order preserving    
    46    set = Set(seq)
    47    return list(set)

    参考资料:

    1、http://blog.csdn.net/zhengnz/article/details/6265282

    2、http://www.benben.cc/blog/?p=386

    3、http://www.peterbe.com/plog/uniqifiers-benchmark/

    2、Python中日期的获取

    (1)获取当前时间

    方法一:

    1 import datetime
    2 datetime.datetime.now()

    输出:datetime.datetime(2013, 2, 1, 14, 13, 39, 972746)

    方法二:

    1 import time
    2 time.localtime()

    输出:time.struct_time(tm_year=2013, tm_mon=2, tm_mday=1, tm_hour=14, tm_min=14, tm_sec=41, tm_wday=4, tm_yday=32, tm_isdst=0)

    (2)对时间格式进行格式化输出

    方法一:

    datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

    输出:'2013-02-01 14:16:52'

    方法二:

    time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

    输出:'2013-02-01 14:18:17'

    (3)获取前一天日期

    (datetime.date.today() + datetime.timedelta(days=-1)).strftime('%Y-%m-%d')

    输出:'2013-01-31'

    参考资料:

    1、http://justpy.com/archives/160

    2、http://yangshuyezi.blog.hexun.com/20868441_d.html

    3、http://bbs.chinaunix.net/thread-1756983-1-1.html

    4、http://www.cnblogs.com/lwgdream/archive/2012/05/07/2489293.html 

  • 相关阅读:
    (转)运行pip报错:Fatal error in launcher: Unable to create process using '"'
    (转)pycharm autopep8配置
    Hash
    Java 8 Lambda表达式
    位运算
    PHP数组操作大全
    php final static const成员属性用法
    Java编程性能优化
    java中double变量保留小数问题
    encodeURIComponent编码后java后台的解码
  • 原文地址:https://www.cnblogs.com/meyao/p/personal_notes_two.html
Copyright © 2020-2023  润新知