python获取数组元素的几种方法L = ['spam', 'Spam', 'SPAM!']print L[2] # offsets start at zeroprint L[-2] ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3161浏览 1783个赞
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) 2053浏览 594个赞
python字典基本操作范例代码d2 = {'spam': 2, 'ham': 1, 'eggs': 3} # make a dictionaryprint d2 # order is scrambledd2……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2748浏览 2244个赞
python中字典赋值方法x = {}y = xx['key'] = 'value'print yx = {}print yx = {}y = xy['key'] = 'value'print yprint x.clear()print y……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3041浏览 2959个赞
python修改字典内key对应的值d2 = {'spam': 2, 'ham': 1, 'eggs': 3} # make a dictionaryprint d2 # order is scrambledd2[……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2906浏览 716个赞
python保存字符串到文件def save(filename, contents): fh = open(filename, 'w') fh.write(contents) fh.close() save('file.name', 'some stuff&……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2746浏览 159个赞
python从文件读取文本# get contents of file as a list, one line per element data = open('/path/to/file').readlines() ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2533浏览 1373个赞
python按单词翻转字符串,如 hello world 翻转成world hellodef reverseWords(s): return ' '.join(reversed(s.split(' '))) ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1351浏览 464个赞
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) 2304浏览 2164个赞
python返回3天后的日期import datetimeday3 = datetime.datetime.now() + datetime.timedelta(days=3)……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1929浏览 1243个赞
python删除指定文件import os try: os.unlink('/path/to/file') except OSError: pass # Deletion failed... ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2979浏览 2238个赞
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) 2762浏览 1956个赞
给定一组指定面额的硬币,有多少中方法可以组合成指定的金额算法#!/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) 2453浏览 1765个赞
这个函数给定日期,输出星期几,到底0是星期一还是星期天,这和时区有关,反正我这是0表示星期一def get_week_day(date): week_day_dict = { 0 : '星期一', 1 : '星期二', 2 : '星期三'……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2155浏览 1877个赞
python读取文件到字节数组def get_bytes_from_file(filename): return open(filename, "rb").read() ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3079浏览 1633个赞
归并排序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) 2473浏览 308个赞
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) 3348浏览 1844个赞
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) 1309浏览 1709个赞
python获得当前工作目录import os curDir = os.getcwd() ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1989浏览 782个赞
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) 2669浏览 1339个赞
python信息替换演示代码Escape = "^" def subst(Msg, *Args) : """substitutes Args into Msg.""" Result = "" while Tr……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2641浏览 2499个赞
python创建文件的方法file("filename", "w").close() ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3579浏览 1200个赞
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) 2464浏览 621个赞
python实现的堆排序算法代码def heapSort(a): def sift(start, count): root = start while root * 2 + 1 < count: child = root * 2 + 1 ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2622浏览 244个赞
python 给数组按片赋值,这段代码可以直接给数组的4-6个元素赋值inventory = ["sword", "armor", "shield", "healing potion"]inventory[4:6] = ["orb of future telli……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2162浏览 156个赞
python通过加号运算符连接两个列表演示代码inventory = ["sword", "armor", "shield", "healing potion"]chest = ["gold", "gems"]print &quo……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2781浏览 1760个赞
python比较两个列表是否相等,本代码演示了 == 和 is两种方法的区别L1 = [1, ('a', 3)] # same value, unique objectsL2 = [1, ('a', 3)]print L1 == L2, L1 is L2 # equivalen……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2640浏览 222个赞
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) 3944浏览 2110个赞
将一个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) 2065浏览 2735个赞
python通过加号运算符操作列表li = ['a', 'b', 'mpilgrim'] li = li + ['example', 'new'] print li li += ['two……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3347浏览 150个赞
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) 1638浏览 693个赞
python 中文件路径和url的相互转换import urllib pathname = 'path/to/file/or/folder/' url = urllib.pathname2url(pathname) pathname = urllib.url2pathname(url)……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2993浏览 1807个赞
python将字符串转换成单词首字母大写的标题格式 方法1a = 'hello world! how are you?' b = ' '.join(i.capitalize() for i in a.split(' '……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1421浏览 2587个赞
python计算指定字符串在另一个字符串中出现的次数s = "Count, the number,, of commas." print s.count(",") 输出3……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2692浏览 414个赞
python创建目录并更改权限import os os.mkdir("foo", 0666) ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1745浏览 2023个赞
python计算文本文件的行数filename = "somefile.txt" myfile = open(filename) lines = len(myfile.readlines()) print "There are %d lines in %s" % (lines, filename) ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2761浏览 917个赞
python转换字符串为摩尔斯电码chars = ",.0123456789?abcdefghijklmnopqrstuvwxyz"codes = """--..-- .-.-.- ----- .---- ..--- ...-- ....- ..... -.... --... ---.. ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1400浏览 2782个赞
python文件拷贝代码,这段代码演示了通过文件名拷贝和通过文件对象拷贝import shutil # copy files by name shutil.copyfile('/path/to/file', '/path/to/other/phile') # copy file-objec……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1555浏览 2051个赞
python获得文件大小的代码TheFileSize = os.path.getsize(TheFileName) ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3818浏览 478个赞
python字符串和整形的相互转换print int("42"), str(42) # convert from/to stringprint int("42") + 1 # force addition……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3754浏览 620个赞
python获取数组元素的个数mySeq = [1,2,3,4,5] len(mySeq) ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3139浏览 1240个赞
python更新列表(数组)aList = [123, 'abc', 4.56, ['inner', 'list'], (7-9j)]print aList[2]aList[2] = 'float replacer'print aListaList.append(&……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2672浏览 1940个赞
遗传算法的神经网络python实现代码## {{{ http://code.activestate.com/recipes/578241/ (r1)from operator import itemgetter, attrgetterimport mathimport randomimport stringimport timeitfrom……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2141浏览 461个赞
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) 2261浏览 1383个赞
python列表操作extend和append的区别演示代码li = ['a', 'b', 'c'] li.extend(['d', 'e', 'f']) print li pri……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3400浏览 1533个赞