简体   繁体   English

是否可以将我的代码转换为列表理解? 我该如何改进我的代码?

[英]Is it possible to turn my code into a list comprehension? also how can i improve my code?

Is it possible to turn the part of my code where there is a nested for loop into a list comprehension?是否可以将我的代码中存在嵌套 for 循环的部分转换为列表理解? also how can i improve my code.还有我如何改进我的代码。 What it does is takes a input and displays all the permutations/combos它所做的是接受输入并显示所有排列/组合

N = "123123"
s = []
def combos():
    global s
    for i in range(len(N)):
        for j in range(i+1,len(N) + 1):
            s.append(N[i:j])
    s = list(set(s)) # converting to set to remove duplication


combos()

Yes, it should be possible to turn this function to a list comprehension.是的,应该可以将此功能转换为列表理解。 I used the itertools module for this (it's my first time using it, so it may not look "correct" to people more experiencied with it).我为此使用了 itertools 模块(这是我第一次使用它,因此对于更有经验的人来说,它可能看起来不“正确”)。

However, do you really want all the permutations?然而,你真的想要所有的排列吗? Because I'm don't think this is what your function currently does.因为我认为这不是您的功能目前所做的。

Anyways, here's what you could do using itertools.permutations :无论如何,这是您可以使用itertools.permutations执行的操作:

s = list(set(["".join(i) for j in range(len(N)+1) for i in itertools.permutations(N, j)]))

Warning : it doesn't output the same result as your function.警告:它不会输出与您的函数相同的结果。

Note: you can also try replacing itertools.permutations by itertools.combinations as I'm not quite sure of what you want.注意:您也可以尝试更换itertools.permutations通过itertools.combinations因为我不太清楚你想要什么。 The syntax is exactly the same, you literally just have to replace "permutations" by "combinations".语法完全相同,您实际上只需将“排列”替换为“组合”。

Output for itertools.permutations : itertools.permutations输出:

271 substrings - ['', '231132', '122133', '1232', '23131','3', '312123', '131232', '313122', '123', '332', '223311','332211', '1321', '233121', '322113', '23123', '313221', '113223', '212133', '313', '2313', '3311', '311322', '321231', '22131', '2311', '31212', '1323', '1123', '32131', '3221', '32312', '211233', '21233', '321123', '32231', '13321', '323112', '31', '132', '2121', '122', '13122', '1', '232', '121233', '312312', '311232', '213321', '13123', '322131', '1313', '22133', '32112', '231213', '332112', '233211', '123321', '133', '1322', '123312', '132132', '21331', '3123', '131223', '21313', '133221', '21', '3211', '31322', '32213', '32321', '22113', '312132', '1312', '33112', '12231', '22311', '233112', '132123', '13221', '21321', '1213', '12312',
 '13', '1331', '2321', '221133', '23113', '3312', '31232','213132', '23321', '123132', '3322', '122331', '12', '3231', '12331', '321132', '32113', '21132', '323211', '3213', '33122', '113', '13213', '2123', '213', '12133', '21312', '312', '31221', '121323', '331212', '32132', '3121', '13223','223', '1212', '31312', '132321', '3212', '33211', '1223','133212', '221313', '113232', '121332', '231', '131', '12123', '132231', '213213', '121', '321', '33', '33121', '223131', '31231', '123213', '321321', '321312', '231321', '21133', '312321', '322', '11', '2133', '33212', '313212', '331122', '232131', '11232', '21213', '23121', '1233', '3113', '112233', '13322', '23311', '31213', '212331', '323121', '213231', '31321', '11332', '21323', '22331', '31223', '221331', '2312', '1332', '2231', '221', '1132', '23', '1133', '113
23', '2323', '3321', '231123', '1221', '2332', '331', '12233', '23112', '33221', '213123', '23132', '11233', '2233', '323', '312231', '32311', '32123', '211332', '113322', '231312', '132213', '212313', '11322', '223113', '2331', '2', '211', '23231', '332121', '3232', '23213', '322311', '12132', '2113', '233', '23312', '31122', '32121', '12332', '3122', '232311', '13232', '331221', '132312', '13132', '112', '12321', '231231', '3112', '3131', '212', '13212', '2112', '13312', '232113', '1122', '112323', '2132', '3223', '123123', '21231', '31132', '1231', '2211', '23211', '32', '21123','2131', '123231', '321213', '133122', '122313', '112332', '22', '12313', '2213', '131322', '3132', '13231', '311223','12213', '12323', '21332', '31123', '32211', '213312', '312213', '11223', '311', '22313', '211323']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM