Regardless of problem, said the reward system
Who is a great god reply if adopted, add WeChat send 100 yuan cash bonus
I'll post when mapping out finally, the
Question:
Length of a string array (not necessarily)
Such as: [' s1 'and s2', 'the s3]
Enter a number of clusters (number of clusters is dynamic, but must be less than the above array length) :
For example: 2
Want results:
S1, S2, S3
- S3 S1 and S2
S2 - S3, S1
Poor name all possible combinations, of course, there can be repeated,
Also note that S1 and S2 S2 - S1 is so repetitive
Posting such as a veteran, a bit of a shame, but there is no way the
At sixes and sevens matter it too much, recently wrote a few grouping all feel wrong
Project period, immediately rush to use, so please stretch out my hand to deliver a great god,
If you are a great god feel finished no problem, I failed to see again, you can add me WeChat: kissoday
As long as the use, must complete the above promise,
CodePudding user response:
the from itertools import combinations Num=int (input (" & gt;" )) P=[' s1 'and s2', 'the s3', 's4') X=list (combinations (p, num)) Ps=set (p) Gg=[[' - & gt; '. Join (I)] + list (ps - set (I)) for I in x] Print (gg) CodePudding user response:
reference 1st floor chuifengde response: the from itertools import combinations Num=int (input (" & gt;" )) P=[' s1 'and s2', 'the s3', 's4') X=list (combinations (p, num)) Ps=set (p) Gg=[[' - & gt; '. Join (I)] + list (ps - set (I)) for I in x] Print (gg) Sorry brother, may be I describe not clear To give you that array P group, for example, number of clusters is 2 occasions I want the result S1, S2, S3, S4 - S3 S1 and S2 - S4 - S4 S1 and S2 - S3 If the length of the array model in addition to the number of clusters is not zero, the last the rest of the remainder of the final group If the array length is 5: [' s1 'and s2', 'the s3', 's4', 's5] Number of clusters is 2 occasions Should be divided into S1, s2, s3, s4, s5 . Number of clusters for the occasion of 3 Should be divided into: S1, s2, s3, s4, s5 S1 and s2 - s4, s3 - s5 . CodePudding user response:
The import itertools Lista=[' s1 'and s2, s3, s4, s5,' s6 ', 's7', 'the s8', 'integrated'] # lista=[' s1 'and s2', 'the s3] Print (len (lista)) Partnum=int (input (' number of clusters')) Rang1=[I for I in range (1, len (lista) + 2 - partnum)] # built a sequential array print (rang1) Mylist=list (itertools permutations (rang1 partnum)) # print (mylist) Mylist=list (filter (lambda x: sum (x)==len (lista), mylist)) Newlist=[] For mylist1 mylist in: Mylist1=list (mylist1) Mylist1. Sort () If mylist1 not in newlist: Newlist. Append (mylist1) Print (newlist) Def get_chuan (fenzu1, start, temp, lista, num) : If the start==num: Return temp # print (start, temp, num) Fenzu2=fenzu1 [start] Temp1=[] For I in itertools.com binations (lista, fenzu2) : Temp1. Append (' - '. Join (I)) Lista1=lista. Copy () For i1 in I: Lista1. Remove (i1) Start1=start + 1 # temp. Append (temp1) Temp2=get_chuan (fenzu1 start1, temp1, lista1, num) Temp. Append (temp2) Return temp Chuan=[] For fenzu1 in newlist: # fenzu1=,2,6 [1] Temp=[] Num=len (fenzu1) Temp=get_chuan (fenzu1, start=0, temp=temp, lista=lista, num=num) Print (temp) CodePudding user response:
Don't know if this meaning, The from itertools import combinations Def get_comb (set_temp comb_num) : Comb_items=list (combinations (set_temp comb_num)) If len (set_temp) - comb_num & lt;=comb_num: Return the [[' - '. Join (comb_item), '-'. Join (list (set_temp - set (comb_item)))] for comb_item in comb_items] The else: Comb_items_temp=[] For comb_item comb_items in: Comb_items_next=get_comb (set_temp - set (comb_item), comb_num) For comb_item_next comb_items_next in: Temp=[' - '. Join (comb_item)] Temp. The extend (comb_item_next) Comb_items_temp. Append (temp) Return comb_items_temp Test_set={s1, s2, s3, s4, s5 '} Test_comb=get_comb (test_set, 2) Print (len (test_comb), test_comb) CodePudding user response:
the from itertools import permutations The from more_itertools import chunked Num=int (input (" & gt;" )) P=[' s1 'and s2', 'the s3', 's4', 's5', 's6'] L=[] For I in the list (permutations (p, len (p))) : Tmp_x=chunked (I, num) Tmp_y=[sorted (j) for j in tmp_x] Tmp_y=sorted (tmp_y, key=lambda t: (- len (t), t [0])) If tmp_y not in l: L.A. ppend (tmp_y) Result=[] For t in l: Result. Append ([r [0] if len (r)==1 else '- & gt;'. Join in t (r) for r]) For I in result: Print (I) Print (len (l)) CodePudding user response:
Thank you all for your help, although the final results are not accord with the demand on my side, but I found in your code is a lot of tips and inspiration, now the problem has been solved, thank you again for the fourth floor, fifth floor of the two brothers, I hope you see after the post with my WeChat, let me say thank you, Warn you ahead, after all, is not completely solve the problem, therefore you divide 100 red envelopes, In order to avoid fraud, the two of you send me direct messages, let me have your WeChat name, Such as red envelopes to send the stickers and I will come over, the Contact me at once!!!!!!