2012-01-30 61 views
3

我有一個這樣的名單:如何Python列表分爲兩個列表,按照元素的某些方面

[[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"]] 

我想把它分成根據PlotunPlot值兩份名單,導致:

list1=[[8, "Plot", "Sunday"], [12, "Plot", "Monday"], ...] 
list2=[[1, "unPlot", "Monday"], [4, "unPlot", "Tuesday"], ...] 

回答

8

嘗試使用基本列表理解:

>>> [ 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']] 

或用filter,如果你看中了函數式編程:

>>> 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']] 

我perso最終找到更清晰的列表理解。這當然是最「pythonic」的方式。

+0

感謝這樣一個簡單的解決方案。 – 2012-01-30 14:42:37

2

嘗試:

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) 

或更短與補償rehension:

plotList = [i for i in yourList if "Plot" in i] 
unPlotList = [i for i in yourList if "unPlot" in i] 
+0

ezdazuzena明白了......應該工作......只要確保先將plotlist和unplotlist定義爲空列表即可。 – maneesha 2012-01-30 14:32:44

0

使用列表理解:

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"] 
1

您可以使用列表解析,例如,

# 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'] 

如果你想遍歷輸入列表中只有一次,那麼也許:

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 
1

你可以簡單地瀏覽清單,並檢查是否值是「暗算」是這樣的:

for i in List: 
    if i[1]=="Plot": 
    list1.append(i) 
    else: 
    list2.append(i) 
0

你也可以用filter命令做到這一點:

list1 = filter(lambda x: x[1] == "Plot", list) 
list2 = filter(lambda x: x[1] == "unPlot", list) 
1

我有分區列表一般情況下的輔助函數有兩種:

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, ([], [])) 

例如:

>>> partition([1, 2, 3, 4], lambda d: d % 2 == 0) 
([2, 4], [1, 3]) 

或者你的情況:

>>> partition(your_list, lambda i: i[1] == "Plot") 
4
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) 

這樣你迭代一次列表

相關問題