python file_name_of_this_app.py http://www.ijingxuan.com/#!/usr/bin/env python# -*- coding: utf-8 -*-# ***********************************************************************……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2857浏览 2090个赞
python通过collections解决约瑟夫问题据说著名犹太历史学家 Josephus有过以下的故事:在罗马人占领乔塔帕特后,39 个犹太人与Josephus及他的朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌人抓到,于是决定了一个自杀方式,41个人排成一个圆圈,由第1个人开始报数,每报数到第3人该人就必须自杀,然后再由下一个重新报数,直到所有人都自……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2911浏览 727个赞
来源:http://stackoverflow.com/questions/3806562/ways-to-move-up-and-down-the-dir-structure-in-python#Moving up/down dir structureprint os.listdir('.') # current levelpr……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1753浏览 942个赞
python超简代码解决约瑟夫环问题 约瑟环问题大家都熟悉。题目是这样的。一共有三十个人,从1-30依次编号。每次隔9个人就踢出去一个人。求踢出的前十五个人的号码。转自:http://blog.sina.com.cn/s/blog_9e11a7b40101dlzz.htmla = [ x for x in range(1,31) ] #生成编号de……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2562浏览 1567个赞
python返回昨天的日期 #-*-coding:utf-8-*- import datetimedef getYesterday(): # today=datetime.date.today() oneday=datetime.timedelta(days=1) yesterday=today-oneday ……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1868浏览 731个赞
python计算两个日期相差的天数#两个日期相隔多少天,例:2008-10-03和2008-10-01是相隔两天 def datediff(beginDate,endDate): format="%Y-%m-%d"; bd=strtodatetime(beginDate,format) ed=s……继续阅读 » 水墨上仙 4年前 (2021-03-05) 3185浏览 2714个赞
Evaluate X and Y returned from the differential equation solvers using printput frequency in Python''' printSoln(X,Y,freq). Prints X and Y returned from the di……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1961浏览 2619个赞
Python example of using Laguerre’s method to compute all the roots of equation''' roots = polyRoots(a). Uses Laguerre's method to compute all the roots of……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2326浏览 1437个赞
Powell’s method of minimizing user-supplied function in Python''' xMin,nCyc = powell(F,x,h=0.1,tol=1.0e-6) Powell's method of minimizing user-supplied fun……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1149浏览 916个赞
Plots data points and the fitting polynomial using Python''' plotPoly(xData,yData,coeff) Plots data points and the fitting polynomial defined by its coeffi……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2270浏览 926个赞
Solve simultaneous equations using the Newton-Raphson method in Python''' soln = newtonRaphson2(f,x,tol=1.0e-9). Solves the simultaneous equations f(x) = 0 by……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1605浏览 878个赞
在python中使用tempconv模块转换问题from tempconv import ctoffrom tempconv import ftoc temp = 95convTemp = ftoc(temp)print("the converted temp is " + str(convTemp))temp = 0c……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1771浏览 2395个赞
python计算指定多少天后的日期,python做日期增减是相当简单的 d1 = datetime.datetime.now()d3 = d1 + datetime.timedelta(days =10)print d3.ctime()……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2953浏览 387个赞
python中if elif条件语句实用范例marks = 99if marks >= 90: grade = 'A'elif marks >= 80: grade = 'B'elif marks >= 70: grade = 'C'elif mark……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2368浏览 2204个赞
python计算对角线有理函数插值''' p = rational(xData,yData,x) Evaluates the diagonal rational function interpolant p(x) that passes through he data points''&……继续阅读 » 水墨上仙 4年前 (2021-03-05) 3153浏览 2407个赞
Python查找函数f(x)=0的根的代码''' root = ridder(f,a,b,tol=1.0e-9). Finds a root of f(x) = 0 with Ridder's method. The root must be bracketed in (a,b).'……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1688浏览 1196个赞
python通过range函数计算一组数的和sum = 0numbers = range(1,10)for i in numbers: sum += iprint(sum)……继续阅读 » 水墨上仙 4年前 (2021-03-05) 3451浏览 2051个赞
python中localtime和gtime的区别及时区计算 localtime和gtime的区别:import timenow = time.time()time.localtime() (2007,……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2192浏览 162个赞
python中使用sharp模块的简单范例from shapes import Shapefrom shapes import Rectangle s1 = Shape(2,4)print(s1)r1 = Rectangle(4,8,3,5)print(r1)……继续阅读 » 水墨上仙 4年前 (2021-03-05) 3261浏览 2874个赞
python中while循环语句的简单实用范例number = 1while number < 20: print(number) number += 1……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1385浏览 860个赞
python自定义类并使用的演示示例class Person: def __init__(self, first, middle, last, age): self.first = first; self.middle = middle; self.last = last; self.age = ag……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1653浏览 1650个赞
python简单的函数定义和使用范例这里定义了一个温度转换的函数def convertTemp(temp, scale): if scale == "c": return (temp - 32.0) * (5.0/9.0) elif scale == "f": return t……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2321浏览 699个赞
python通过for语句遍历数据的代码演示for name in ["kak", "John", "Mani", "Matt"]: print(name)……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2982浏览 918个赞
python合并两个字典的方法,可以使用dict方法也可以使用update方法 例如dict1={1:[1,11,111],2:[2,22,222]}dict2={3:[3,33,333],4:[4,44,444]}合并两个字典得到类似{1:[1,11,111],2:[2,22,222]……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2700浏览 1961个赞
python从sqlite读取数据并显示import cgi, os, sysimport sqlite3 as db conn = db.connect('test.db')cursor = conn.cursor()conn.row_factory = db.Rowcursor.execute("select……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2687浏览 1012个赞
Python 导出数据到Excel可读取的CSV文件import csvwith open('eggs.csv', 'wb') as csvfile: #spamwriter = csv.writer(csvfile, delimiter=' ',quotechar='|……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1958浏览 1757个赞
python有一个非常好用的目录操作类库shutil,通过这个库可以很简单的复制整个目录及目录下的文件import shutil#复制文件shutil.copyfile('listfile.py', 'd:/test.py')#复制目录shutil.copytree('d:/temp',……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1832浏览 2728个赞
通过pymongo可以很容易的链接到mongodb,下面的代码链接到本地mongodb,数据库为mydb,并检索出mycollection中的所有数据输出,简单的几行代码已经做了很多事情from pymongo import Connectionconnection = Connection(‘localhost’, 27017) db = conn……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2016浏览 2394个赞
python使用nntp读取新闻组内容from nntplib import *s = NNTP('web.aioe.org')(resp, count, first, last, name) = s.group('comp.lang.python')(resp, subs) = s.xhdr('s……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2619浏览 1205个赞
python写入xml文件演示代码本范例通过xml模块对xml文件进行写入操作from xml.dom.minidom import Documentdoc = Document()people = doc.createElement("people")doc.appendChild(people)aperson = doc……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2177浏览 455个赞
这段代码通过判断操作系统里面是否存在PROGRAMFILES(X86)程序目录来判断是否是64位的系统,总感觉不是很严谨import os def Is64Windows(): return 'PROGRAMFILES(X86)' in os.environ ……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2516浏览 1974个赞
python向sqlite插入数据代码演示import sqlite3 as db conn = db.connect('mytest.db')cursor = conn.cursor()cursor.execute('insert into person values("Mani","……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2436浏览 2760个赞
windows下Python通过PIL写入字体出现“The _imagingft C module is not installed”的完美解决方法主要是因为默认安装的PIL缺少指定的库造成的,其实在windows下安装一个编译好的PIL即可 需要注意的是,安装已经编译好的PIL需要先……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2716浏览 1637个赞
python创建一个最简单的http webserver服务器import sysimport BaseHTTPServerfrom SimpleHTTPServer import SimpleHTTPRequestHandler Handler = SimpleHTTPRequestHandlerServer = BaseHTTPServer……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2614浏览 1063个赞
python分析网页上的所有超级链接import urllib, htmllib, formatter website = urllib.urlopen("http://w3mentor.com")data = website.read()website.close()format = formatter.AbstractF……继续阅读 » 水墨上仙 4年前 (2021-03-05) 3103浏览 491个赞
python抓取网页及网页上所有连接的演示代码Website crawler in pythonimport urllib, htmllib, formatter, re, sys url = sys.argv[1]website = urllib.urlopen("http://"+url)data = website.r……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1913浏览 233个赞
python使用 htmllib 分析网页内容import htmllib, urllib, formatter, sys website = urllib.urlopen("http://w3mentor.com")data = website.read()website.close()format = formatter……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2267浏览 603个赞
python判断给定的数是否是回文数此代码有很大的改进余地#! /usr/bin/env python#coding=utf-8a = input()b = str(a)flag = Truefor i in range(len(b)/2): if b[i]!=b[len(b)-i-1]: flag = False ……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2198浏览 1325个赞
python使用smtplib模块通过gmail发送邮件import smtplibfrom email.MIMEMultipart import MIMEMultipartfrom email.MIMEText import MIMEText fromaddr = 'fromaddr@gmail.com'toaddr = ……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2158浏览 918个赞
python通过imaplib模块读取gmail里的邮件import imaplib mailserver = imaplib.IMAP4_SSL('imap.gmail.com', 993)username = 'gmailusername'password = 'gmailpassword……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2661浏览 1475个赞
python操作sqlite的CRUDimport sqlite3 as db conn = db.connect('mytest.db')cursor = conn.cursor()cursor.execute("drop table if exists datecounts")cursor.execu……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2866浏览 826个赞
python查询sqlite数据表代码import sqlite3 as db conn = db.connect('mytest.db')conn.row_factory = db.Rowcursor = conn.cursor() cursor.execute("select * from person&qu……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1314浏览 197个赞
python通过ftplib登录到ftp服务器import ftplib connect = ftplib.FTP("www.mysite.com")connect.login("domain\user", "password")data = []connect.dir(data.ap……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2074浏览 576个赞
python在sqlite动态创建表代码import sqlite3 as db conn = db.connect('mytest.db')cursor = conn.cursor()cursor.execute("create table person(name text, age text, address t……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1487浏览 1493个赞
python中如果使用系统默认的open方法打开的文件只能写入ascii吗,如果要写入中文需要用到codecs模块,下面的代码向 c:/1.txt文件写入 ”你好,脚本分享网 75271.com“中文字符串# -*- coding: utf-8 -*-import codecscontent = u'你好,脚本分享网 75271.com&……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2399浏览 2828个赞