python - split a list of tuples based on the value in the tuple -
I have a list that looks like this: Each of the names of the name of Tuple, start, stop and a direction:
major_list = [('a', 20,30, -1), ('b', 31, 40, -1), ('c', 41,50, -1) ('D', 51, 60, + 1), ('z', 90,100, -1), ('E', 61, 70, + 1), ('F', 71, 80, + 1)]
which needs to be split in such a way:
[[('a', 20,30, -1), ('b' 31, 40, -1), ('C', 41, 50, -1)], [('P', 51, 60, +1)], [('z', 90,100, -1]], [(There are two rules to divide in the e-list: 1) If the stomach (1), (1) between tuples 20> make a new list [or] 2) If imminent In the opposite direction of Tuple (say 'C', 41, 50, -1), ('D', 51, 60, + 1), create a new list after 'C' here As far as I have so far:
for split list = [], enumerate (main_list) in tup: If I! = Len (major_List) -1: if i == 0: tmp_list = [] next_tup = key_list [i + 1] if (st (int (next_tup [1]) - int (tup [2])); 20 ) Or (next_tup [3]! = Tup [3]): tmp_list.append (tup) if tmp_list: SplitList.append (tmp_list) tmp_list = [] Other: tmp_list.append (tup)
The Splitist has an empty list cast for some reasons, and I can not understand what I have done wrong. Is there another dragon approach to do this?
If this is the first element in the list, the element is a list in the final
, Then check the necessary sub-passages in the last element of the final
list against the current:
Major_list = [('A', 20, 30, -1), ('B', 31, 40, -1), ('C', 41, 50, -1), ('D', 51, 60, + 1), ('z' 90,100, -1), ('E', 61,70, + 1), ('F', 71,80, + 1)] final = [] in major_list for ele: if not last: last .append ([Ele]) #free is empty, so add the first element to the list # of the final sybillist Ntim items last added by adding the finishing touches and our current element AL F (final [-1]] [- 1] [1] - Elephant [2]) & gt; 20 or the last [-1] [- 1] [3]! = Ele [3]: # If this does not meet the requirement, then it is finally included in a new list. Ampad ([yes]) Other: # Add to the other last finalist [1]. Append (AI) print (last) [[('A', 20, 30, -1), ('B', 31, 40, -1), 'C', 41, 50, -1], [ ('D', 51, 60, 1)], [('z', 90, 100, -1]], [('E', 61, 70, 1), 'F', 71, 80, 1)]]
Comments
Post a Comment