在Python中将文档中的所有字母从一个列表转换为另一个列表

时间:2021-01-02 08:01:22

I have two lists

我有两个清单

list1 = ['a', 'b', 'c']
list2 = ['d', 'e', 'f']

I have a variable with random text in it.

我有一个带有随机文本的变量。

var = 'backout'

var ='backout'

I want to convert all letters in the variable that exist in list1 to the letters in list2.

我想将list1中存在的变量中的所有字母转换为list2中的字母。

expectedOutput = 'edfkout'

expectedOutput ='edfkout'

Is there a way to do this?

有没有办法做到这一点?

4 个解决方案

#1


7  

You want to use str.translate using a translation table from string.maketrans (This is str.maketrans in Python 3)

你想使用string.maketrans中的转换表来使用str.translate(这是Python 3中的str.maketrans)

from string import maketrans

s1 = 'abc'
s2 = 'def'
table = maketrans(s1, s2)
print('backout'.translate(table))

Edit:

编辑:

Note that we have to use strings instead of lists as our arguments to maketrans.

请注意,我们必须使用字符串而不是列表作为maketrans的参数。

#2


0  

We can map the keys to values using a zip()wrapped with dict() and then iterate the letters and map them to their corresponding ones with themselves being the default (in case not found):

我们可以使用用dict()包装的zip()将键映射到值,然后迭代这些字母并将它们映射到相应的字母,它们本身是默认值(如果未找到):

keys = ['a', 'b', 'c']
values = ['d', 'e', 'f']
mapper = dict(zip(keys, values))
var = 'backout'
output = "".join([mapper.get(k, k) for k in var])
print(output) # edfkout

#3


0  

Convert each char to its ascii value using map(ord,list1) and do the 1-1 mapping b/w list1 and list2 using zip

使用map(ord,list1)将每个char转换为其ascii值,并使用zip执行1-1映射b / w list1和list2

tbl = dict(zip(map(ord,list1), map(ord,list2)))
var.translate(tbl)

Output:

输出:

edfkout

#4


0  

For people who prefer shorter but more complicated solution (instead of using the translate() method), here it is:

对于喜欢更短但更复杂的解决方案(而不是使用translate()方法)的人来说,这里是:

list1 = ['a', 'b', 'c']
list2 = ['d', 'e', 'f']
var = 'backout'

trans_dict = dict(zip(list1, list2))
out_string = ''.join([trans_dict.get(ch, ch) for ch in var])

The explanation:

说明:

 dict(zip(list1, list2))

creates the {'a': 'd', 'b': 'e', 'c': 'f'} dictionary.

创建{'a':'d','b':'e','c':'f'}字典。

trans_dict.get(ch, ch)

The 1st argument is a key - if it is found in keys, we obtain its value: trans_dict[ch]

第一个参数是一个键 - 如果在键中找到它,我们得到它的值:trans_dict [ch]

The 2nd argument is a default value - used if the first argument is not found in keys. We obtain ch.

第二个参数是默认值 - 如果在键中找不到第一个参数,则使用该参数。我们得到了ch。

[trans_dict.get(ch, ch) for ch in var]

is a list comprehension - something as creating a list from the empty list, appending next and next element in the for loop.

是列表理解 - 从空列表创建列表,在for循环中追加next和next元素。

''.join(list_of_string) 

is a standard way for concatenating individual elements of the list (in our case, the individual characters).
(Instead of the empty string there may be an arbitrary string - it is used for delimiting individual elements in the concatenated string)

是连接列表中各个元素的标准方法(在我们的例子中,是单个字符)。 (而不是空字符串可能有一个任意字符串 - 它用于分隔连接字符串中的单个元素)

#1


7  

You want to use str.translate using a translation table from string.maketrans (This is str.maketrans in Python 3)

你想使用string.maketrans中的转换表来使用str.translate(这是Python 3中的str.maketrans)

from string import maketrans

s1 = 'abc'
s2 = 'def'
table = maketrans(s1, s2)
print('backout'.translate(table))

Edit:

编辑:

Note that we have to use strings instead of lists as our arguments to maketrans.

请注意,我们必须使用字符串而不是列表作为maketrans的参数。

#2


0  

We can map the keys to values using a zip()wrapped with dict() and then iterate the letters and map them to their corresponding ones with themselves being the default (in case not found):

我们可以使用用dict()包装的zip()将键映射到值,然后迭代这些字母并将它们映射到相应的字母,它们本身是默认值(如果未找到):

keys = ['a', 'b', 'c']
values = ['d', 'e', 'f']
mapper = dict(zip(keys, values))
var = 'backout'
output = "".join([mapper.get(k, k) for k in var])
print(output) # edfkout

#3


0  

Convert each char to its ascii value using map(ord,list1) and do the 1-1 mapping b/w list1 and list2 using zip

使用map(ord,list1)将每个char转换为其ascii值,并使用zip执行1-1映射b / w list1和list2

tbl = dict(zip(map(ord,list1), map(ord,list2)))
var.translate(tbl)

Output:

输出:

edfkout

#4


0  

For people who prefer shorter but more complicated solution (instead of using the translate() method), here it is:

对于喜欢更短但更复杂的解决方案(而不是使用translate()方法)的人来说,这里是:

list1 = ['a', 'b', 'c']
list2 = ['d', 'e', 'f']
var = 'backout'

trans_dict = dict(zip(list1, list2))
out_string = ''.join([trans_dict.get(ch, ch) for ch in var])

The explanation:

说明:

 dict(zip(list1, list2))

creates the {'a': 'd', 'b': 'e', 'c': 'f'} dictionary.

创建{'a':'d','b':'e','c':'f'}字典。

trans_dict.get(ch, ch)

The 1st argument is a key - if it is found in keys, we obtain its value: trans_dict[ch]

第一个参数是一个键 - 如果在键中找到它,我们得到它的值:trans_dict [ch]

The 2nd argument is a default value - used if the first argument is not found in keys. We obtain ch.

第二个参数是默认值 - 如果在键中找不到第一个参数,则使用该参数。我们得到了ch。

[trans_dict.get(ch, ch) for ch in var]

is a list comprehension - something as creating a list from the empty list, appending next and next element in the for loop.

是列表理解 - 从空列表创建列表,在for循环中追加next和next元素。

''.join(list_of_string) 

is a standard way for concatenating individual elements of the list (in our case, the individual characters).
(Instead of the empty string there may be an arbitrary string - it is used for delimiting individual elements in the concatenated string)

是连接列表中各个元素的标准方法(在我们的例子中,是单个字符)。 (而不是空字符串可能有一个任意字符串 - 它用于分隔连接字符串中的单个元素)