史高治

新随笔 联系 管理
变量:
 
a=1;b=2;c=1——发现变量a和c的内存地址竟然相同,这就是Python不需要定义变量的秘密:内存中根本就没有a、b、c,自然就不必定义啦。
例如,a,b=b,a的真相是a,b=(2,1),等号后的都是存在内存里的,但是b和a并没在内存(=后若是str、int等Python类型比如'a'和'b',那才是真的放在内存里了),它俩相当于此刻的内存中的数值2和1的行参。
 
获取实例的类名=type(666).__name__
#判断变量是不是所猜类型:
print(type(123).__name__=='int')
print(isinstance(123,int))
******************分割线******************
对于可变类型list和range等,切片copy=deepcopy>copy>等号赋值:
 
import copy
a=range(0,100)
b=a[:]
c=copy.copy(a)
d=copy.deepcopy(a)
 
print(type(a))
print(id(a),id(b),id(c),id(d))
****************************************分割线****************************************
sequence:str、tuple、list;set
 
obj='qwert'
对象可迭代吗=hasattr(obj,'__iter__')
字串不足9位则左侧补x=obj.rjust(9,'x')
 
给func(a,k1=v1,k2=v2,k3=v3……)样式的函数,赋值俩list实参:func(a,**dict(zip(sequence_k,sequence_v)))
******************分割线******************
encode与decode:
print('\u0026'.encode().decode())   #'str'.encode()→b'str',b'str'.decode()→'str'
***分割线***
终端输出颜色:
 
from termcolor import colored
 
s=colored('world','green')
print(colored(666,'red'),'hello',s)
***分割线***
Egのstr对象在tuple、list、set中的变身:
 
s='qwert'
jh=set();jh.update(s)
print(tuple(s),list(s),set(s),jh,sep='\n')
 
jh.add(s)
print((s,),[s,],jh,sep='\n')
******************分割线******************
自身加1子:list用.append(x),set用.add(x)。x若为序列则整个序列作为1个子。
自身加n子:list用.extend(序列),set用.update(序列),dict用.update(dict2)。
 
返回个新的同类:前3种序列的增用+,set|增-删,dict的增用dict(d1,**d2)。
******************分割线******************
Egの前3种序列的去重(只保留首次出现的元素)且保持原序:
 
sequence=list('qwqewerttr')
 
s11={i:4 for i in sequence}.keys()    #法1の普通{},py3.6的dict是有序的
 
from collections import OrderedDict    #法2のOrderedDict库
od=OrderedDict()
for i in sequence:
    od[i]=6
s21=od.keys()
 
import numpy    #法3のnumpy库
unique,indexes=numpy.unique(sequence,return_index=True)
s31=unique[indexes.argsort()]  #.argsort():升序后新ndarray的各值,在原ndarray的索引
s32=[sequence[i] for i in sorted(indexes)]
 
print(''.join(s11),tuple(s21),s31,s32,sep='\n')
****************************************分割线****************************************
随机:
 
import random,string
 
随机个整数=random.randint(3,6)   #.choice(range(3,7,1))=.randrange(3,7,1)
随机个奇数=random.randrange(1,100,2)
随机个3位带小数=f'{random.uniform(16,49):0.3f}'
序列中随机取9个串起来=''.join(random.sample(string.ascii_letters+string.digits,9))
字母数字32位可重复=''.join(random.choices(string.ascii_letters+string.digits,k=32))
 
from collections import Counter
frequency=Counter(字母数字32位可重复).most_common(7)
******************分割线******************
素数:
import sympy
 
是否素数=sympy.isprime(5)
第n个素数=sympy.prime(10**6)    #第1个是2
多少素数小于等于它=sympy.primepi(11)
小于它的最大素数=sympy.prevprime(7)
大于它的最小素数=sympy.nextprime(5)
区间内随机个素数=sympy.randprime(16,49)
区间内的所有素数=list(sympy.primerange(41,163))
前n个素数=list(sympy.primerange(2,sympy.prime(81)+1))
****************************************分割线****************************************
排列&组合:
 
t=list('江畔何人初见月?江月何年初照人?')
序列去重=sorted(set(t),key=t.index)
 
import itertools    #[1,6,3]→[[1, 6, 3], [1, 6, 3], [1, 6, 3], [1, 6, 3], [1, 6, 3]]
序列重现=list(itertools.repeat([1,6,3], 5))
 
from collections import Counter
words = ['look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes','the', 'eyes', 'the', 'eyes',
     'the', 'eyes', 'not', 'around', 'the','my', 'eyes', "you're", 'under', 'eyes', "don't",
    'look', 'around', 'the', 'eyes', 'look', 'into']
频次前3的那些元素=Counter(words).most_common(3)
******************分割线******************
以0~9这10个数字生产5位数,各5位数内无重复数字:
 
t=list(range(10))
data=[int('%s'*3 %(x,y,z)) for x in t for y in t for z in t if (x!=0 and x!=y and x!=z and y!=z)]  #3位数
 
t=[str(x) for x in t]    #t=list(map(str,t))
import itertools
data1=[int(''.join(x)) for x in itertools.permutations(t,5) if x[0]!='0']   #元素内无相同字符,5位数
data2=[''.join(x) for x in itertools.combinations_with_replacement(t,5) if x[0]!='0']
data3=[''.join(x) for x in itertools.combinations(t,5)  if x[0]!='0']
data4=[a+b+c+d+e for a,b,c,d,e in itertools.product(t,t,t,t,t) if a!='0']   #笛卡尔积
print(len(data1),data1)
****************************************分割线****************************************
两个序列的交集、并集、差集:
 
a = [0,1,2,3]
b = [2,3,4,5]
intersection=list(set(a).intersection(set(b)))  #交集
union=list(set(a).union(set(b)))    #并集
dif=list(set(a) ^ set(b))   #差集
only = list(set(a).difference(set(b)))   #a有b无
only1=[x for x in a if x not in b]
only2=[x for x in a if not x in b]
 
Egの猜数字游戏の永远猜不对:
import random
t=set()
for i in range(9):
    if  i<8:
        x = input('请输入你猜的数字,在0~9之间:')
        t.add(x)    #if x.isdecimal()
print(t)
r=random.choice(tuple(set(map(str,range(9))).difference(t)))
print('真实的数字其实是:%s' %r)
posted on 2017-10-19 17:02  史高治  阅读(212)  评论(0编辑  收藏  举报