python获取数组元素的几种方法L = ['spam', 'Spam', 'SPAM!']print L[2] # offsets start at zeroprint L[-2] ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2693浏览 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) 1972浏览 243个赞
python字典基本操作范例代码d2 = {'spam': 2, 'ham': 1, 'eggs': 3} # make a dictionaryprint d2 # order is scrambledd2……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2872浏览 2897个赞
python中字典赋值方法x = {}y = xx['key'] = 'value'print yx = {}print yx = {}y = xy['key'] = 'value'print yprint x.clear()print y……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3077浏览 2152个赞
python修改字典内key对应的值d2 = {'spam': 2, 'ham': 1, 'eggs': 3} # make a dictionaryprint d2 # order is scrambledd2[……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3006浏览 1067个赞
python保存字符串到文件def save(filename, contents): fh = open(filename, 'w') fh.write(contents) fh.close() save('file.name', 'some stuff&……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2978浏览 475个赞
python从文件读取文本# get contents of file as a list, one line per element data = open('/path/to/file').readlines() ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2404浏览 1325个赞
python按单词翻转字符串,如 hello world 翻转成world hellodef reverseWords(s): return ' '.join(reversed(s.split(' '))) ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1499浏览 1386个赞
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) 2753浏览 969个赞
python返回3天后的日期import datetimeday3 = datetime.datetime.now() + datetime.timedelta(days=3)……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2082浏览 461个赞
python删除指定文件import os try: os.unlink('/path/to/file') except OSError: pass # Deletion failed... ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2030浏览 178个赞
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) 2121浏览 2100个赞
给定一组指定面额的硬币,有多少中方法可以组合成指定的金额算法#!/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) 2679浏览 1740个赞
这个函数给定日期,输出星期几,到底0是星期一还是星期天,这和时区有关,反正我这是0表示星期一def get_week_day(date): week_day_dict = { 0 : '星期一', 1 : '星期二', 2 : '星期三'……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2840浏览 1689个赞
python读取文件到字节数组def get_bytes_from_file(filename): return open(filename, "rb").read() ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3251浏览 1263个赞
归并排序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) 2202浏览 616个赞
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) 3119浏览 2253个赞
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) 2735浏览 724个赞
python获得当前工作目录import os curDir = os.getcwd() ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1992浏览 1616个赞
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) 1501浏览 2981个赞
python信息替换演示代码Escape = "^" def subst(Msg, *Args) : """substitutes Args into Msg.""" Result = "" while Tr……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2112浏览 191个赞
python创建文件的方法file("filename", "w").close() ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2248浏览 1054个赞
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) 2488浏览 2300个赞
python实现的堆排序算法代码def heapSort(a): def sift(start, count): root = start while root * 2 + 1 < count: child = root * 2 + 1 ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1659浏览 987个赞
python 给数组按片赋值,这段代码可以直接给数组的4-6个元素赋值inventory = ["sword", "armor", "shield", "healing potion"]inventory[4:6] = ["orb of future telli……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3304浏览 1235个赞
python通过加号运算符连接两个列表演示代码inventory = ["sword", "armor", "shield", "healing potion"]chest = ["gold", "gems"]print &quo……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1396浏览 566个赞
python比较两个列表是否相等,本代码演示了 == 和 is两种方法的区别L1 = [1, ('a', 3)] # same value, unique objectsL2 = [1, ('a', 3)]print L1 == L2, L1 is L2 # equivalen……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2910浏览 518个赞
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) 3026浏览 1520个赞
将一个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) 2560浏览 2252个赞
python通过加号运算符操作列表li = ['a', 'b', 'mpilgrim'] li = li + ['example', 'new'] print li li += ['two……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3538浏览 817个赞
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) 2590浏览 2299个赞
python 中文件路径和url的相互转换import urllib pathname = 'path/to/file/or/folder/' url = urllib.pathname2url(pathname) pathname = urllib.url2pathname(url)……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2516浏览 1227个赞
python将字符串转换成单词首字母大写的标题格式 方法1a = 'hello world! how are you?' b = ' '.join(i.capitalize() for i in a.split(' '……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1367浏览 2418个赞
python计算指定字符串在另一个字符串中出现的次数s = "Count, the number,, of commas." print s.count(",") 输出3……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3480浏览 2617个赞
python创建目录并更改权限import os os.mkdir("foo", 0666) ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2082浏览 2667个赞
python计算文本文件的行数filename = "somefile.txt" myfile = open(filename) lines = len(myfile.readlines()) print "There are %d lines in %s" % (lines, filename) ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2387浏览 1370个赞
python转换字符串为摩尔斯电码chars = ",.0123456789?abcdefghijklmnopqrstuvwxyz"codes = """--..-- .-.-.- ----- .---- ..--- ...-- ....- ..... -.... --... ---.. ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2254浏览 2345个赞
python文件拷贝代码,这段代码演示了通过文件名拷贝和通过文件对象拷贝import shutil # copy files by name shutil.copyfile('/path/to/file', '/path/to/other/phile') # copy file-objec……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2256浏览 1725个赞
python获得文件大小的代码TheFileSize = os.path.getsize(TheFileName) ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3974浏览 1253个赞
python字符串和整形的相互转换print int("42"), str(42) # convert from/to stringprint int("42") + 1 # force addition……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2344浏览 361个赞
python获取数组元素的个数mySeq = [1,2,3,4,5] len(mySeq) ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3638浏览 1594个赞
python更新列表(数组)aList = [123, 'abc', 4.56, ['inner', 'list'], (7-9j)]print aList[2]aList[2] = 'float replacer'print aListaList.append(&……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3596浏览 1971个赞
遗传算法的神经网络python实现代码## {{{ http://code.activestate.com/recipes/578241/ (r1)from operator import itemgetter, attrgetterimport mathimport randomimport stringimport timeitfrom……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3026浏览 1224个赞
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) 3588浏览 2202个赞
python列表操作extend和append的区别演示代码li = ['a', 'b', 'c'] li.extend(['d', 'e', 'f']) print li pri……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1845浏览 1858个赞