美文网首页
python coding技巧

python coding技巧

作者: 凌烟阁主5221 | 来源:发表于2019-06-19 15:39 被阅读0次

主要总结记录一下关于list操作的技巧

words = ['look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes','look', 'into', 'my']

1,找出list中重复出现次数最多的元素:

1.1最简单的就是循环遍历,统计出现个数

1.2 print(max(lt, key=words.count))

1.3使用Counter 

from collections import Counter

c = Counter(words)

print(c.most_common(1)[0][0]) #出现频率最高的单词

2,使用python读取csv并转化成list of list的方法

2.1csv

import csv

f = open(file_name, 'r')

csvreader = csv.reader(f)

final_list = list(csvreader)

2.2pandas

import pandas as pd

df = pd.read_csv(filename, index_col=0)

final_list = df.tolist()

3# 计时器

def timeit(func):

    def wrapper():

        start = time.time()

        func()

        end =time.time()

        print(f'time consuming: {(end - start):.4f} seconds. ({func})')

    return wrapper

相关文章

网友评论

      本文标题:python coding技巧

      本文链接:https://www.haomeiwen.com/subject/mmskqctx.html