繁体   English   中英

用多个元素交换 Python 中的元组列表

[英]Swap list of tuples in Python with multiple elements

我正在尝试在元组列表中交换城市及其各自的位置:

my_cities = [("London", "Rome", 38.197274, -84.86311),
                ("Michigan", "Denver", 39.161921, -75.526755),
                ("Paris", "Lisbon", 44.95, -93.094)]

所以每个城市都有一个新的对,所以 output 应该是一个包含新城市对+它们的位置的元组列表。

我试过了:

def swap_cities(my_cities):
    for x in range(0, len(my_cities) - 1): 
        my_cities[x] = (my_cities[x+1][0], my_cities[x+1][0])

        return my_cities

但这并没有给我正确的结果:

[('Michigan', 'Michigan'),
 ('Michigan', 'Denver', 39.161921, -75.526755),
 ('Paris', 'Lisbon', 44.95, -93.094)]

预期结果(创建新对):

 my_cities = [("Michigan", "Rome", 39.161921, -84.86311),
                ("London", "Lisbon", 38.197274, -93.094),
                ("Paris", "Denver", 44.95, -75.526755)]

正如亚历山大在他的评论中所说,您的 return 语句缩进太远并且在 for 循环内。 因此,只会发生一次迭代。 您的循环当前将当前索引的值设置为 NEXT 索引的第一个元素,两次(作为元组)。

这就是为什么您将("Michigan", "Michigan")视为列表末尾的第一个元素。

还不清楚你想如何交换城市,但看起来你想轮换它们。 即,如果您有:

city1A --> city1B
city2A --> city2B
city3A --> city3B

然后在切换后它们都会循环向下移动一个:

city1A --> city3B
city2A --> city1B
city3A --> city2B

因为你总是需要原始城市的知识,所以你不能直接覆盖它们,你需要某种“缓冲区”来保留一个城市或所有原始城市。 下面有两个例子。 一个单独保留原始列表并且不更改任何值,而另一个更改输入列表就地 由您决定这是否是您想要的。

my_cities = [("London", "Rome", 38.197274, -84.86311),
                ("Michigan", "Denver", 39.161921, -75.526755),
                ("Paris", "Lisbon", 44.95, -93.094)]

def swap_cities(cities):
    """ 
    Rotates the 2nd city and 2nd location through the list. Does not modify
    cities.

    Input:
        cities: [(str, str, float, float), ...] - The list of city pair tuples

    Returns: A list of new city pairs where the 2nd city and 2nd loc have been
    swapped.
    """
    return [(cities[x][0], cities[x-1][1], cities[x][2], cities[x-1][3]) for x in range(len(cities))]

def swap_cities_inplace(cities):
    """
    Modifies the input list in place to rotate the 2nd city and location through
    the list of city pairs. Explicitly returns None to remind you that this
    modification is IN PLACE.

    Input:
        cities: [(str, str, float, float), ...] - The list of city pair tuples

    Returns: None, but modifies cities in place.
    """
    temp = [(cities[x][0], cities[x-1][1], cities[x][2], cities[x-1][3]) for x in range(len(cities))]
    for i in range(len(cities)):
        cities[i] = temp[i]
    return None

print("Original cities")
print(my_cities)

print("\nSwap cities, not in place")
print(swap_cities(my_cities))
print(f"And see my_cities is unchanged:\n{my_cities}")


print("\nSwap cities in place. This is what my_cities looks like after")
swap_cities_inplace(my_cities)
print(my_cities)

Output:

Original cities
[('London', 'Rome', 38.197274, -84.86311), ('Michigan', 'Denver', 39.161921, -75.526755), ('Paris', 'Lisbon', 44.95, -93.094)]

Swap cities, not in place
[('London', 'Lisbon', 38.197274, -93.094), ('Michigan', 'Rome', 39.161921, -84.86311), ('Paris', 'Denver', 44.95, -75.526755)]
And see my_cities is unchanged:
[('London', 'Rome', 38.197274, -84.86311), ('Michigan', 'Denver', 39.161921, -75.526755), ('Paris', 'Lisbon', 44.95, -93.094)]

Swap cities in place. This is what my_cities looks like after
[('London', 'Lisbon', 38.197274, -93.094), ('Michigan', 'Rome', 39.161921, -84.86311), ('Paris', 'Denver', 44.95, -75.526755)]

所以那里可能有一个令人生畏的寻找循环理解。 让我们分解一下。 在这两个函数中,理解是

[(my_cities[x][0], my_cities[x-1][1], my_cities[x][2], my_cities[x-1][3]) for x in range(len(my_cities))]

第一个元素是同一个城市——记住我们不会改变它。 第二个元素是前一个条目的第二个元素。 这是我们交换的城市。 第三个元素与第一个城市的位置相同。 第四个元素是前一个条目的第二个城市的位置。 然后,我们只需使用x来遍历原始列表的所有索引。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM