python获取数组元素的几种方法L = ['spam', 'Spam', 'SPAM!']print L[2] # offsets start at zeroprint L[-2] ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2779浏览 2338个赞
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) 2312浏览 836个赞
python字典基本操作范例代码d2 = {'spam': 2, 'ham': 1, 'eggs': 3} # make a dictionaryprint d2 # order is scrambledd2……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1990浏览 151个赞
python中字典赋值方法x = {}y = xx['key'] = 'value'print yx = {}print yx = {}y = xy['key'] = 'value'print yprint x.clear()print y……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3037浏览 103个赞
python修改字典内key对应的值d2 = {'spam': 2, 'ham': 1, 'eggs': 3} # make a dictionaryprint d2 # order is scrambledd2[……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2341浏览 1054个赞
python保存字符串到文件def save(filename, contents): fh = open(filename, 'w') fh.write(contents) fh.close() save('file.name', 'some stuff&……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1406浏览 1444个赞
python从文件读取文本# get contents of file as a list, one line per element data = open('/path/to/file').readlines() ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2013浏览 1395个赞
python按单词翻转字符串,如 hello world 翻转成world hellodef reverseWords(s): return ' '.join(reversed(s.split(' '))) ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1659浏览 1163个赞
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) 2740浏览 2824个赞
python返回3天后的日期import datetimeday3 = datetime.datetime.now() + datetime.timedelta(days=3)……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2642浏览 797个赞
python删除指定文件import os try: os.unlink('/path/to/file') except OSError: pass # Deletion failed... ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2486浏览 2360个赞
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) 3086浏览 1574个赞
给定一组指定面额的硬币,有多少中方法可以组合成指定的金额算法#!/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) 1301浏览 2159个赞
这个函数给定日期,输出星期几,到底0是星期一还是星期天,这和时区有关,反正我这是0表示星期一def get_week_day(date): week_day_dict = { 0 : '星期一', 1 : '星期二', 2 : '星期三'……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1574浏览 2623个赞
python读取文件到字节数组def get_bytes_from_file(filename): return open(filename, "rb").read() ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3797浏览 2110个赞
归并排序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) 2115浏览 2192个赞
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) 2220浏览 2516个赞
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) 2892浏览 343个赞
python获得当前工作目录import os curDir = os.getcwd() ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1886浏览 1847个赞
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) 2028浏览 1742个赞
python信息替换演示代码Escape = "^" def subst(Msg, *Args) : """substitutes Args into Msg.""" Result = "" while Tr……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2316浏览 198个赞
python创建文件的方法file("filename", "w").close() ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2231浏览 1350个赞
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) 2263浏览 2856个赞
python实现的堆排序算法代码def heapSort(a): def sift(start, count): root = start while root * 2 + 1 < count: child = root * 2 + 1 ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1293浏览 1581个赞
python 给数组按片赋值,这段代码可以直接给数组的4-6个元素赋值inventory = ["sword", "armor", "shield", "healing potion"]inventory[4:6] = ["orb of future telli……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2325浏览 2949个赞
python通过加号运算符连接两个列表演示代码inventory = ["sword", "armor", "shield", "healing potion"]chest = ["gold", "gems"]print &quo……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3004浏览 2844个赞
python比较两个列表是否相等,本代码演示了 == 和 is两种方法的区别L1 = [1, ('a', 3)] # same value, unique objectsL2 = [1, ('a', 3)]print L1 == L2, L1 is L2 # equivalen……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3693浏览 228个赞
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) 3854浏览 2688个赞
将一个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) 2314浏览 2917个赞
python通过加号运算符操作列表li = ['a', 'b', 'mpilgrim'] li = li + ['example', 'new'] print li li += ['two……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3235浏览 2309个赞
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) 1328浏览 1445个赞
python 中文件路径和url的相互转换import urllib pathname = 'path/to/file/or/folder/' url = urllib.pathname2url(pathname) pathname = urllib.url2pathname(url)……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2308浏览 2286个赞
python将字符串转换成单词首字母大写的标题格式 方法1a = 'hello world! how are you?' b = ' '.join(i.capitalize() for i in a.split(' '……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2286浏览 663个赞
python计算指定字符串在另一个字符串中出现的次数s = "Count, the number,, of commas." print s.count(",") 输出3……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3210浏览 361个赞
python创建目录并更改权限import os os.mkdir("foo", 0666) ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1888浏览 2369个赞
python计算文本文件的行数filename = "somefile.txt" myfile = open(filename) lines = len(myfile.readlines()) print "There are %d lines in %s" % (lines, filename) ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2501浏览 2909个赞
python转换字符串为摩尔斯电码chars = ",.0123456789?abcdefghijklmnopqrstuvwxyz"codes = """--..-- .-.-.- ----- .---- ..--- ...-- ....- ..... -.... --... ---.. ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1351浏览 2361个赞
python文件拷贝代码,这段代码演示了通过文件名拷贝和通过文件对象拷贝import shutil # copy files by name shutil.copyfile('/path/to/file', '/path/to/other/phile') # copy file-objec……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1330浏览 532个赞
python获得文件大小的代码TheFileSize = os.path.getsize(TheFileName) ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 4104浏览 1235个赞
python字符串和整形的相互转换print int("42"), str(42) # convert from/to stringprint int("42") + 1 # force addition……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3972浏览 1053个赞
python获取数组元素的个数mySeq = [1,2,3,4,5] len(mySeq) ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3694浏览 2241个赞
python更新列表(数组)aList = [123, 'abc', 4.56, ['inner', 'list'], (7-9j)]print aList[2]aList[2] = 'float replacer'print aListaList.append(&……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2015浏览 2181个赞
遗传算法的神经网络python实现代码## {{{ http://code.activestate.com/recipes/578241/ (r1)from operator import itemgetter, attrgetterimport mathimport randomimport stringimport timeitfrom……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2833浏览 1677个赞
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) 2917浏览 941个赞
python列表操作extend和append的区别演示代码li = ['a', 'b', 'c'] li.extend(['d', 'e', 'f']) print li pri……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2309浏览 115个赞