Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用包含所有唯一元素的列表的索引替换列表中的元素_Python_List_Replace_Graph - Fatal编程技术网

Python 用包含所有唯一元素的列表的索引替换列表中的元素

Python 用包含所有唯一元素的列表的索引替换列表中的元素,python,list,replace,graph,Python,List,Replace,Graph,假设我有一个列表,其中包含图形的唯一节点a-D: List1 = [A, B, C, D] 以及另一个列表,其中包含具有成对元素的图形的边: List2 = [[C, D], [B, A], [D, A], [C, B]] 如何编写一种快速、经济的方法,用List1中的元素索引替换List2中的所有元素,从而得到: List3 = [(2, 3), (1, 0), (3, 0), (2, 1)] 我目前的做法是: for i in range(len(List2)): List3[

假设我有一个列表,其中包含图形的唯一节点
a
-
D

List1 = [A, B, C, D]
以及另一个列表,其中包含具有成对元素的图形的边:

List2 = [[C, D], [B, A], [D, A], [C, B]]
如何编写一种快速、经济的方法,用
List1
中的元素索引替换
List2
中的所有元素,从而得到:

List3 = [(2, 3), (1, 0), (3, 0), (2, 1)]
我目前的做法是:

for i in range(len(List2)):
    List3[i] = (List1.index(List2[i][0]), List1.index(List2[i][1]))

然而,对于一个大的列表来说,这需要很长的时间,我正试图找到一种可能更快的方法来实现这一点。

您可以创建一个从字母到所需索引的
dict

>>> indices = {key: index for index, key in enumerate(List1)}
>>> indices
{'B': 1, 'D': 3, 'A': 0, 'C': 2}
然后使用嵌套列表

>>> [[indices[i] for i in sub] for sub in List2]
[[2, 3], [1, 0], [3, 0], [2, 1]]
编辑
获取元组列表的步骤

>>> [tuple(indices[i] for i in sub) for sub in List2]
[(2, 3), (1, 0), (3, 0), (2, 1)]

您可以创建一个从字母到所需索引的
dict

>>> indices = {key: index for index, key in enumerate(List1)}
>>> indices
{'B': 1, 'D': 3, 'A': 0, 'C': 2}
然后使用嵌套列表

>>> [[indices[i] for i in sub] for sub in List2]
[[2, 3], [1, 0], [3, 0], [2, 1]]
编辑
获取元组列表的步骤

>>> [tuple(indices[i] for i in sub) for sub in List2]
[(2, 3), (1, 0), (3, 0), (2, 1)]

您可以创建一个从字母到所需索引的
dict

>>> indices = {key: index for index, key in enumerate(List1)}
>>> indices
{'B': 1, 'D': 3, 'A': 0, 'C': 2}
然后使用嵌套列表

>>> [[indices[i] for i in sub] for sub in List2]
[[2, 3], [1, 0], [3, 0], [2, 1]]
编辑
获取元组列表的步骤

>>> [tuple(indices[i] for i in sub) for sub in List2]
[(2, 3), (1, 0), (3, 0), (2, 1)]

您可以创建一个从字母到所需索引的
dict

>>> indices = {key: index for index, key in enumerate(List1)}
>>> indices
{'B': 1, 'D': 3, 'A': 0, 'C': 2}
然后使用嵌套列表

>>> [[indices[i] for i in sub] for sub in List2]
[[2, 3], [1, 0], [3, 0], [2, 1]]
编辑
获取元组列表的步骤

>>> [tuple(indices[i] for i in sub) for sub in List2]
[(2, 3), (1, 0), (3, 0), (2, 1)]

你可以使用列表理解和列表索引的方法来实现你想要的

List1 = ['A', 'B', 'C', 'D']

List2 = [['C', 'D'], ['B', 'A'], ['D', 'A'], ['C', 'B']]

print [ (List1.index(item[0]),List1.index(item[1])) for item in List2]

你可以使用列表理解和列表索引的方法来实现你想要的

List1 = ['A', 'B', 'C', 'D']

List2 = [['C', 'D'], ['B', 'A'], ['D', 'A'], ['C', 'B']]

print [ (List1.index(item[0]),List1.index(item[1])) for item in List2]

你可以使用列表理解和列表索引的方法来实现你想要的

List1 = ['A', 'B', 'C', 'D']

List2 = [['C', 'D'], ['B', 'A'], ['D', 'A'], ['C', 'B']]

print [ (List1.index(item[0]),List1.index(item[1])) for item in List2]

你可以使用列表理解和列表索引的方法来实现你想要的

List1 = ['A', 'B', 'C', 'D']

List2 = [['C', 'D'], ['B', 'A'], ['D', 'A'], ['C', 'B']]

print [ (List1.index(item[0]),List1.index(item[1])) for item in List2]


如果需要重复执行,为什么不创建一个映射,例如
{a:1,B:2,…}
,它可以通过
枚举
轻松创建?如果需要重复执行,为什么不创建一个映射,例如
{a:1,B:2,…}
,它可以通过
枚举
轻松创建?如果需要重复执行,为什么不创建一个映射,例如
{a:1,B:2,…}
,它可以通过
枚举
轻松创建?如果需要重复创建,为什么不创建一个映射,例如
{a:1,B:2,…}
,它可以通过
枚举
轻松创建,但这需要您为
列表2中的每个子元素调用
列表1.index
,这效率非常低。感谢您指出我的错误。我将在我原来的PostAlternative中更改它,使用
enumerate(List1,1)
使用基于1的索引,而不是
+1
。@jonrsharpe TIL
enumerate
可以接受第二个参数:)教我如何使用RTFMhaha@user4911943当然,只需将
[]
tuple()交换即可
在内部列表理解中,但这要求您为
List2
中的每个子元素调用
List1.index
,这非常低效。感谢您指出我的错误。我将在我原来的PostAlternative中更改它,使用
enumerate(List1,1)
使用基于1的索引,而不是
+1
。@jonrsharpe TIL
enumerate
可以接受第二个参数:)教我如何使用RTFMhaha@user4911943当然,只需将
[]
tuple()交换即可
在内部列表理解中,但这要求您为
List2
中的每个子元素调用
List1.index
,这非常低效。感谢您指出我的错误。我将在我原来的PostAlternative中更改它,使用
enumerate(List1,1)
使用基于1的索引,而不是
+1
。@jonrsharpe TIL
enumerate
可以接受第二个参数:)教我如何使用RTFMhaha@user4911943当然,只需将
[]
tuple()交换即可
在内部列表理解中,但这要求您为
List2
中的每个子元素调用
List1.index
,这非常低效。感谢您指出我的错误。我将在我原来的PostAlternative中更改它,使用
enumerate(List1,1)
使用基于1的索引,而不是
+1
。@jonrsharpe TIL
enumerate
可以接受第二个参数:)教我如何使用RTFMhaha@user4911943当然,只需使用内部列表中的
tuple()
替换掉
[]