# python练习

Python统计列表中的重复项出现的次数的方法

#方法1：

mylist = [1,2,2,2,2,3,3,3,4,4,4,4]

myset = set(mylist)  #myset是另外一个列表，里面的内容是mylist里面的无重复 项

for item in myset:

print("the %d has found %d" %(item,mylist.count(item)))

#方法2:

List=[1,2,2,2,2,3,3,3,4,4,4,4]

a = {}

for i in List:

if List.count(i)>1:

a[i] = List.count(i)

print (a)

"""利用字典的特性来实现。

>>> from collections import Counter

>>> Counter([1,2,2,2,2,3,3,3,4,4,4,4])

Counter({1: 5, 2: 3, 3: 2})

l=[1,4,2,4,2,2,5,2,6,3,3,6,3,6,6,3,3,3,7,8,9,8,7,0,7,1,2,4,7,8,9]

count_times = []

for i in l :

count_times.append(l.count(i))

m = max(count_times)

n = l.index(m)

print (l[n])

# 简单的拼手气红包

import random

from time import sleep

# 所有涉及金额的浮点数都需要用 round 方法保留2位小数，避免出现最终结果多出0.01

amount = round(float(input('请设置红包的金额 \> ')),2)

num = int(input('请设置红包的数量 \> '))

hb_dict = {}

xing = '赵钱孙李周吴郑王'

ming = '一二三四五六七八九十'

while num:

xingming = random.choice(xing)+random.choice(ming)+random.choice(ming)

if xingming in hb_dict.keys():

xingming = random.choice(xing)+random.choice(ming)+random.choice(ming)

num -= 1

if num == 0:

print('%s抢到红包%.2f元 红包抢完了!' % (xingming,amount))

hb_dict[amount] = xingming

amount -= amount

elif num > 0:

hb = round(random.uniform(0.01,amount)/num,2)

hb_dict[hb] = xingming

# 算法: 在0.01到红包总金额之间随机一个浮点数 / 红包剩余个数

print('%s抢到红包%.2f元 剩余%d个!' % (xingming,hb,num))

amount = round((amount - hb),2)

sleep(1)

# 转置字典中的 key / value

# hb_dict2 = {value:key for key,value in hb_dict.items()}

max_hb = max(hb_dict.items())

print('%s运气最佳 抢得%.2f元!!' % (max_hb[1],max_hb[0]))

mongodb基本操作及常用命令

show dbs

db

use easondb

db.mycol.insert({'id':1,'name':'Eason','age':25,'tags':['Linux','Python','MongoDB']})

db.mycol.save({'id':2,'name':'imaoxian','age':28,'tags':['C++','Java','javascript']})

db.mycol.find()

db.mycol.find().pretty()

db.mycol.find({'id':{\$lte:2}})

db.mycol.find({'id':2})

db.mycol.update({'id':2},{\$set:{'name':'Maoxian','age':29}})

db.mycol.remove({'id':2})

db.mycol.drop()

use easondb

db.dropDatabase()

python操作mongodb

import pymongo  # 导入pymongo模块

client = pymongo.MongoClient('127.0.0.1',27017)     # 创建一个mongo连接

db = client['testdb']                           # 定义一个名为testdb的 DB

sheet1 = testdb['sheet1']                       # 定义一个名为sheet1的 表

for i in range(100):

# 循环生成一组词典

data = {

'i':i,

'i*i':i*i

}

# 将词典insert到sheet1表中

sheet1.insert_one(data)

# 读取出sheet1 中的数据

for item in sheet1.find():

print(item)

python:pymysql数据库操作

import pymysql

# 获取一个数据库连接,注意如果是UTF-8类型的,需要制定数据库

db = pymysql.connect(host="127.0.0.1",user="root",passwd="123456",db="mysql",charset='utf8' )

# 使用 cursor()方法创建一个游标对象 cursor

cursor = db.cursor()

# 使用 execute()方法执行 SQL 查询

# 使用 fetchall()方法获取所有行.

data = cursor.fetchall()

print(data)

cursor.close()#关闭游标

db.close()#关闭数据库连接

import pymysql

cursor = db.cursor()

# SQL 插入数据

sql = "INSERT INTO employee (first_name, last_name, age, sex, income) " \

"VALUES ('w', 'Eason', '29', 'M', '8000')"

# SQL 更新数据

# sql = "UPDATE employee first_name = Wang WHERE first_name = w"

# SQL 删除数据

# sql = "DELETE FROM employee WHERE age > 20"

try:

cursor.execute(sql)

db.commit()

except:

db.rollback()

db.close()

import random

import string

for num in range(200):

numlist = []

for i in range(12):

numlist.append(random.choice(string.ascii_uppercase+string.digits))

# print(''.join(numlist))

with open('200.txt', 'a') as f:     # 'a' 表示追加写入

f.write(''.join(numlist)+'\n')

money = float(input('请输入您打算用来投资的本金 \> '))

year = int(input('请输入投资期限(单位：年) \> '))

rate = float(input('请输入投资年化收益率 \> '))

Type = int(input('''1.每日利息复投 2.每月利息复投 3.每年利息复投

请选择复投方式 \> '''))

def day_return(money,year,rate=0.12):

'方案：每日利息加入本金复投！'

for y in range(year):

for day in range(365):

money = money*rate/365 + money

print('第%d年结束时，本金为：%.2f' % (y+1,money))

def month_return(money,year,rate=0.12):

'方案：每月利息加入本金复投！'

for y in range(year):

for month in range(12):

money = money*rate/12 + money

print('第%d年结束时，本金为：%.2f' % (y+1,money))

def year_return(money,year,rate=0.12):

'方案：每年利息加入本金复投！'

for y in range(year):

money = money*rate + money

print('第%d年结束时，本金为：%.2f' % (y+1,money))

if Type == 1:

day_return(money,year,rate)

elif Type == 2:

month_return(money,year,rate)

elif Type == 3:

year_return(money,year,rate)

else:

print('输入有误！')

# Python 3.5.1

from urllib import request, parse

import json

url = 'http://fanyi.baidu.com/v2transapi'

context = input('请输入需要翻译的内容 :\> ')

if context >= '\u4e00' and context <= '\u9fa5':

# 判断输入内容是否为汉字

From,To = 'zh','en'

else:

From,To = 'en','zh'

data = {

'query':context,

'from':From,

'to':To,

'transtype':'translang',

'simple_means_flag':3

}

data = parse.urlencode(data).encode('utf-8')

r = request.Request(url,data)

print('翻译结果为:' + Result['trans_result']['data'][0]['dst'])

python：乘法表

# for 循环

for i in range(1,10):

for j in range(1,i+1):

print('%dx%d=%d' % (j,i,j*i),end='\t')

print()

# while 循环

m = 1

while m < 10:

n = 1

while n < m+1:

print('%dx%d=%d' % (n,m,m*n),end='\t')

n+=1

m+=1

print()

#!/usr/bin/env python

#encoding:utf-8

#Author:sean

import string

import random

#激活码中的字符和数字

field = string.letters + string.digits

#获得四个字母和数字的随机组合

def getRandom():

return ''.join(random.sample(field,4))

#生成的每个激活码中有几组

def concatenate(group):

return '-'.join([getRandom() for i in range(group)])

#生成n组激活码

def generate(n):

return [concatenate(4) for i in range(n)]

if __name__ == '__main__':

print generate(10)

#!/usr/bin/env python

#encoding:utf-8

import re

from collections import Counter

FileSource = './media/abc.txt'

def getMostCommonWord(articlefilesource):

'''输入一个英文的纯文本文件，统计其中的单词出现的个数'''

pattern = r'[A-Za-z]+|\\$?\d+%?\$'

with open(articlefilesource) as f:

return Counter(r).most_common()

if __name__ == '__main__':

print getMostCommonWord(FileSource)

#!/usr/bin/env python

#encoding:utf-8

from goose import Goose

from goose.text import StopWordsChinese

import sys

#要分析的网页url

url = '

def extract(url):

'''

提取网页正文

'''

g = Goose({'stopwords_class':StopWordsChinese})

artlcle = g.extract(url=url)

return artlcle.cleaned_text

if __name__ == '__main__':

print extract(url)

import psutil

import prettytable

import time

startTime = time.time()

port = 22  # ssh -i /etc/ssh/ssh_host_rsa_key root@10.6.28.28

# define data structure for each connection, each ip is unique unit

'counts': 0,

'stat': {

'established': 0,

'time_wait': 0,

'others': 0

}

}

# define data structure for statistics

statistics = {

'portIsUsed': False,

'portUsedCounts': 0,

'portPeerList': [

{

'counts': 0,

'stat': {

'established': 0,

'time_wait': 0,

'others': 0

},

},

]

}

tmp_portPeerList = list()

portPeerSet = set()

netstat = psutil.net_connections()

# get all ip address only for statistics data

for i, sconn in enumerate(netstat):

statistics['portIsUsed'] = True

statistics['portUsedCounts'] += 1

tmp_portPeerList.append(str(ipaddress))  # dict() list() set() is unhashable type, collections.Counter

for ip in tmp_portPeerList:

for member in portPeerSet:

statistics['portPeerList'].append(eval(member))

for sconn in netstat:

for i, item in enumerate(statistics['portPeerList']):

statistics['portPeerList'][i]['counts'] += 1

if sconn.status == 'ESTABLISHED':

statistics['portPeerList'][i]['stat']['established'] += 1

elif sconn.status == 'TIME_WAIT':

statistics['portPeerList'][i]['stat']['time_wait'] += 1

else:

statistics['portPeerList'][i]['stat']['others'] += 1

# print statistics result using prettytable

if statistics['portIsUsed']:

print "Total connections of port %s is %d." % (port, statistics['portUsedCounts'])

table = prettytable.PrettyTable()

table.field_names = ["Total Counts", "Remote IP Address", "Established Conns", "Time_wait Conns",

"Others Conns"]

for i, ip in enumerate(statistics['portPeerList']):

ip['stat']['others']])

print table.get_string(sortby=table.field_names[1], reversesort=True)

else:

print 'port %s has no connections, please make sure port is listen or in use.' % port

endTime = time.time()

print "Elapsed time: %s seconds." % (endTime - startTime)

python 收集主机信息

#!/usr/bin/env  python

"""

