python字典基本操作范例代码d2 = {'spam': 2, 'ham': 1, 'eggs': 3} # make a dictionaryprint d2 # order is scrambledd2……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2107浏览 2458个赞
python中字典赋值方法x = {}y = xx['key'] = 'value'print yx = {}print yx = {}y = xy['key'] = 'value'print yprint x.clear()print y……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2711浏览 300个赞
python修改字典内key对应的值d2 = {'spam': 2, 'ham': 1, 'eggs': 3} # make a dictionaryprint d2 # order is scrambledd2[……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3030浏览 2632个赞
python保存字符串到文件def save(filename, contents): fh = open(filename, 'w') fh.write(contents) fh.close() save('file.name', 'some stuff&……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3035浏览 1840个赞
python从文件读取文本# get contents of file as a list, one line per element data = open('/path/to/file').readlines() ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1834浏览 1290个赞
python按单词翻转字符串,如 hello world 翻转成world hellodef reverseWords(s): return ' '.join(reversed(s.split(' '))) ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1441浏览 1263个赞
python检测是文件和是目录import os if os.path.isdir(path): print "it's a directory" elif os.path.isfile(path): print "it's a normal file" ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1746浏览 2409个赞
python返回3天后的日期import datetimeday3 = datetime.datetime.now() + datetime.timedelta(days=3)……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3562浏览 850个赞
python删除指定文件import os try: os.unlink('/path/to/file') except OSError: pass # Deletion failed... ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2366浏览 495个赞
python创建临时文件夹import tempfile, os tempfd, tempname = tempfile.mkstemp('.suffix') os.write(tempfd, "aString") # or, if you want a file-object: os.fdopen(te……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2563浏览 638个赞
给定一组指定面额的硬币,有多少中方法可以组合成指定的金额算法#!/usr/bin/python#+# This script computes the number of different ways that combinations# of a given set of coin denominations can add up to a s……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2049浏览 2070个赞
这个函数给定日期,输出星期几,到底0是星期一还是星期天,这和时区有关,反正我这是0表示星期一def get_week_day(date): week_day_dict = { 0 : '星期一', 1 : '星期二', 2 : '星期三'……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2029浏览 1211个赞
python读取文件到字节数组def get_bytes_from_file(filename): return open(filename, "rb").read() ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3813浏览 1968个赞
归并排序python实现代码def mergesort(arr): if len(arr) == 1: return arr m = len(arr) / 2 l = mergesort(arr[:m]) r = mergesort(arr[m:]) i……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1629浏览 498个赞
python列出指定目录下的文件和子目录# if you know the exact name: import os files = os.listdir('/path/to/dir/') # if you want shell-style globbing: import glob files = gl……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3488浏览 398个赞
python实现插入法排序算法def insertsort(array): for removed_index in range(1, len(array)): removed_value = array[removed_index] insert_index = removed_index ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2615浏览 2749个赞
python获得当前工作目录import os curDir = os.getcwd() ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3030浏览 1992个赞
python生成随机密码或随机字符串import string,random def makePassword(minlength=5,maxlength=25): length=random.randint(minlength,maxlength) letters=string.ascii_letters+string.digit……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2029浏览 607个赞
python信息替换演示代码Escape = "^" def subst(Msg, *Args) : """substitutes Args into Msg.""" Result = "" while Tr……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1730浏览 702个赞
python创建文件的方法file("filename", "w").close() ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2003浏览 2592个赞
python生成随机数代码import random # Generate a random integer between 0 and n, exclusive random.randrange(n) # Generate a random integer between m and n, inclusive random……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1911浏览 2630个赞
python实现的堆排序算法代码def heapSort(a): def sift(start, count): root = start while root * 2 + 1 < count: child = root * 2 + 1 ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3045浏览 875个赞
python 给数组按片赋值,这段代码可以直接给数组的4-6个元素赋值inventory = ["sword", "armor", "shield", "healing potion"]inventory[4:6] = ["orb of future telli……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1770浏览 2250个赞
python通过加号运算符连接两个列表演示代码inventory = ["sword", "armor", "shield", "healing potion"]chest = ["gold", "gems"]print &quo……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2665浏览 2476个赞
python比较两个列表是否相等,本代码演示了 == 和 is两种方法的区别L1 = [1, ('a', 3)] # same value, unique objectsL2 = [1, ('a', 3)]print L1 == L2, L1 is L2 # equivalen……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3260浏览 2036个赞
python比较两个列表的大小L1 = [1, ('a', 3)]L2 = [1, ('a', 2)]print L1 < L2, L1 == L2, L1 > L2 # less,equal,greater: tuple of results……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3489浏览 2205个赞
将一个Etree XML结构转换为一个Python dict+list格式的代码来源:http://code.activestate.com/recipes/578244-lxml-etree-xml-object-to-basic-python-dictlists/?in=lang-pythonfrom lxml import etree, obje……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1361浏览 1178个赞
python通过加号运算符操作列表li = ['a', 'b', 'mpilgrim'] li = li + ['example', 'new'] print li li += ['two……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3176浏览 1727个赞
python实现的ping#!/usr/bin/env python#coding:utf-8import os, sys, socket, struct, select, time # From /usr/include/linux/icmp.h; your milage may vary.ICMP_ECHO_REQUEST = 8 # S……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2595浏览 2703个赞
python 中文件路径和url的相互转换import urllib pathname = 'path/to/file/or/folder/' url = urllib.pathname2url(pathname) pathname = urllib.url2pathname(url)……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3180浏览 1074个赞
python将字符串转换成单词首字母大写的标题格式 方法1a = 'hello world! how are you?' b = ' '.join(i.capitalize() for i in a.split(' '……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1858浏览 108个赞
python计算指定字符串在另一个字符串中出现的次数s = "Count, the number,, of commas." print s.count(",") 输出3……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2492浏览 153个赞
python创建目录并更改权限import os os.mkdir("foo", 0666) ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2947浏览 2287个赞
python计算文本文件的行数filename = "somefile.txt" myfile = open(filename) lines = len(myfile.readlines()) print "There are %d lines in %s" % (lines, filename) ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2682浏览 2334个赞
python转换字符串为摩尔斯电码chars = ",.0123456789?abcdefghijklmnopqrstuvwxyz"codes = """--..-- .-.-.- ----- .---- ..--- ...-- ....- ..... -.... --... ---.. ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3157浏览 604个赞
python文件拷贝代码,这段代码演示了通过文件名拷贝和通过文件对象拷贝import shutil # copy files by name shutil.copyfile('/path/to/file', '/path/to/other/phile') # copy file-objec……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3028浏览 390个赞
python获得文件大小的代码TheFileSize = os.path.getsize(TheFileName) ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3783浏览 1639个赞
python字符串和整形的相互转换print int("42"), str(42) # convert from/to stringprint int("42") + 1 # force addition……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3582浏览 598个赞
python获取数组元素的个数mySeq = [1,2,3,4,5] len(mySeq) ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1942浏览 1472个赞
python更新列表(数组)aList = [123, 'abc', 4.56, ['inner', 'list'], (7-9j)]print aList[2]aList[2] = 'float replacer'print aListaList.append(&……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2075浏览 1085个赞
遗传算法的神经网络python实现代码## {{{ http://code.activestate.com/recipes/578241/ (r1)from operator import itemgetter, attrgetterimport mathimport randomimport stringimport timeitfrom……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2160浏览 421个赞
python列表操作:追加元素到列表scores = ["1","2","3"]# add a scorescore = int(raw_input("What score did you get?: "))scores.append(score)# list hi……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2628浏览 2865个赞
python列表操作extend和append的区别演示代码li = ['a', 'b', 'c'] li.extend(['d', 'e', 'f']) print li pri……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2102浏览 320个赞
python检查字符串是否是正确的ISBNdef isISBN(isbn): """Checks if the passed string is a valid ISBN number.""" if len(isbn) != 10 or not isbn[:9].isd……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2417浏览 487个赞
如果是以指定的字符串结束则返回Truename = raw_input('What is your name? ')if name.endswith('Yin'): print 'Hello, Mr. Yin'……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2281浏览 2989个赞