简体   繁体   中英

How should i find in a list and replace in another list(python)?

For example: want to find a duplicate "a" in

list = ["b","a","a","d"]

And replace it in another list:

second_list = ["_","_","_","_"]

so second_list will be converted to:

["_","a","a","_"]

How should I do it?
want a solution without zip, was using
guessed_word[listed_word.index(player_guess)] = player_guess
but the duplicates made a bug, any ways despite zip() ?

There are various approaches, but you could use list comperehension.

list1 = list("bcad")
list2 = ["_" for x in l1]
key = "a"
list2_replaced = [item1 if item1 == key else item2 for item1, item2 in zip(list1,list2)] 

Explanation:

Make the first list (equivalent to list1 = ["b", "c", "a", "d"])

list1 = list("bcad")

Make the second list (it may already exist in your case)

list2 = ["_" for x in l1]

Define the key you want to look for, you could also put "a" directly into the list comprehension below if you don't need it to be variable.

key = "a"

Iterate over a zipped list; zip returns a list of tuples with each tuple containing the ith element of list 1 as first entry (item1), and the ith element of list 2 as the second entry (item2). If item1 matches your key, place item1 in the second list, else use the already existing item.

list2_replaced = [item1 if item1 == key else item2 for item1, item2 in zip(list1,list2)] 

There may be better approaches, but this should work.

You can combine zip() with a list comprehension, and use a ternary if statement to take an element from the first list if it's what you want, and from the second list otherwise.

list = ["b","c","a","d"]
second_list = ["_","_","_","_"]
third_list = [(i if i == "a" else j) for (i, j) in zip(list, second_list)]
# ['_', '_', 'a', '_']
original = ["b", "d", "a", "c"]
second_list = ["_", "_", "_", "_"]
if "a" in original:
    index = original.index("a")
    second_list[index] = "a"
print second_list

output if there is an "a" in the list:

['_', '_', 'a', '_']

output if "a"` is not in the list:

['_', '_', '_', '_']

EDIT: using a fixed-length list of "_" is not advised because whenever an item is added to original , a matching "_" has to be added to second_list . to fix this, one can do

second_list = ["_"] * len(original)

which will create a list of N times "_" (N being the number of items in original )

Here first we check, if element "a" is present in first list, if yes, then we find index of "a" in first list and replace corresponding indexed element of second list with "a"

In [114]: first = ["b","c","a","d"] 
     ...: second_list = ["_","_","_","_"] 
     ...: if "a" in first:
     ...:     second_list[first.index("a")]="a" 
     ...: print(second_list)                                                                                                                  
['_', '_', 'a', '_']

Please try the below code

list1 = ["b","c","a","d"]
list2 = ["_","_","_","_"]

for idx,i in enumerate(list1):
    if(i == "a"):
        list2[idx] = list1[idx]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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