关闭

python 示例开发程序

486人阅读 评论(0) 收藏 举报
分类:

1、 求1-100 的素数

# 素数 
from math import sqrt

i =2
j=2
flags = 0
for i in range(2,101):
        flags = 0
        tmp = int(sqrt(i))
        tmp = tmp + 1
        for j in range(tmp):
                if j <= 1:
                        continue
                if i%j == 0:
                        flags = 1
                        break
        if flags == 0:
                print "%d is a sushu" % (i)

2、关于 文件的一般操作

#coding=utf-8
# 文件的相关操作, 有中文一定要加这一行
f = open('lcf.py', 'rw+')
pointer = f.seek(0, 2)# 2代表尾部,0代表相对偏移
size = f.tell();
print 'file: %s, size is %d' % ('lcf/py', size)

pointer = f.seek(0, 0)#第二个0 代表 头部, 1代笔当前位置
lines = f.read()
print '1: content is: %s' % lines


pointer = f.seek(0, 0)
lines = f.read(size)
print '2: content is: %s' % lines

s = 'my name is your king'
p = f.write(s)
pointer = f.seek(0, 0)
lines = f.read(size)
print '3: content is: %s' % lines

f.close()



3、 class 对象 里面的一些操作

# 对象交换变量,传参以及判断闰年
class Bird(object):
        a = [10]
        b = [11]
        def __init__(self, words):
                print "birds happy hapy",words

        def swap(self, a, b):
                c    = a[0]
                a[0] = b[0]
                b[0] = c
        laugh = "ha"
        def  str(self):
                for i in range(5):
                        print self.laugh
        number = 0
        def  panduan(self):
                if self.number%100 == 0 and self.number%4 == 0 or self.number % 400 == 0 :
                        print "it's ", self.number , "a  run nian "
                else:
                        print "NULL"
#lcf = Bird()
#print lcf.a, lcf.b
#
#lcf.swap(lcf.a, lcf.b)
#print lcf.a, lcf.b

#lcf.str()
lcf2 = Bird('happy happy!')
lcf2.number = 2000
lcf2.panduan()

n1 = [1,34, 2,80,65,5]

for i in n1:
        print i



4、 爬 取 百度贴吧 图片

#爬取百度贴吧一些小图片
#urllib.urlretriev---将远程数据下载到本地
import urllib
import urllib2
import re

#http://tieba.baidu.com/p/3868127254
a = raw_input('inpt url:')
s = urllib2.urlopen(a)
s1 = s.read()

def getimg(aaa):
        reg = re.compile(r'img.src="(.*?)"')
        #reg = re.compile(r'<title>')
        l = re.findall(reg, aaa)
        tmp =0
        for x in l:
                tmp += 1
                urllib.urlretrieve(x, '%s.jpg' % tmp)

#print s1
getimg(s1)

5、   python 如何  使用 第三方头文件

#calc.py
#encoding: UTF-8
 
def adds(a, b):
    return a + b
#test_calc.py 
#coding: UTF-8
import nose
import calc
 
def test_adds():
    assert calc.adds(1, 2) == 4
 
if __name__ == '__main__':
    nose.runmodule()


6、a = [(3, 'URLA'), (6, 'URLB'), (33, 'URLC'), (9, 'URLA'), (365, 'URLD'), (23, 'URLA'), (8, 'URLB')]
想找出来相同字符串出现次数大于2的,然后前面的数相加求平均

#-*-coding:utf-8-*-
a = [(3, 'URLA'), (6, 'URLB'), (33, 'URLC'), (9, 'URLA'), (365, 'URLD'), (23, 'URLA'), (8, 'URLB')]
c = {}
sum_a = 0
num_a = 0
#初始化
for j in a:
    c[j[1]] = 0
print c
#计数
for i in a:
    c[i[1]] =  c[i[1]] + 1
print c
#满足条件 求解
for k in c:
        print  k  , c[k]
        if c[k] > 2:
           num_a = num_a + c[k]
           for q  in  a:
                if q[1] == k:
                        sum_a = sum_a + q[0]
avg = sum_a / num_a
print "number:%d,  sum: %d, avg=%d\n" %(num_a,sum_a,avg)



7、 为别人写的一段小程序  根据ip地理信息库,转换为   192.168.1 , 局域网这种格式   输入 192.168.1.1,192.168.1.255,局域网

#coding=utf-8
import re
import sys
import os
import socket
import struct
import string
f=open('2.txt', 'r+')
#print f.read()
f2=open('out.csv', 'w+')

def getIpNum(ip):
	num_ip = socket.ntohl(struct.unpack("I", socket.inet_aton(str(ip)))[0])
#	print num_ip
	return num_ip
	
def getIpStr(ip):
	num_ip = socket.inet_ntoa(struct.pack('I', socket.htonl(ip)))
#	print num_ip
	return num_ip


for line in f.readlines():
#	print line
	str1 = line.split(",")
#	for a in str1:
#		print a
	start= str1[0]
	end  = str1[1]
	city = str1[2]	
#	print start,end,city       
	start_s = getIpNum(start)
	end_s = getIpNum(end)+1
#	print start_s,end_s
#	getIpStr(start_s);
#	getIpStr(end_s);
	b1 = 0 
        b2 = 0 
        b3 = 0 
        b4 = 0
	j = 0;
	for i in range(start_s, end_s):
		str2 = getIpStr(i);
		str3 = str2.split('.')
		a1 = int(str3[0])
		a2 = int(str3[1])
		a3 = int(str3[2])
		a4 = int(str3[3])
		#print b1,b2,b3,b4
		if b1 == a1 and b2 == a2 and b3 == a3:
			continue
		 
		b1 = a1; b2 = a2; b3=a3; b4=a4;
				
		#print b1,b2,b3,b4
		out  = str(a1) + '.' + str(a2) + '.' + str(a3)  +','+city	
		f2.write(out)
		
	
f.close()
f2.close()
			






0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:272864次
    • 积分:4211
    • 等级:
    • 排名:第7259名
    • 原创:152篇
    • 转载:101篇
    • 译文:0篇
    • 评论:10条
    最新评论