如果你不知道索引(Python),如何交换列表中的元素,同时保持它们的索引相同

时间:2017-11-16 09:44:17

标签: python python-2.7 list

说我有这两个清单:

list1=["Green","2","Blue","4"]
list2=["1","Orange","3","red"]

如果我想将'3'与'blue'交换,如果我不知道列表中元素的位置,我该怎么办?

我正在使用Python 2.7

1 个答案:

答案 0 :(得分:2)

list1=["Green","2","Blue","4"]
list2=["1","Orange","3","red"]

l1 = list1.index("Blue")
l2 = list2.index("3")

list1[l1], list2[l2] = list2[l2], list1[l1]