file name: collect_info_a.py

"""

from  subprocess  import  Popen, PIPE

def  getIfconfig():

p = Popen(['ifconfig'], stdout=PIPE, stderr=PIPE)

return data

def  getDmi():

p = Popen(['dmidecode'], stdout=PIPE, stderr=PIPE)

return data

"""

parseData(data) 的函数，将字符串分割成一个列表，每遇到顶格的行，就是

"""

def  parseData(data):

parsed_data = []

new_line = ''

data = [i for i in data.split('\n')  if i] #将字符串分割，去掉空行的元素

for  line  in data:

if  line[0].strip():  #当遇到顶格的行，就把new_line 保存的上一段信息，append 到parsed_line

parsed_data.append(new_line)

new_line = line+'\n'  #重新保存新的一段的信息

else:

new_line += line+'\n'

parsed_data.append(new_line)

return [i for i in parsed_data if i]  #去掉空行的元素

"""

parseData(data) 函数返回的就是一个处理过的列表，将收集到的ip 字符串信息和 dmidecode 字符串信息，交给

"""

def  parseIfconfig(parsed_data):

parsed_data = [i for parsed_data if i and  not i.startswith('lo')]  #将"lo" 网卡的信息去掉

dic = {}

for lines in parsed_data:

devname = lines.split('\n')[0].split()[0]

break   #由于只需要第一张网卡的信息，所以这里就可以退出循环了

return dic

def  parseDmi(parsed_data):

dic = {}

parsed_data = [i for i in parsed_data if  i.startswith('System Information')] #把这段信息直接整出来

parsed_data = [i for i in parsed_data[0].split('\n')[1:] if i ]

parsed_data = [i.strip().split(':') for i in parsed_data if i]

dmi_dic = dict(parsed_data)

dic = {}

dic['vender'] = dmi_dic['Manufacturer'].strip()

dic['product'] = dmi_dic['Product Name'].strip()

dic['sn']  = dmi_dic['Serial Number'].strip()

return dic

"""

