Reputation: 759
I have a list like this:
[[8, "Plot", "Sunday"], [1, "unPlot", "Monday"], [12, "Plot", "Monday"], [10, "Plot", "Tuesday"], [4, "unPlot", "Tuesday"], [14, "Plot", "Wednesday"], [6, "unPlot", "Wednesday"], [1, "unPlot", "Thursday"], [19, "Plot", "Thursday"], [28, "Plot", "Friday"], [10, "unPlot", "Friday"], [3, "unPlot", "Saturday"]]
I want to separate it into two lists according the Plot
and unPlot
values, resulting:
list1=[[8, "Plot", "Sunday"], [12, "Plot", "Monday"], ...]
list2=[[1, "unPlot", "Monday"], [4, "unPlot", "Tuesday"], ...]
Upvotes: 6
Views: 10189
Reputation: 12935
data = [[8, "Plot", "Sunday"], [1, "unPlot", "Monday"], [12, "Plot", "Monday"], [10, "Plot", "Tuesday"], [4, "unPlot", "Tuesday"], [14, "Plot", "Wednesday"], [6, "unPlot", "Wednesday"], [1, "unPlot", "Thursday"], [19, "Plot", "Thursday"], [28, "Plot", "Friday"], [10, "unPlot", "Friday"], [3, "unPlot", "Saturday"]]
res = {'Plot':[],'unPlot':[]}
for i in data: res[i[1]].append(i)
This way you iterate the list once
Upvotes: 6
Reputation: 7235
Try with basic list comprehension:
>>> [ x for x in l if x[1] == "Plot" ]
[[8, 'Plot', 'Sunday'], [12, 'Plot', 'Monday'], [10, 'Plot', 'Tuesday'], [14, 'Plot', 'Wednesday'], [19, 'Plot', 'Thursday'], [28, 'Plot', 'Friday']]
>>> [ x for x in l if x[1] == "unPlot" ]
[[1, 'unPlot', 'Monday'], [4, 'unPlot', 'Tuesday'], [6, 'unPlot', 'Wednesday'], [1, 'unPlot', 'Thursday'], [10, 'unPlot', 'Friday'], [3, 'unPlot', 'Saturday']]
Or with filter
if you fancy functional programming:
>>> filter(lambda x: x[1] == "Plot", l)
[[8, 'Plot', 'Sunday'], [12, 'Plot', 'Monday'], [10, 'Plot', 'Tuesday'], [14, 'Plot', 'Wednesday'], [19, 'Plot', 'Thursday'], [28, 'Plot', 'Friday']]
>>> filter(lambda x: x[1] == "unPlot", l)
[[1, 'unPlot', 'Monday'], [4, 'unPlot', 'Tuesday'], [6, 'unPlot', 'Wednesday'], [1, 'unPlot', 'Thursday'], [10, 'unPlot', 'Friday'], [3, 'unPlot', 'Saturday']]
I personally find list comprehensions much clearer. It's certainly the most "pythonic" way.
Upvotes: 12
Reputation: 3274
I have a helper function for the general case of partitioning a list in two:
def partition(iterable, condition):
def partition_element(partitions, element):
(partitions[0] if condition(element) else partitions[1]).append(element)
return partitions
return reduce(partition_element, iterable, ([], []))
For example:
>>> partition([1, 2, 3, 4], lambda d: d % 2 == 0)
([2, 4], [1, 3])
Or in your case:
>>> partition(your_list, lambda i: i[1] == "Plot")
Upvotes: 1
Reputation: 1
You can also do it with the filter command:
list1 = filter(lambda x: x[1] == "Plot", list)
list2 = filter(lambda x: x[1] == "unPlot", list)
Upvotes: 0
Reputation: 1447
You could simply go through the list, and check if the value is "Plot" like this:
for i in List:
if i[1]=="Plot":
list1.append(i)
else:
list2.append(i)
Upvotes: 1
Reputation: 129914
You could use list comprehensions, e.g.
# old_list elements should be tuples if they're fixed-size, BTW
list1 = [(X, Y, Z) for X, Y, Z in old_list if Y == 'Plot']
list2 = [(X, Y, Z) for X, Y, Z in old_list if Y == 'unPlot']
If you want to traverse the input list only once, then maybe:
def split_list(old_list):
list1 = []
list2 = []
for X, Y, Z in old_list:
if Y == 'Plot':
list1.append((X, Y, Z))
else:
list2.append((X, Y, Z))
return list1, list2
Upvotes: 1
Reputation: 36777
Use list comprehension:
l = [[8, "Plot", "Sunday"], [1, "unPlot", "Monday"], [12, "Plot", "Monday"], [10, "Plot", "Tuesday"], [4, "unPlot", "Tuesday"], [14, "Plot", "Wednesday"], [6, "unPlot", "Wednesday"], [1, "unPlot", "Thursday"], [19, "Plot", "Thursday"], [28, "Plot", "Friday"], [10, "unPlot", "Friday"], [3, "unPlot", "Saturday"]]
list1 = [x for x in l if x[1] == "Plot"]
list2 = [x for x in l if x[1] == "unPlot"]
Upvotes: 0
Reputation: 6780
Try:
yourList=[[8, "Plot", "Sunday"], [1, "unPlot", "Monday"], [12, "Plot", "Monday"], [10, "Plot", "Tuesday"], [4, "unPlot", "Tuesday"], [14, "Plot", "Wednesday"], [6, "unPlot", "Wednesday"], [1, "unPlot", "Thursday"], [19, "Plot", "Thursday"], [28, "Plot", "Friday"], [10, "unPlot", "Friday"], [3, "unPlot", "Saturday"]]
plotList=[]
unPlotList=[]
for i in yourList:
if "Plot" in i:
plotList.append(i)
else:
unPlotList.append(i)
or shorter with comprehension:
plotList = [i for i in yourList if "Plot" in i]
unPlotList = [i for i in yourList if "unPlot" in i]
Upvotes: 3