在Python中,根据项目子集的顺序对列表进行排序
假设变量列表在Python中,根据项目子集的顺序对列表进行排序,python,list,algorithm,Python,List,Algorithm,假设变量列表{a,b,c,d,e}并且基于一些值,它们的顺序应该是c>b>d>e>a。我拥有的是列表的有序子集,例如: b>d>a c>e b>e>a c>b>a等等 使用列表子集的顺序,如何获得列表的完整顺序。如果不存在任何两个元素的比较,则它们各自的顺序将不重要,例如,如果不存在d和e的比较,则它们各自的顺序将不重要。将每个有序子集视为定义图中的有向边(变量是顶点),因此,例如,b>d>a定义边b->d和d->a,然后是结果图。将每个有序子集视为定义图中的有向边(变量是顶点),因此,例如,b
{a,b,c,d,e}
并且基于一些值,它们的顺序应该是c>b>d>e>a
。我拥有的是列表的有序子集,例如:
b>d>a
c>e
b>e>a
c>b>a
等等
使用列表子集的顺序,如何获得列表的完整顺序。如果不存在任何两个元素的比较,则它们各自的顺序将不重要,例如,如果不存在
d
和e
的比较,则它们各自的顺序将不重要。将每个有序子集视为定义图中的有向边(变量是顶点),因此,例如,b>d>a定义边b->d和d->a,然后是结果图。将每个有序子集视为定义图中的有向边(变量是顶点),因此,例如,b>d>a定义边b->d和d->a,然后是结果图。要解决上述问题,请执行以下步骤:
步骤1:为每个字符指定一个唯一的整数。。。我用了一本字典
list_alpha = [None,'a','b','c','d','e']
dictionary = dict()
for i in range(1,len(list_alpha)):
dictionary[list_alpha[i]] = i
reset_list = []
n = int(input())
for seq in range(n):
comparison = input()
comparison_list = comparison.split(" ")
reset_list.append(comparison_list)
index_list = []
for j in comparison_list:
index_list.append(dictionary[j])
index_list.sort()
for i,j in enumerate(comparison_list):
dictionary[j] = index_list[i]
for comparison_list in reset_list:
index_list = []
for j in comparison_list:
index_list.append(dictionary[j])
index_list.sort()
for i,j in enumerate(comparison_list):
dictionary[j] = index_list[i]
tp = []
for i,j in dictionary.items():
tp.append((i,j))
tp.sort(key = lambda x : (x[1]))
for i,j in tp:
print(i)
第2步:然后以列表形式进行比较
第3步:列出他们在字典中的值……对该列表进行排序,并按相同顺序将值分配给角色的比较列表
步骤4:使用重置列表再次执行步骤3,以保持正确的比较顺序
最后,通过执行这些操作,您将按照字典中的值对字符进行排序,从而获得正确的顺序
list_alpha = [None,'a','b','c','d','e']
dictionary = dict()
for i in range(1,len(list_alpha)):
dictionary[list_alpha[i]] = i
reset_list = []
n = int(input())
for seq in range(n):
comparison = input()
comparison_list = comparison.split(" ")
reset_list.append(comparison_list)
index_list = []
for j in comparison_list:
index_list.append(dictionary[j])
index_list.sort()
for i,j in enumerate(comparison_list):
dictionary[j] = index_list[i]
for comparison_list in reset_list:
index_list = []
for j in comparison_list:
index_list.append(dictionary[j])
index_list.sort()
for i,j in enumerate(comparison_list):
dictionary[j] = index_list[i]
tp = []
for i,j in dictionary.items():
tp.append((i,j))
tp.sort(key = lambda x : (x[1]))
for i,j in tp:
print(i)
输入:
4
b d a
c e
b e a
c b a
c
b
d
e
a
输出:
4
b d a
c e
b e a
c b a
c
b
d
e
a
要解决上述问题,请执行以下步骤: 步骤1:为每个字符指定一个唯一的整数。。。我用了一本字典
list_alpha = [None,'a','b','c','d','e']
dictionary = dict()
for i in range(1,len(list_alpha)):
dictionary[list_alpha[i]] = i
reset_list = []
n = int(input())
for seq in range(n):
comparison = input()
comparison_list = comparison.split(" ")
reset_list.append(comparison_list)
index_list = []
for j in comparison_list:
index_list.append(dictionary[j])
index_list.sort()
for i,j in enumerate(comparison_list):
dictionary[j] = index_list[i]
for comparison_list in reset_list:
index_list = []
for j in comparison_list:
index_list.append(dictionary[j])
index_list.sort()
for i,j in enumerate(comparison_list):
dictionary[j] = index_list[i]
tp = []
for i,j in dictionary.items():
tp.append((i,j))
tp.sort(key = lambda x : (x[1]))
for i,j in tp:
print(i)
第2步:然后以列表形式进行比较
第3步:列出他们在字典中的值……对该列表进行排序,并按相同顺序将值分配给角色的比较列表
步骤4:使用重置列表再次执行步骤3,以保持正确的比较顺序
最后,通过执行这些操作,您将按照字典中的值对字符进行排序,从而获得正确的顺序
list_alpha = [None,'a','b','c','d','e']
dictionary = dict()
for i in range(1,len(list_alpha)):
dictionary[list_alpha[i]] = i
reset_list = []
n = int(input())
for seq in range(n):
comparison = input()
comparison_list = comparison.split(" ")
reset_list.append(comparison_list)
index_list = []
for j in comparison_list:
index_list.append(dictionary[j])
index_list.sort()
for i,j in enumerate(comparison_list):
dictionary[j] = index_list[i]
for comparison_list in reset_list:
index_list = []
for j in comparison_list:
index_list.append(dictionary[j])
index_list.sort()
for i,j in enumerate(comparison_list):
dictionary[j] = index_list[i]
tp = []
for i,j in dictionary.items():
tp.append((i,j))
tp.sort(key = lambda x : (x[1]))
for i,j in tp:
print(i)
输入:
4
b d a
c e
b e a
c b a
c
b
d
e
a
输出:
4
b d a
c e
b e a
c b a
c
b
d
e
a
如果您有一个合适的比较函数,那么您应该能够或多或少地直接使用python的内置
列表。sort
(或sorted
)通过cmp_to_键进行排序(尽管我建议这样做是出于警告,我可能缺少一些完全不同且更有效的解决方案)
例如:
from functools import cmp_to_key
def compare(x, y):
orders = [['b', 'd', 'a'],
['c', 'e'],
['b', 'e', 'a'],
['c', 'b', 'a']]
for order in orders:
if x in order and y in order:
if order.index(x) > order.index(y):
return 1
else:
return -1
return 0
print(sorted(['a', 'b', 'c', 'd', 'e'],
key=cmp_to_key(compare)))
这使得:
['c', 'b', 'd', 'e', 'a']
下面是一个更高效的版本,它使用集合进行包含测试,尽管它依赖于可散列的项:
from functools import cmp_to_key
orders = [['b', 'd', 'a'],
['c', 'e'],
['b', 'e', 'a'],
['c', 'b', 'a']]
order_pairs = [(set(order), order) for order in orders]
def compare(x, y):
for order_set, order in order_pairs:
if x in order_set and y in order_set:
if order.index(x) > order.index(y):
return 1
else:
return -1
return 0
print(sorted(['a', 'b', 'c', 'd', 'e'],
key=cmp_to_key(compare)))
如果您有一个合适的比较函数,那么您应该能够或多或少地直接使用python的内置列表。sort
(或sorted
)通过cmp_to_键进行排序(尽管我建议这样做是出于警告,我可能缺少一些完全不同且更有效的解决方案)
例如:
from functools import cmp_to_key
def compare(x, y):
orders = [['b', 'd', 'a'],
['c', 'e'],
['b', 'e', 'a'],
['c', 'b', 'a']]
for order in orders:
if x in order and y in order:
if order.index(x) > order.index(y):
return 1
else:
return -1
return 0
print(sorted(['a', 'b', 'c', 'd', 'e'],
key=cmp_to_key(compare)))
这使得:
['c', 'b', 'd', 'e', 'a']
下面是一个更高效的版本,它使用集合进行包含测试,尽管它依赖于可散列的项:
from functools import cmp_to_key
orders = [['b', 'd', 'a'],
['c', 'e'],
['b', 'e', 'a'],
['c', 'b', 'a']]
order_pairs = [(set(order), order) for order in orders]
def compare(x, y):
for order_set, order in order_pairs:
if x in order_set and y in order_set:
if order.index(x) > order.index(y):
return 1
else:
return -1
return 0
print(sorted(['a', 'b', 'c', 'd', 'e'],
key=cmp_to_key(compare)))
另请参阅:&另请参阅:&请参阅我想我已使用您提到的相同方法解决了此问题。。。不使用functools模块。你能确认一下吗?@YashShah对不起,我没有详细研究你的算法。我想我已经用你提到的方法解决了它。。。不使用functools模块。你能确认一下吗?@YashShah对不起,我还没有详细看过你的算法。