简体   繁体   English

遍历元组并将索引值与列表中的项目进行比较

[英]Iterating through tuple and compare index values with items from the list

I have a tuple and list. 我有一个元组和列表。 I need to replace items in the list with values from tuple but on the way that I'm taking item from list comparing it with indexes in tuple and if there is some match I need to take value from tuple and to replace that item in list with that value. 我需要用元组中的值替换列表中的项目,但是在从列表中获取项目并将其与元组中的索引进行比较的方式上,如果有匹配项,我需要从元组中获取值并替换列表中的项目那个价值。

If this is little bit confusing, here is a pseudeocode: 如果这有点令人困惑,那么这里是一个伪代码:

tuple = ('a','b','c','d','e','f','g','h','i')

list = ['1','4','8','3','b','g','x','4','z','r','0','0']

result = ['b','e','i','d','b','g','x','e','z','r','a','a']

I'm new in Python so I tried to implement some previous knowledge from C#, Java, JS, PHP etc but without success. 我是Python的新手,所以我尝试从C#,Java,JS,PHP等实现一些先前的知识,但没有成功。

UPDATE: This is solution for my question, thank you people to you all! 更新:这是我的问题的解决方案,谢谢大家!

input_tuple = ('a','b','c','d','e','f','g','h','i','j')
input_list = ['1','2','0','1','3','0','7','9','12','899']

lenght=len(input_tuple)-1 # find last index value in tuple

for i, v in enumerate(input_list):
    #print (i, v)       

    result = [v if not v.isdigit() or int(v)>lenght else input_tuple[int(v)] for v in input_list] #slight modification "or int(v)>lenght" to avoid if number in list is bigger then index of tuple 

print(result)

If I understand correctly, you want to replace numeric strings in your list with the corresponding string from the tuple, interpreting the numeric string as an index. 如果我理解正确,则想用元组中的相应字符串替换列表中的数字字符串,并将数字字符串解释为索引。

That's not too hard to do: 这样做并不难:

for i, v in enumerate(input_list):
    if v.isdigit():
        input_list[i] = input_tuple[int(v)]

Note that this modifies the input list in place. 请注意,这将修改输入列表。 If you don't want to do that, you can instead create a new list with a list comprehension: 如果您不想这样做,则可以使用列表理解功能创建一个新列表:

result = [v if not v.isdigit() else input_tuple[int(v)] for v in input_list]

Note that I'm not using your original names tuple and list because those are the names of the builtin types, and it's a bad idea to mask them (it can cause bugs if you want to call them in other code). 请注意,我没有使用您的原始名称tuplelist因为它们是内置类型的名称,并且屏蔽它们是一个坏主意(如果您想用其他代码调用它们,可能会导致错误)。

Use '123'.isdigit() to check whether values is digit or not. 使用'123'.isdigit()检查值是否为数字。

t = ('a','b','c','d','e','f','g','h','i')
l = ['1','4','8','3','b','g','x','4','z','r','0','0']
[list(t)[int(el)] if el.isdigit() else el for el in l]

Output: 输出:

['b', 'e', 'i', 'd', 'b', 'g', 'x', 'e', 'z', 'r', 'a', 'a']

You can use the function map() with some help function. 您可以将map()函数与一些帮助功能一起使用。 For example: 例如:

t = ('a','b','c','d','e','f','g','h','i')    
l = ['1','4','8','3','b','g','x','4','z','r','0','0']

def func(x):
    if x.isdigit():
        return  t[int(x)]
    else:
        return x

r = list(map(func, l))
# ['b', 'e', 'i', 'd', 'b', 'g', 'x', 'e', 'z', 'r', 'a', 'a']

or listcomp: 或listcomp:

r = [func(i) for i in l]

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

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