[英]Sorting List of Tuples with Multiconditions in Python
我正在使用Python 2.6.2。 我有一個元組pair
的列表,我想使用兩個嵌套條件pair
它們進行排序。
fwd_count
降序順序對元組進行排序, fwd_count
多個元組,count的值相同,則僅需要基於rvs_count
值按降序對那些具有相同count的元組進行排序。 fwd_count
並在rvs_count
或)元組具有相同的計數fwd_count
和不存在rvs_count
我設法編寫了以下代碼:
pair=[((0, 12), (0, 36)), ((1, 12), (0, 36)), ((2, 12), (1, 36)), ((3, 12), (1, 36)), ((1, 36), (4, 12)), ((0, 36), (5, 12)), ((1, 36), (6, 12))]
fwd_count = {}
rvs_count = {}
for link in sorted(pair):
fwd_count[link[0]] = 0
rvs_count[link[1]] = 0
for link in sorted(pair):
fwd_count[link[0]] += 1
rvs_count[link[1]] += 1
#fwd_count {(6, 12): 1, (5, 12): 1, (4, 12): 1, (1, 36): 2, (0, 36): 2}
#rvs_count {(3, 12): 1, (1, 12): 1, (1, 36): 2, (0, 12): 1, (2, 12): 1, (0, 36): 1}
fwd_count_sort=sorted(fwd_count.items(), key=lambda x: x[1], reverse=True)
rvs_count_sort=sorted(rvs_count.items(), key=lambda x: x[1])
#fwd_count_sort [((1, 36), 2), ((0, 36), 2), ((6, 12), 1), ((5, 12), 1), ((4, 12), 1)]
#rvs_count_sort [((3, 12), 1), ((1, 12), 1), ((1, 36), 2), ((0, 12), 1), ((2, 12), 1), ((0, 36), 1)]
我正在尋找的結果是:
#fwd_count_sort_final [((0, 36), 2), ((1, 36), 2), ((6, 12), 1), ((5, 12), 1), ((4, 12), 1)]
其中(1, 36)
fwd_count_sort
(1, 36)
和(0, 36)
fwd_count_sort
(0, 36)
位置已從fwd_count_sort
位置交換位置。
題:
fwd_count
和rvs_count
信息進行多條件排序? (只有元組很重要,不需要記錄排序值。),或者 我目前正在研究上面的項目#2,但是嘗試了解是否有任何更簡單的方法。
這是我在http://stygianvision.net/updates/python-sort-list-object-dictionary-multiple-key/上找到的最接近的“使用數字值進行雙向排序”的方法,但不確定是否可以如果我創建一個具有{tuple:{fwd_count:rvs_count}}關系的新字典,請使用它。
更新:2012年11月12日-已解決
我設法通過使用列表解決了這個問題。 下面是代碼,希望對那些對多條件列表進行排序的人有用。
#pair=[((0, 12), (0, 36)), ((1, 12), (1, 36)), ((2, 12), (0, 36)), ((3, 12), (1, 36)), ((1, 36), (4, 12)), ((0, 36), (5, 12)), ((1, 36), (6, 12))]
rvs_count = {}
fwd_count = {}
for link in sorted(pair):
rvs_count[link[0]] = 0
fwd_count[link[1]] = 0
for link in sorted(pair):
rvs_count[link[0]] += 1
fwd_count[link[1]] += 1
keys = []
for link in pair:
if link[0] not in keys:
keys.append(link[0])
if link[1] not in keys:
keys.append(link[1])
aggregated = []
for k in keys:
a = -1
d = -1
if k in fwd_count.keys():
a = fwd_count[k]
if k in rvs_count.keys():
d = rvs_count[k]
aggregated.append(tuple((k, tuple((a,d)) )))
def compare(x,y):
a1 = x[1][0]
d1 = x[1][1]
a2 = y[1][0]
d2 = y[1][1]
if a1 > a2:
return - a1 + a2
elif a1 == a2:
if d1 > d2:
return d1 - d2
elif d1 == d2:
return 0
else:
return d1 - d2
else:
return - a1 + a2
s = sorted(aggregated, cmp=compare)
print(s)
j = [v[0] for v in s]
print(j)
感謝Andre Fernandes,Brian和Duke對您的工作發表了意見
如果您需要交換所有對中的所有第一個元素(而不僅僅是交換fwd_count_sort=sorted(rvs_count.items(), key=lambda x: (x[0][1],-x[0][0]), reverse=True)
(1, 36)
和(0, 36)
fwd_count_sort=sorted(rvs_count.items(), key=lambda x: (x[0][1],-x[0][0]), reverse=True)
(0, 36)
),則可以執行fwd_count_sort=sorted(rvs_count.items(), key=lambda x: (x[0][1],-x[0][0]), reverse=True)
我不確定您的排序標准的定義,但這是一種根據fwd_count
和rvs_count
的值pair
列表進行排序的方法。 希望您可以使用它來獲得所需的結果。
def keyFromPair(pair):
"""Return a tuple (f, r) to be used for sorting the pairs by frequency."""
global fwd_count
global rvs_count
first, second = pair
countFirstInv = -fwd_count[first] # use the negative to reverse the sort order
countSecond = rvs_count[second]
return (first, second)
pairs_sorted = sorted(pair, key = keyFromPair)
基本思想是使用Python的內置元組排序機制對多個鍵進行排序,並反轉元組中的值之一,從而使其成為逆序排序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.