随笔分类 -  python

上一页 1 ··· 35 36 37 38 39 40 41 42 43 ··· 48 下一页
摘要:def is_Power_of_four(n): while n and not (n & 0b11): n >>= 2 return (n == 1) print(is_Power_of_four(4)) print(is_Power_of_four(12)) print(is_Power_of_four(16)) print(... 阅读全文
posted @ 2018-12-25 10:23 anobscureretreat 阅读(515) 评论(0) 推荐(0)
摘要:As it turns out, there is a string method named find that is remarkably similar to the function we wrote: >>> word = 'banana' >>> index = word.find('a 阅读全文
posted @ 2018-12-18 12:45 anobscureretreat 阅读(571) 评论(0) 推荐(0)
摘要:>>> import os >>> cwd = os.getcwd() >>> cwd '/home/dinsdale' cwd stands for “current working directory”. The result in this example is /home/dinsdale, which is the home directory of a user named ... 阅读全文
posted @ 2018-12-18 11:47 anobscureretreat 阅读(153) 评论(0) 推荐(0)
摘要:import random for i in range(10): x = random.random() print(x) The function randint takes parameters low and high and returns an integer between low and high (including both). >>> random... 阅读全文
posted @ 2018-12-18 11:38 anobscureretreat 阅读(204) 评论(0) 推荐(0)
摘要:>>> import string >>> string.punctuation '!"#$%&'()*+,-./:;?@[\]^_`{|}~' 阅读全文
posted @ 2018-12-18 11:37 anobscureretreat 阅读(343) 评论(0) 推荐(0)
摘要:Dictionaries have a method called items that returns a sequence of tuples, where each tuple is a key-value pair. >>> d = {'a':0, 'b':1, 'c':2} >>> t = 阅读全文
posted @ 2018-12-18 11:36 anobscureretreat 阅读(134) 评论(0) 推荐(0)
摘要:>>> for pair in zip(s, t): ... print(pair) ... ('a', 0) ('b', 1) ('c', 2) If you want to use list operators and methods, you can use a zip object to m 阅读全文
posted @ 2018-12-18 11:22 anobscureretreat 阅读(252) 评论(0) 推荐(0)
摘要:>>> t1 = [1, 2] >>> t2 = t1.append(3) >>> t1 [1, 2, 3] >>> t2 None 阅读全文
posted @ 2018-12-18 11:15 anobscureretreat 阅读(216) 评论(0) 推荐(0)
摘要:Because list is the name of a built-in function, you should avoid using it as a variable name. I also avoid l because it looks too much like 1. So tha 阅读全文
posted @ 2018-12-18 11:12 anobscureretreat 阅读(181) 评论(0) 推荐(0)
摘要:There are several ways to delete elements from a list. If you know the index of the element you want, you can use pop: >>> t = ['a', 'b', 'c'] >>> x = t.pop(1) >>> t ['a', 'c'] >>> x 'b' pop modifi... 阅读全文
posted @ 2018-12-18 11:04 anobscureretreat 阅读(152) 评论(0) 推荐(0)
摘要:gzip 和 bz2 模块可以很容易的处理这些文件。 两个模块都为 open() 函数提供了另外的实现来解决这个问题。 比如,为了以文本形式读取压缩文件,可以这样做: # gzip compression import gzip with gzip.open('somefile.gz', 'rt') 阅读全文
posted @ 2018-12-14 20:57 anobscureretreat 阅读(1513) 评论(0) 推荐(0)
摘要:假设你的包中的文件组织成如下: mypackage/ __init__.py somedata.dat spam.py mypackage/ __init__.py somedata.dat spam.py 现在假设spam.py文件需要读取somedata.dat文件中的内容。你可以用以下代码来完 阅读全文
posted @ 2018-12-14 20:53 anobscureretreat 阅读(566) 评论(0) 推荐(0)
摘要:class py_solution: def twoSum(self, nums, target): lookup = {} for i, num in enumerate(nums): if target - num in lookup: return (lookup[targ... 阅读全文
posted @ 2018-12-13 10:05 anobscureretreat 阅读(240) 评论(0) 推荐(0)
摘要:#用递归函数求 n 阶乘的值 def factorial(i): if i==0: return 1 else: return i * factorial(i-1)# sum=n*(n-1)!所以直接调用自身 n=int(input('请输入阶乘数:')) for i in range(n+1): print('%d !值为 %3d... 阅读全文
posted @ 2018-12-13 01:03 anobscureretreat 阅读(2726) 评论(0) 推荐(0)
摘要:# 用for循环计算 n! sum = 1 n=int(input('请输入n=')) for i in range(0,n+1): for j in range(i,0,-1): sum *= j # sum=sum*j print('%d!=%3d' %(i,sum)) sum=1 阅读全文
posted @ 2018-12-13 00:57 anobscureretreat 阅读(1161) 评论(0) 推荐(0)
摘要:获取result时,会阻塞,如果p里面执行的脚本时间很长,会一直等待执行完毕,然后打印出result。 阅读全文
posted @ 2018-12-09 20:17 anobscureretreat 阅读(1624) 评论(0) 推荐(0)
摘要:赋值(共用一个对象) 浅拷贝(新建对象) 浅拷贝(只拷贝第一层内容) 深拷贝 阅读全文
posted @ 2018-12-08 13:26 anobscureretreat 阅读(372) 评论(0) 推荐(0)
摘要:# lst = ["篮球","排球","乒乓球","足球","电子竞技","台球"] # for el in lst: # lst.remove(el) # print(lst)#['排球', '足球', '台球'] 会发现删不干净 原因是:删除一个.元素的索引重新排序,for循环向后走一个,就漏掉一个删掉了索引是0的元素,然后索引是1的元素补充到索引为0的位置上,然后索引指向1,... 阅读全文
posted @ 2018-12-08 13:23 anobscureretreat 阅读(550) 评论(0) 推荐(0)
摘要:套接字socket 套接字起源于 20 世纪 70 年代加利福尼亚大学伯克利分校版本的 Unix,即人们所说的 BSD Unix。 因此,有时人们也把套接字称为“伯克利套接字”或“BSD 套接字”。一开始,套接字被设计用在同 一台主机上多个应用程序之间的通讯。这也被称进程间通讯,或 IPC。套接字有 阅读全文
posted @ 2018-12-08 01:10 anobscureretreat 阅读(176) 评论(0) 推荐(0)
摘要:math方法: math.pi = π 值3.141592653 math.pow(2,4) = 16 2的4次方 math.sqrt(144) = 12 144开平方=12 阅读全文
posted @ 2018-12-07 15:14 anobscureretreat 阅读(181) 评论(0) 推荐(0)

上一页 1 ··· 35 36 37 38 39 40 41 42 43 ··· 48 下一页