列表中的Python索引更改

发布于 2025-01-23 20:48:04 字数 652 浏览 0 评论 0原文

# write a function that accepts a list and the indexes of two elements in the list,
# and swaps that two elements of the list, and return the swapped list. 
# for example, if the arguments of the function are [23,65,19,90]m index1 = 1, and index2 = 3,
# then it will return [23,90,19,65].


# Swap function

def swapLst(newLst):
    size = len(newLst)
     
    # Swapping
    temp = newLst[0]
    newLst[0] = newLst[size - 1]
    newLst[size - 1] = temp
     
    return newLst
     
newLst = [23,65,19,90]
print(swapLst(newLst))

您好,我的问题:如何更改代码以更改列表中的任何索引。我的程序只会更改和最后一个索引,我需要帮助。谢谢你!

# write a function that accepts a list and the indexes of two elements in the list,
# and swaps that two elements of the list, and return the swapped list. 
# for example, if the arguments of the function are [23,65,19,90]m index1 = 1, and index2 = 3,
# then it will return [23,90,19,65].


# Swap function

def swapLst(newLst):
    size = len(newLst)
     
    # Swapping
    temp = newLst[0]
    newLst[0] = newLst[size - 1]
    newLst[size - 1] = temp
     
    return newLst
     
newLst = [23,65,19,90]
print(swapLst(newLst))

Hello, My question: How can I change my code to change the any index in the list. My program only changes first and last index, I needed help with that. Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

断桥再见 2025-01-30 20:48:04

您可以使用a,b = b,a在python中交换变量,因此您可以将列表元素与其索引交换:

a = [1, 2, 3, 1, 2, 3, 5] 

def swap(l, i, j):
    l[i], l[j] = l[j], l[i]
    return l
print(swap(a, 0, 1))

输出:

[2, 1, 3, 1, 2, 3, 5]

You can swap variables in python using a, b = b, a, so you can swap list elements with their indexes:

a = [1, 2, 3, 1, 2, 3, 5] 

def swap(l, i, j):
    l[i], l[j] = l[j], l[i]
    return l
print(swap(a, 0, 1))

Output:

[2, 1, 3, 1, 2, 3, 5]
挽清梦 2025-01-30 20:48:04

为了这样做,您也需要接受其他整数作为参数。
a,b = b,a
更容易交换
代码:

def swapLst(newlst, in1, in2): 
    newlst[in1], newlst[in2] = newlst[in2], newlst[in1]
    return newlst
 
newLst = [23,65,19,90] 
print(swapLst(newLst, 1, 3))

输出:

[23, 90, 19, 65]

In order to do so, you need to accept the other integers as arguments as well.
And swapping is easier as a, b = b, a
Code:

def swapLst(newlst, in1, in2): 
    newlst[in1], newlst[in2] = newlst[in2], newlst[in1]
    return newlst
 
newLst = [23,65,19,90] 
print(swapLst(newLst, 1, 3))

Output:

[23, 90, 19, 65]
逆蝶 2025-01-30 20:48:04

您可以修改swaplst函数以在任何给定的索引上交换元素:

def swapLst(lst, index1, index2):
    # Check if indices are valid
    if index1 >= len(lst) or index2 >= len(lst):
        print("Invalid index")
        return lst
    
    # Swapping
    temp = lst[index1]
    lst[index1] = lst[index2]
    lst[index2] = temp
     
    return lst

# Example usage
lst = [23,65,19,90]
new_lst = swapLst(lst, 1, 3)
print(new_lst)   # Output: [23, 90, 19, 65]

you can modify the swapLst function to swap the elements at any given indices:

def swapLst(lst, index1, index2):
    # Check if indices are valid
    if index1 >= len(lst) or index2 >= len(lst):
        print("Invalid index")
        return lst
    
    # Swapping
    temp = lst[index1]
    lst[index1] = lst[index2]
    lst[index2] = temp
     
    return lst

# Example usage
lst = [23,65,19,90]
new_lst = swapLst(lst, 1, 3)
print(new_lst)   # Output: [23, 90, 19, 65]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文