简体   繁体   English

python替换列表中的项目

[英]python replacing item in list

Hi my question relates to a post a long time ago by some user here: 嗨我的问题与很久以前某个用户的帖子有关:

Find and replace string values in Python list 在Python列表中查找并替换字符串值

For my specific situation. 对于我的具体情况。 I have a list like this: 我有一个这样的列表:

appl =  ['1', 'a', 'a', '2', 'a']

I want to replace only the "a single unknown" instance of "a" with just an empty space(that holds the position of where "a" used to be). 我想只用一个空的空格(它保存“a”的位置)来替换“a”的“单个未知”实例。 I am unsure of how to do it for only one character. 我不确定如何只为一个角色做这件事。 Can anyone help? 有人可以帮忙吗? Thanks in advance. 提前致谢。

EDIT: i Should mention that I need to use an "index" function to first locate the "a" since it is a changing variable. 编辑:我应该提到我需要使用“索引”函数来首先找到“a”,因为它是一个变化的变量。 And then I need code that will replace the character. 然后我需要代替字符的代码。

EDIT2: A lot of people are assuming the index will be 2, but I want to point out that the index of the character is not known. EDIT2:很多人都假设索引会是2,但我想指出的是该字符的索引是未知的。 Also "a" will be present in the list approximately ~20 times(will remain a fixed number) but their locations will be changing. 此外,“a”将在列表中出现约20次(将保留固定数字),但它们的位置将会发生变化。 I want to replace an "a" based on user input. 我想根据用户输入替换“a”。 This is the actual list I am working with: 这是我正在使用的实际列表:

track  [2] =   ["|","@","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"]

@ is a charachter, | @是一个charachter,| is a border and " " is an empty space. 是一个边界,“”是一个空的空间。 A user enters input to choose how much @ moves to the right and a new image is displayed that shows the new location of @ and the old location of @ is replaced with a space. 用户输入输入以选择向右移动的数量,并显示一个新图像,显示@的新位置,并将@的旧位置替换为空格。 The length of the list remains constant. 列表的长度保持不变。 This is the context of my question. 这是我的问题的背景。

You can simply find the index of the second occurrence by traversing the list and storing the indexes whose elements are 'a' until you've found two: 您可以通过遍历列表并存储元素为'a'的索引来找到第二个匹配项的索引,直到找到两个:

>>> appl = ['1', 'a', 'a', '2', 'a']
>>> idxs = (i for i,c in enumerate(appl) if c == 'a')
>>> next(idxs)
0
>>> appl[next(idxs)] = ''
>>> appl
['1', 'a', '', '2', 'a']

You may use index to achieve this goal, along with a loop. 您可以使用index来实现此目标,以及循环。 Here is a little function for it: 这是一个小功能:

def replace_element(li, ch, num, repl):
    last_found = 0
    for _ in range(num):
        last_found = li.index(ch, last_found+1)
    li[li.index(ch, last_found)] = repl

use: 使用:

replace_element(appl, 'a', 2, ' ')

This method is particularly nice because it throws the following error when the charactor is not in the list: 此方法特别好,因为当列表不在列表中时会抛出以下错误:

ValueError: 'a' is not in list
appl = ['1', 'a', 'a', '2', 'a']
a_indices = [i for i, x in enumerate(appl) if x == 'a']

if len(a_indices) > 1:
    appl[a_indices[1]] = ' '

First, find the first occurrence of the value: 首先,找到第一次出现的值:

>>> appl = ['1', 'a', 'a', '2', 'a']
>>> first = appl.index('a')
>>> first
1

To find the second, start at the first location plus one: 要找到第二个,从第一个位置加一个:

>>> second = appl.index('a', first+1)
>>> second
2

Then set that location to a space like you want: 然后将该位置设置为您想要的空间:

>>> appl[second] = ' '
>>> appl
['1', 'a', ' ', '2', 'a']

It seems your list (with "|" , "@" ) is short and "@" is always present, just in different locations. 看来你的列表( "|""@" )很短, "@"总是存在,只是在不同的位置。 In this case you can replace the first occurrence of "@" in the appl list in one line: 在这种情况下,您可以在一行中替换appl列表中第一次出现的"@"

appl[appl.index("@")] = " "

If the list is large and the item might not be present: 如果列表很大且项目可能不存在:

i = next((i for i, x in enumerate(lst) if x == item), None)
if i is not None:
   lst[i] = replacement

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

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