The python by a list of element values into a plurality of sub-lists

1. The original list is sorted, when the neighbors are not at the same time, the slice of the original list.

cutList1 DEF (orList): 

orList.sort () # list sorting. The default small to large
newList = [] # empty list

n = 0 # each slice starting point
for K in Range (len (orList)):
IF orList [K] == orList [-1]: # row over due order, when the orList [k] is equal to the value of the last element of the list, after slicing can exit the loop
newList.append (orList [n:]) # taken from the last orList [k]
BREAK # exit loop
if orList [k] = orList! [k + 1]: # row over due order, unequal neighbors, it means that when the sub-lists to cut
subList = orList [n: k + 1] # slice
newList.append (the subList)
n-K +. 1 = # n for storing the starting point of each slice
return newList

myList1 = [. 4, 2,. 1, 2, 2,. 1,. 1,. 5,. 6,. 5,. 4]
Print (cutList1 (myList1)) # [[. 1,. 1, 1], [2, 2, 2], [4, 4], [5, 5], [6]]

2. set to the first weight, then the original number is calculated for each element in the list, followed by the number of each element of the new list is generated directly, and finally in a large list
cutList2 DEF (orList): 

newSeqAfterSet = SET (orList) SET # deduplication. The default small to large
newList = []
for K in newSeqAfterSet:
kCount = orList.count (K) # count the number of each element in the element list
kList = [k] * kCount # generates a new list
newList.append (kList )
return newList
 
myList2 = [4, 2, 1, 2, 2, 1, 1, 5, 6, 5, 4]
print (cutList2(myList2)) # [[1, 1, 1], [2, 2, 2], [4, 4], [5, 5], [6]]
3. After the first element of the list to take out, in a temporary variable, and then circulating the original list, taking the same values, this element is removed after this operation cycle ...
cutList3 DEF (orList): 

copylist = DeepCopy (orList) # Copy list element for removing, retaining the integrity of the original list

newList = []
while. 1:
IF copylist == []: Copy # exits the while loop is a list of space-time
break

# is updated each time the while loop firstElement value, and resets eleList empty
firstElement = copyList [0] # copy list of the first element
eleList = [] # sublist
for ELE in orList:
IF ELE == firstElement:
eleList. the append (ELE)
copyList.remove (ELE) # remove elements. After completion of the for loop, equal to the original list of all the elements and the first element has been withdrawn
# and these elements are removed from the copy list

newList.append (eleList)

return newList

myList3 = [. 4, 2,. 1, 2, 2, 1, 1, 5, 6, 5, 4]
Print (cutList3 (myList3)) # [[. 4,. 4], [2, 2, 2], [. 1,. 1,. 1], [. 5,. 5], [. 6]]

4. As can be seen from the above
  4.1 Method 1 and list Sort 2 are newly generated through the process according to the sort order when the three elements appear
  4.2 method 1 lead to the original ordered list is broken ring, which should advance before using a deep copy
  4.3 from an efficiency point of view, method 2 should be the shortest running time. This is not specific to see, until later ...
 

Guess you like

Origin www.cnblogs.com/lipx9527/p/11071427.html
Recommended