python获取数组元素的几种方法L = ['spam', 'Spam', 'SPAM!']print L[2] # offsets start at zeroprint L[-2] ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2083浏览 1331个赞
python字典复制演示代码L = [1,2,3]D = {'a':1, 'b':2}A = L[:] # instead of: A = L (or list(L))B = D.copy() # instead of: B = DA[1] = 'Ni……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2965浏览 1850个赞
python字典基本操作范例代码d2 = {'spam': 2, 'ham': 1, 'eggs': 3} # make a dictionaryprint d2 # order is scrambledd2……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1870浏览 2535个赞
python中字典赋值方法x = {}y = xx['key'] = 'value'print yx = {}print yx = {}y = xy['key'] = 'value'print yprint x.clear()print y……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1957浏览 1817个赞
python修改字典内key对应的值d2 = {'spam': 2, 'ham': 1, 'eggs': 3} # make a dictionaryprint d2 # order is scrambledd2[……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3571浏览 282个赞
python保存字符串到文件def save(filename, contents): fh = open(filename, 'w') fh.write(contents) fh.close() save('file.name', 'some stuff&……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2962浏览 899个赞
python从文件读取文本# get contents of file as a list, one line per element data = open('/path/to/file').readlines() ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1483浏览 2752个赞
python按单词翻转字符串,如 hello world 翻转成world hellodef reverseWords(s): return ' '.join(reversed(s.split(' '))) ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2736浏览 1275个赞
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) 2172浏览 699个赞
python返回3天后的日期import datetimeday3 = datetime.datetime.now() + datetime.timedelta(days=3)……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1923浏览 960个赞
python删除指定文件import os try: os.unlink('/path/to/file') except OSError: pass # Deletion failed... ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1853浏览 2668个赞
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) 1608浏览 1220个赞
给定一组指定面额的硬币,有多少中方法可以组合成指定的金额算法#!/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) 1314浏览 2594个赞
这个函数给定日期,输出星期几,到底0是星期一还是星期天,这和时区有关,反正我这是0表示星期一def get_week_day(date): week_day_dict = { 0 : '星期一', 1 : '星期二', 2 : '星期三'……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1629浏览 427个赞
python读取文件到字节数组def get_bytes_from_file(filename): return open(filename, "rb").read() ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3953浏览 1520个赞
归并排序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) 2352浏览 2760个赞
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) 2894浏览 1505个赞
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) 1525浏览 2620个赞
python获得当前工作目录import os curDir = os.getcwd() ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3326浏览 2647个赞
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) 2383浏览 2109个赞
python信息替换演示代码Escape = "^" def subst(Msg, *Args) : """substitutes Args into Msg.""" Result = "" while Tr……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2995浏览 1695个赞
python创建文件的方法file("filename", "w").close() ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3715浏览 1028个赞
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) 1707浏览 2244个赞
python实现的堆排序算法代码def heapSort(a): def sift(start, count): root = start while root * 2 + 1 < count: child = root * 2 + 1 ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1611浏览 2198个赞
python 给数组按片赋值,这段代码可以直接给数组的4-6个元素赋值inventory = ["sword", "armor", "shield", "healing potion"]inventory[4:6] = ["orb of future telli……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2151浏览 1357个赞
python通过加号运算符连接两个列表演示代码inventory = ["sword", "armor", "shield", "healing potion"]chest = ["gold", "gems"]print &quo……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1733浏览 636个赞
python比较两个列表是否相等,本代码演示了 == 和 is两种方法的区别L1 = [1, ('a', 3)] # same value, unique objectsL2 = [1, ('a', 3)]print L1 == L2, L1 is L2 # equivalen……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2241浏览 2562个赞
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) 2683浏览 1458个赞
将一个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) 3172浏览 1665个赞
python通过加号运算符操作列表li = ['a', 'b', 'mpilgrim'] li = li + ['example', 'new'] print li li += ['two……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2234浏览 1161个赞
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) 2167浏览 1635个赞
python 中文件路径和url的相互转换import urllib pathname = 'path/to/file/or/folder/' url = urllib.pathname2url(pathname) pathname = urllib.url2pathname(url)……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2700浏览 2623个赞
python将字符串转换成单词首字母大写的标题格式 方法1a = 'hello world! how are you?' b = ' '.join(i.capitalize() for i in a.split(' '……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1616浏览 969个赞
python计算指定字符串在另一个字符串中出现的次数s = "Count, the number,, of commas." print s.count(",") 输出3……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2335浏览 2259个赞
python创建目录并更改权限import os os.mkdir("foo", 0666) ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1992浏览 1192个赞
python计算文本文件的行数filename = "somefile.txt" myfile = open(filename) lines = len(myfile.readlines()) print "There are %d lines in %s" % (lines, filename) ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2914浏览 780个赞
python转换字符串为摩尔斯电码chars = ",.0123456789?abcdefghijklmnopqrstuvwxyz"codes = """--..-- .-.-.- ----- .---- ..--- ...-- ....- ..... -.... --... ---.. ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2646浏览 196个赞
python文件拷贝代码,这段代码演示了通过文件名拷贝和通过文件对象拷贝import shutil # copy files by name shutil.copyfile('/path/to/file', '/path/to/other/phile') # copy file-objec……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1936浏览 1356个赞
python获得文件大小的代码TheFileSize = os.path.getsize(TheFileName) ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2750浏览 840个赞
python字符串和整形的相互转换print int("42"), str(42) # convert from/to stringprint int("42") + 1 # force addition……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3963浏览 708个赞
python获取数组元素的个数mySeq = [1,2,3,4,5] len(mySeq) ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2896浏览 2608个赞
python更新列表(数组)aList = [123, 'abc', 4.56, ['inner', 'list'], (7-9j)]print aList[2]aList[2] = 'float replacer'print aListaList.append(&……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3388浏览 2425个赞
遗传算法的神经网络python实现代码## {{{ http://code.activestate.com/recipes/578241/ (r1)from operator import itemgetter, attrgetterimport mathimport randomimport stringimport timeitfrom……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1961浏览 2042个赞
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) 2054浏览 1637个赞
python列表操作extend和append的区别演示代码li = ['a', 'b', 'c'] li.extend(['d', 'e', 'f']) print li pri……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2734浏览 1762个赞