'Split List with a List of delimiters
list1 = [185, 184, 183, 182, 181, 180, 179, 178, 207, 177, 176, 206, 175, 174, 173, 172, 171, 170, 169, 205, 168, 167, 166, 165, 164, 163, 162, 161, 160, 159, 158, 157, 156, 204, 155, 154, 153, 152, 151, 150, 149, 148, 147, 146, 145, 144, 143, 203, 142, 141, 140, 139, 138, 137, 136, 135, 134, 133, 132, 131, 130, 202, 129, 128, 127, 126, 125, 124, 123, 122, 121, 120, 119, 118, 117, 201, 116, 115, 114, 113, 112, 111, 110, 109, 108, 107, 106, 105, 104, 200, 103, 102, 101, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 199, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 198, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 197, 11, 196, 10, 9, 8]
delim = [182, 175, 164, 151, 138, 125, 112, 99, 86, 73, 60, 47, 34, 21, 11]
How can I split the list, with the delim list and still keep the delim values?
the result I'm looking for is
newList = [ [185,184, 183], [182, 181, 180, 179, 178, 207, 177, 176, 206], [175, 174 .......
I have to use Python 2.7, because the "Main" application I'm automating for is still using 2.7 for its scripting language
I'm not sure how to get a loop to change what item it uses in the 'delim' list when it 'hits' the previous item in the 'delim' list
edit: OK I'm getting close.... just have to solve a IndexError
newList, currList, currDelimIndex = [], [], 0
for i in list1:
if i == delim[currDelimIndex]:
newList.append(currList)
currDelimIndex += 1
currList = [i]
else:
currList.append(i)
Thanks!
Solution 1:[1]
Here is what I came up with, if anyone has a better solution, feel free to share
list1 = [185, 184, 183, 182, 181, 180, 179, 178, 207, 177, 176, 206, 175, 174, 173, 172, 171, 170, 169, 205, 168, 167, 166, 165, 164, 163, 162, 161, 160, 159, 158, 157, 156, 204, 155, 154, 153, 152, 151, 150, 149, 148, 147, 146, 145, 144, 143, 203, 142, 141, 140, 139, 138, 137, 136, 135, 134, 133, 132, 131, 130, 202, 129, 128, 127, 126, 125, 124, 123, 122, 121, 120, 119, 118, 117, 201, 116, 115, 114, 113, 112, 111, 110, 109, 108, 107, 106, 105, 104, 200, 103, 102, 101, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 199, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 198, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 197, 11, 196, 10, 9, 8]
delim = [182, 175, 164, 151, 138, 125, 112, 99, 86, 73, 60, 47, 34, 21, 11]
newList, currList, currDelimIndex = [], [], 0
for edge in list1:
if edge == delim [currDelimIndex]:
newList.append(currList)
if currDelimIndex < len(delim) - 1:
currDelimIndex += 1
currList = [edge]
else:
currList.append(edge)
newList.append(currList) # append the last group
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
