phfb.net
当前位置:首页 >> python字符排列组合 >>

python字符排列组合

import res = 'abcdefghijkl42819iwlh'm = re.findall(r'[a-eg-za-eg-z]+', s)print(m)运行结果是['abcde', 'ghijkl', 'iwlh']除了f以外的所有字母都匹配到了

>>> import itertools >>> srcstr = 'abcd' >>> [''.join(x) for x in itertools.permutations(src, 2)] ['ab', 'ac', 'ad', 'ba', 'bc', 'bd', 'ca', 'cb', 'cd', 'da', 'db', 'dc'] >>> [''.join(x) for x in itertools.permutations(src, 3)] ['abc', 'abd', 'acb', 'acd', 'adb', 'adc', 'bac', 'bad', 'bca',

import mathimport randomoTemp = []oList = []i = 0while True: a = random.randint(1,4) if a in oTemp: continue else: oTemp.append(a) i +=1 if i%4==0: Num = oTemp[0]*1000+oTemp[1]*100+oTemp[2]*10+oTemp[3] if Num in oList: i = 0 oTemp=[]

python替换某个文本中的字符串,然后生成新的文本文档,代码如下:import osos.chdir('d:\\') # 跳到d盘if not os.path.exists('test1.txt'): # 看一下这个文件是否存在exit(-1) #不存在就退出lines = open('test1.txt').readlines() #打开文件,读入每

用一个字符一个字符去判断的话估计是很复杂,用正则表达式去判断应该会快点.

>> from itertools import combinations, permutations>> permutations([1, 2, 3], 2)<itertools.permutations at 0x7febfd880fc0> # 可迭代对象 >> list(permutations([1, 2, 3], 2)) #排列 [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)] >> list(combinations([1, 2, 3], 2)) #组合 [(1, 2), (1, 3), (2, 3)]

生成排列可以用product:from itertools import product l = [1, 2, 3] print list(product(l, l)) print list(product(l, repeat=4))组合的话可以用combinations:from itertools import combinations print list(combinations([1,2,3,4,5], 3))

A = ['name1','name2','name3']B = [78,95,85]C = [60,88,99]D = [90,85,100]index = len(A)for i in range(index): print A[i],B[i],C[i],D[i]

排列组合合并Python里两个list的元素import itertoolsa,b=[1,2,3],[4,5,6]print(list(itertools.product(a,b)))

#encoding: utf-8 str = ["a","b","c","d","e"] result = [] for x in ["a","b","c","d","e"]:for y in ["a","b","c","d","e"]:for z in ["a","b","c","d","e"]:for o in ["a", "b", "c", "d", "e"]:if (x != y ) and (x !

网站首页 | 网站地图
All rights reserved Powered by www.phfb.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com