getHostName: 函数

fn : 文件名参数

"""

def getHostName(fn):

with open(fn) as fd:

for line in fd:

if line.startswith('HOSTNAME'):

HostName = line.split('=')[1].strip()

break

return {'HostName': HostName}

"""

getOSver: 函数

fn : 文件名参数

"""

def  getOSver(fn):

with open(fn) as fd:

for line in fd:

osver = line.strip()

break

return {'osver': osver}

"""

getCpu: 函数

fn : 文件名参数

"""

def getCpu(fn):

num = 0

dic = {}

with open(fn) as fd:

for line in fd:

if line.startswith('processor'):

num += 1

if line.startswith('model name'):

model_name = line.split(':')[1]

model_name = model_name.split()[0] + ' ' + model_name.split()[-1]

dic['cpu_num'] = num

dic['cpu_model'] = model_name

return dic

"""

getMemory: 函数

fn : 文件名参数

"""

def  getMemory(fn):

with open(fn) as fd:

for line in fd:

if line.startswith('MemTotal'):

mem = int(line.split()[1].strip())

break

mem = '%s' % int((mem/1024.0)) +'M'

return {'Memory': mem}

if  __name__ == '__main__':

data_ip = getIfconfig()

parsed_data_ip = parseData(data_ip)

ip = parseIfconfig(parsed_data_ip)

data_dmi = getDmi()

parsed_data_dmi = parseData(data_dmi)

dmi = prseDmi(parsed_data_dmi)

HostName = getHostName('/etc/sysconfig/network')

osver = getOSver('/etc/issue')

cpu = getCpu('/proc/cpuinfo')

mem = getMemory('/proc/meminfo')

dic = {} #定义空字典，上面收集到的主机信息都是字典形式的，就是为了现在能将它们update 在一个字典

dic.update(ip)

dic.update(dmi)

dic.update(HostName)

dic.update(osver)

dic.update(cpu)

dic.update(mem)

print dic

python 收集ip信息

#!/usr/bin/env  python

from subprocess  import  Popen, PIPE

def  getData():

p = Popen(['ifconfig'], stdout=PIPE, stderr=PIPE)

return [i for i in data if i and  not  i.startswith('lo')]

def parseData(data):

dic = {}

for lines  in data :

devname =  lines.split('\n')[0].split()[0]

return dic

if  __name__ == "__main__":

data = getData()

print parseData(data)

#!/usr/bin/env  python

from   subprocess  import   Popen, PIPE

def  getIP():

p = Popen(['ifconfig'], stdout=PIPE, stderr=PIPE)

stdout,  stderr  =  p.communicate()

return  [i  for  i  in stdout.split('\n') if i]

def genIP(data):

lines = []

new_line = ''

for line in data:

if  line[0].strip():

lines.append(new_line)

new_line = line + '\n'

else:

new_line += line + '\n'

lines.append(new_line)

lines = [i for i in lines  if i and not i.starswith('lo')]

return lines

def parseData(data):

dic = {}

for lines in data:

devname = lines.split('\n')[0].split()[0]

return dic

if  __name__ == "__main__":

data =  getIP()

data_list = genIP(data)

print parseData(data_list)

python脚本： 双向统计文件字符、单词数、行数

#!/usr/bin/python

import sys

import os

if len(sys.argv) == 1:

else:

try:

fn = sys.argv[1]

except IndexError:

sys.exit()

if not os.path.exists(fn):

print "%s is not exits." %fn

sys.exit()

fd = open(sys.argv[1])

chars = len(data)

words = len(data.split())

lines = data.count('\n')

print "%(lines)s %(words)s %(chars)s" %locals()

python 代码统计文件的行数

#!/usr/bin/python

#encofing:utf8

# 统计文件的行数

import sys

def lineCount(fd):

n = 0

for i in fd:

n += 1

return n

fd = sys.stdin

print lineCount(fd)

python 递归法列出目录中的所有文件

import os

os.listdir('/etc/')：列出指定目录下的所有文件和目录

os.path.isdir('/etc/')：判断是否是一个目录，返回一个布尔值

os.path.isfile('/etc/passwd')：判断是否是一个文件，返回一个布尔值

os.path.join('/etc','passwd')：连接两个路径

#!/usr/bin/python

import sys

import os

def print_files(path):

lsdir=os.listdir(path)

dirs=[i for i in lsdir if os.path.isdir(os.path.join(path, i))]

files=[i for i in lsdir if os.path.isfile(os.path.join(path, i))]

if files:

for f in files:

print os.path.join(path, f)

if dirs:

for d in dirs:

print_files(os.path.join(path, d))

print_files(sys.argv[1])

python 统计单词个数---不去重

import re

pattern  = re.compile(r'\w+')

pattern.match('hello ,world')

words = pattern.findall('hello hello  world')

len(words)

python 统计列表相同值重复次数

>>> test_list = ['a',0,'a',1,'a',0,1]

>>> test_set = set(test_list)

>>> for i in test_set:

...     print('values %s times %d' % (i,test_list.count(i)))

...

values a times 3

values 0 times 2

values 1 times 2

>>> from collections import Counter

>>> test_list = ['a',0,'a',1,'a',0,1]

>>> num = Counter(test_list)

>>> num

Counter({'a': 3, 0: 2, 1: 2})

>>> num[0]

2

>>> num[1]

2

>>> num['a']

3

>>> test_list = ['a',0,'a',1,'a',0,1,6]

>>> test_dict = {}

>>> for i in test_list:

...     if test_list.count(i) >= 1:

...             test_dict[i] = test_list.count(i)

...

>>> print(test_dict)

{0: 2, 'a': 3, 6: 1, 1: 2}

0 条评论

• ### 计算机基础系列：源代码如何被计算机执行

现在各行各业的朋友都开始使用计算机解决自己的业务问题，网络上有大量的免费公开课，教我们处理数据并数学建模。Python等编程语言上手快，开源软件多，足以应付绝大...

• ### Python基础篇 strings 03

找出子字符串出现频次和出现的索引位置核查是否存在字符串并找出其索引位置查找所有字符的出现次数和索引

• ### 使用python打印99乘法表的3种写法

99乘法表就是两个数相乘，最大就是 X * X ,而不会出现 X * (X+1)，这里可以使用2个for循环，第一层是1-9，第二次最大截止第一层的数字，程序呼...

• ### Python进阶：丢失的一笔订单

2018年某天曾接到一个需求，要求给10个监考老师监考的10个科目来分配考场，要求每个老师的监考考场不能重复。见下图，不知道你感觉怎么样，我当时搞了几天没有找出...

• ### Access内置函数及举例

举例:select DateDiff("d","2006-5-1","2006-6-1")返回31,其中d可以换为yyyy,m,H等

• ### Python进阶：NumPy

NumPy(Numerical Python) 是 Python 语言的一个扩展程序库，支持大量的维度数组与矩阵运算，此外也针对数组运算提供大量的数学函数库。这...

• ### Python入门（五）：常用标准库sys和os

我们可以编写自己的模块，也可以使用大量的Python标准库模块和开源模块，站在巨人的肩上行动，正因为如此，使得当今python如此热门，今天就让我们来一起领略p...

• ### 10分钟入门Pandas-系列（1）

常规numpy数据是一种数据类型，而pandas DataFrames每一列有一种数据类型，使用DataFrame.to_numpy()时，将保持所有的数据类型...

• ### Python入门（四）：函数、类、模块

是能重用的程序段，给一块代码起一个名字，然后在其它地方使用函数名字重复调用这块代码。

• ### Python入门（一）：程序安装

Anaconda指的是一个开源的Python发行版本，其包含了conda、Python等180多个科学包及其依赖项。因为包含了大量的科学包，Anaconda 的...