首页
关于
Search
1
怎么快速从GitHub上下载代码
48 阅读
2
GitHub下载加速的有效方法
39 阅读
3
Python中的center()是怎么用的
35 阅读
4
如何在GitHub上下载旧版本
34 阅读
5
怎样删除GitHub存储库
32 阅读
Python
Github
IDC推荐
登录
Search
Xbe
累计撰写
242
篇文章
累计收到
1
条评论
首页
栏目
Python
Github
IDC推荐
页面
关于
搜索到
81
篇与
的结果
2025-03-08
如何用python求100以内的素数?
如何用python求100以内的素数?质数(primenumber)又称素数,有个。质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数的数称为质数,如:2、3、5、7、11、13、17、19。方法一,用for循环来实现num=[]; i=2 for i in range(2,100): j=2 for j in range(2,i): if(i%j==0): break else: num.append(i) print(num)方法二,用函数来实现import math def func_get_prime(n): return filter(lambda x: not [x%i for i in range(2, int(math.sqrt(x))+1) if x%i ==0], range(2,n+1)) print func_get_prime(100)输出结果为:[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
2025年03月08日
5 阅读
0 评论
0 点赞
2025-03-08
在Python中如何获取元素在数组中的索引号?
在Python中如何获取元素在数组中的索引号?具体如下:这里python是通过index方法获取索引号的li = ['a', 'b', 'new', 'D', 'z', 'example', 'new', 'two', 'elements'] print li.index("example") print li.index("new") print li.index("z") print "c" in li运行结果如下:5 2 4 False
2025年03月08日
4 阅读
0 评论
0 点赞
2025-03-08
Python列表中extend和append有什么区别?
python列表操作之extend和append的区别:list.append(obj)在列表末尾添加新的对象list.extend(seq)在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)li = ['a', 'b', 'c'] li.extend(['d', 'e', 'f']) print li print len(li) print li[-1] li = ['a', 'b', 'c'] li.append(['d', 'e', 'f']) print li print len(li) print li[-1]运行结果如下:['a', 'b', 'c', 'd', 'e', 'f'] 6 f ['a', 'b', 'c', ['d', 'e', 'f']] 4 ['d', 'e', 'f']
2025年03月08日
4 阅读
0 评论
0 点赞
2025-03-08
Python列表怎么更新值?
序列是Python中最基本的数据结构。序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推。Python有6个序列的内置类型,但最常见的是列表和元组。序列都可以进行的操作包括索引,切片,加,乘,检查成员。此外,Python已经内置确定序列的长度以及确定和最小的元素的方法。列表是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现。列表的数据项不需要具有相同的类型创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可。如下所示:list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5 ] list3 = ["a", "b", "c", "d"]与字符串的索引一样,列表索引从0开始。列表可以进行截取、组合等。那如何在python中更新列表呢?aList = [123, 'abc', 4.56, ['inner', 'list'], (7-9j)] print aList[2] aList[2] = 'float replacer' print aList aList.append("hi, i'm new here") print aList运行结果如下:4.56 [123, 'abc', 'float replacer', ['inner', 'list'], (7-9j)] [123, 'abc', 'float replacer', ['inner', 'list'], (7-9j), "hi, i'm new here"]
2025年03月08日
3 阅读
0 评论
0 点赞
2025-03-08
用Python实现的二分查找算法
先来看个用Python实现的二分查找算法实例#!/usr/bin/env python import sys def search2(a,m): low = 0 high = len(a) - 1 while(low <= high): mid = (low + high)/2 midval = a[mid] if midval < m: low = mid + 1 elif midval > m: high = mid - 1 else: print mid return mid print -1 return -1 if __name__ == "__main__": a = [int(i) for i in list(sys.argv[1])] m = int(sys.argv[2]) search2(a,m)运行:administrator@ubuntu:~/Python$ python test_search2.py 123456789 4注:1.'__':由于python的类成员都是公有、公开的被存取public,缺少像正统面向对象语言的私有private属性。于是就用__来将就一下,模拟私有属性。这些__属性往往是内部使用,通常情况下不用改写。也不用读取。加上2个下划线的目的,一是不和普通公有属性重名冲突,二是不让对象的使用者(非开发者)随意使用。2.__name__ == "__main__"表示程序脚本是直接被执行的.如果不等于表示脚本是被其他程序用import引入的.则其__name__属性被设为模块名Python采用二分查找找出数字的下标要考虑有重复数字的情况class Solution(object): def searchRange(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ def binary_search(start,end,value): while end>=start: mid = (start+end)//2 print(mid) if nums[mid]>target: end = mid-1 elif nums[mid]<target: start="mid+1" else:="" if="" value="=-1:" mid-1="">=start and nums[mid+value] == target: end = mid+value else: return mid else: if mid+1<=end and nums[mid+value] == target: start = mid+value else: return mid return -1 a=binary_search(0,len(nums)-1,-1) b=binary_search(0,len(nums)-1,1) return [a,b] a = Solution() l = [2,2] print(a.searchRange(l,2)) </target:>二分算法的定义不在多说了import sys source = [1,2,3,4,5,6,7,8,9,10] #must be in order des = int(sys.argv[1]) low = 0 high = len(source) - 1 targetIndex = -1 print "des=",des while low <= high: middle = (low + high)/2 if des == source[middle]: targetIndex = middle break elif des < source[middle]: high = middle -1 print "middle element[index=",middle,",value=",source[middle],"] is bigger than des, continue search from[",low,"to",high,"]" else: low = middle + 1 print "middle element[index=",middle,",value=",source[middle],"] is smaller than des, continue search from[",low,"to",high,"]" print "search complete, target element's index in source list is ",targetIndex最后在分享一个'fileName--BinarySearch.py' src = [] def BinarySearch(low, high, target, *src): '二分查找' while low <= high: mid = (low + high) // 2 midVal = src[mid] if target < midVal: high = mid - 1 elif target > midVal: low = mid + 1 else: return mid BinarySearch(low, high, target, *src) print('Please input 10 number:') for number in range(10): src.append(int(input('Num %d:' % number))) sortList = tuple(src) key = int(input('Please input key:')) location = BinarySearch(0, len(src) - 1, key, *sortList) if location != None: print('Find target at %d' % (location + 1)) else: print('No target!')
2025年03月08日
4 阅读
0 评论
0 点赞
1
...
7
8
9
...
17