简体   繁体   English

如何用来自另一个参数的字符串替换数组中的元素

[英]How do I replace an element in an array with a string from another parameter

So I am a beginner in Python and programming.所以我是 Python 和编程的初学者。

How can I replace an element in an array with a string from another parameter.如何用来自另一个参数的字符串替换数组中的元素。

Basically lets say there is an array = ["a","s","d","f","g","h]基本上可以说有一个array = ["a","s","d","f","g","h]

and I want to replace the 3rd element of that array with a string "Z".我想用字符串“Z”替换该数组的第三个元素。

This is what I have came up with这是我想出的

def xyz(array,string):
    string = ("")
    array = []

    for value in array:
        replace = value.replace(3,"string")
        return replace

您可以像这样直接操作列表:

array[2] = "Z"

Simply do this:只需这样做:

array[2]='Z'

then this is what you get:那么这就是你得到的:

array = ["a", "s", "d", "f", "g", "h"]
array[2]
>>'d'
array[2]='Z'
array
>>['a', 's', 'Z', 'f', 'g', 'h']

暂无
暂无

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

相关问题 如何用字符串替换lxml中的元素 - How do I replace an element in lxml with a string 如何通过使用来自另一个数组的 pop 方法填充一个空数组的元素来“替换”一个数组? - How do i 'replace' an array by filling an empty array's elements using pop method from another array? 如何用 python 中同一数组的另一个值替换数组值? - How do I replace array value with another value from same array in python? 如何用另一个数组的元素替换一个数组的元素? - How do you replace the elements of an array with elements from another array? 用另一个列表中的字符串替换列表元素 - Replace element of list with string from another list 如何用 Python 中的另一个字符替换字符串中的一个字符? - How do I replace a character in a string with another character in Python? 如何用同一索引中的另一个新元素替换元素并将前一个元素移动到下一个索引 - How do I replace an element with another new element in the same index and move the previous element to the next index 如何替换二维数组中的特定字符串? - How do I replace a specific string in a 2d array? 如何用另一行中的值替换NaN? - How do I replace NaNs with the values from another row? 如何将一个数组的0、2、3、4个元素与另一个数组的0、2、3、4个元素匹配,并在python中从两个数组中打印出第5个元素? - How do I match 0,2,3,4 elements of one array to 0,2,3,4 elements of another array and print the 5th element from both the arrays in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM