随笔分类 -  python

摘要:from fractions import Fraction value = 4.2 print(Fraction(value).limit_denominator()) 阅读全文
posted @ 2018-11-15 21:07 anobscureretreat 阅读(1664) 评论(0) 推荐(0) 编辑
摘要:import math print(math.fabs(-2.1)) print(math.fabs(-0.0)) print(math.fabs(10.1)) print(math.fabs(0.0)) 阅读全文
posted @ 2018-11-15 21:03 anobscureretreat 阅读(1946) 评论(0) 推荐(0) 编辑
摘要:print("Addition of two complex numbers : ",(4+3j)+(3-7j)) print("Subtraction of two complex numbers : ",(4+3j)-(3-7j)) print("Multiplication of two complex numbers : ",(4+3j)*(3-7j)) print("D... 阅读全文
posted @ 2018-11-15 21:01 anobscureretreat 阅读(814) 评论(0) 推荐(0) 编辑
摘要:import fractions f1 = fractions.Fraction(2, 3) f2 = fractions.Fraction(3, 7) print('{} + {} = {}'.format(f1, f2, f1 + f2)) print('{} - {} = {}'.format(f1, f2, f1 - f2)) print('{} * ... 阅读全文
posted @ 2018-11-15 20:59 anobscureretreat 阅读(722) 评论(0) 推荐(0) 编辑
摘要:def is_Power_of_three(n): while (n % 3 == 0): n /= 3; return n == 1; print(is_Power_of_three(9)) print(is_Power_of_three(81)) print(is_Power_of_three(21)) ... 阅读全文
posted @ 2018-11-15 10:27 anobscureretreat 阅读(805) 评论(2) 推荐(0) 编辑
摘要: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(16)) print(is_Power_of_four(255)) 阅读全文
posted @ 2018-11-15 10:23 anobscureretreat 阅读(806) 评论(0) 推荐(0) 编辑
摘要:def is_arithmetic(l): delta = l[1] - l[0] for index in range(len(l) - 1): if not (l[index + 1] - l[index] == delta): return False return True print(i... 阅读全文
posted @ 2018-11-15 10:20 anobscureretreat 阅读(3816) 评论(0) 推荐(0) 编辑
摘要:def is_geometric(li): if len(li) <= 1: return True # Calculate ratio ratio = li[1]/float(li[0]) # Check the ratio of the remaining for i in range(1, len(li))... 阅读全文
posted @ 2018-11-15 10:15 anobscureretreat 阅读(2386) 评论(0) 推荐(0) 编辑
摘要:def add_binary_nums(x,y): max_len = max(len(x), len(y)) x = x.zfill(max_len) y = y.zfill(max_len) result = '' carry = 0 for i in... 阅读全文
posted @ 2018-11-15 10:06 anobscureretreat 阅读(2807) 评论(0) 推荐(0) 编辑
摘要:import re def end_num(string): #以一个数字结尾字符串 text = re.compile(r".*[0-9]$") if text.match(string): return True else: return False print(end_num('abcdef'... 阅读全文
posted @ 2018-11-14 10:24 anobscureretreat 阅读(4054) 评论(0) 推荐(0) 编辑
摘要:def is_decimal(num): import re #以数字开头,小数点后保留1位数字或两位数字或者没有小数部分 dnumre = re.compile(r"""^[0-9]+(\.[0-9]{1,2})?$""") result = dnumre.search(num) return bool(result) prin... 阅读全文
posted @ 2018-11-14 10:18 anobscureretreat 阅读(4603) 评论(0) 推荐(0) 编辑
摘要:将结尾的Road用Rd.替换 阅读全文
posted @ 2018-11-14 10:13 anobscureretreat 阅读(4845) 评论(1) 推荐(1) 编辑
摘要:x = frozenset([1, 2, 3, 4, 5]) y = frozenset([3, 4, 5, 6, 7]) #如果x与y没有公共元素,返回true print(x.isdisjoint(y)) #返回x与y不一样的元素 print(x.difference(y)) #返回x与y并集 print(x | y) 阅读全文
posted @ 2018-11-14 10:10 anobscureretreat 阅读(147) 评论(0) 推荐(0) 编辑
摘要:setx = set(["apple", "mango"]) sety = set(["mango", "orange"]) setz = set(["mango"]) issubset = setx = sety print(issuperset) issubset = setz = setz print(issuperset) 阅读全文
posted @ 2018-11-14 10:01 anobscureretreat 阅读(373) 评论(0) 推荐(0) 编辑
摘要:num_set = set([0, 1, 3, 4, 5]) num_set.pop() print(num_set) num_set.pop() print(num_set) 阅读全文
posted @ 2018-11-14 09:57 anobscureretreat 阅读(834) 评论(0) 推荐(0) 编辑
摘要:#Create a new set num_set = set([0, 1, 2, 3, 4, 5]) #Discard number 4 num_set.discard(4) print(num_set) 阅读全文
posted @ 2018-11-14 09:55 anobscureretreat 阅读(1427) 评论(0) 推荐(0) 编辑
摘要:#Create a set seta = set([5, 10, 3, 15, 2, 20]) #Find maximum value print(max(seta)) #Find minimum value print(min(seta)) 阅读全文
posted @ 2018-11-13 10:26 anobscureretreat 阅读(3970) 评论(0) 推荐(0) 编辑
摘要:#Intersection setx = set(["green", "blue"]) sety = set(["blue", "yellow"]) setz = setx & sety print(setz) 阅读全文
posted @ 2018-11-13 10:22 anobscureretreat 阅读(304) 评论(0) 推荐(0) 编辑
摘要:#Union setx = set(["green", "blue"]) sety = set(["blue", "yellow"]) seta = setx | sety print(seta) 阅读全文
posted @ 2018-11-13 10:20 anobscureretreat 阅读(222) 评论(0) 推荐(0) 编辑
摘要:setx = set(["apple", "mango"]) sety = set(["mango", "orange"]) #Symmetric difference setc = setx ^ sety print(setc) 阅读全文
posted @ 2018-11-13 10:17 anobscureretreat 阅读(338) 评论(0) 推荐(0) 编辑