如何解释Python 3.6中的str.maketrans函数? [英] How to explain the str.maketrans function in Python 3.6?

查看:58
本文介绍了如何解释Python 3.6中的str.maketrans函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在参加 Udacity 课程,该课程指导学生使用 Python 进行编程.其中一个项目让学生重命名目录中的照片文件(删除名称中的任何数字),以便按字母顺序排列文件,然后将拼出一条秘密信息.例如,如果文件名是 "48athens",程序会试图删除数字,只留下 "athens" 作为文件名.

I am currently participating in an Udacity course that instructs students on programming using Python. One of the projects has students rename photo files (remove any numbers in the name) in a directory in order to have the files arranged alphabetically, after which a secret message will be spelled out. For instance, if a file name is "48athens", the program seeks to remove the numbers, leaving only "athens" as the file name.

我使用的是 Python 3.6,而课程讲师使用的是 Python 2.7.我应该使用 Python 2.7 来简化学习过程.但是,目前我将继续使用 Python 3.6.

I am using Python 3.6, while the course instructor is using Python 2.7. I should likely be using Python 2.7 so as to simplify the learning process. However, for now I will keep using Python 3.6.

教师重命名文件的方式是使用 .translate 函数,该函数在 Python 2.x 中接受两个参数,而 Python 3.x 只接受一个参数.它从文件名中删除任何数字(0 到 9).

The way in which the instructor has the files renamed is using the .translate function, which takes two arguments in Python 2.x, while Python 3.x only takes one argument. It removes any numbers (0 through 9) from the file names.

import os

def rename_files(): #Obtain the file names from a folder.
    file_list = os.listdir(r"C:\Users\Dennis\Desktop\OOP\prank\prank")
    print (file_list)
    saved_path = os.getcwd()
    os.chdir(r"C:\Users\Dennis\Desktop\OOP\prank\prank")
    for file_name in file_list: #Rename the files inside of the folder.
        os.rename(file_name, file_name.translate(None, "0123456789"))
    os.chdir(saved_path)

rename_files()

然而,这在 Python 3.x 中不起作用,正如它所说:

However, this does not work in Python 3.x, as it says that:

TypeError: translate() takes exactly one argument (2 given)

谢天谢地,我找到了另一种使用他人帮助的方法.但是,我不确定它是如何工作的.有人可以向我解释 str.maketrans 函数,以及引号中的前两个空白参数的用途吗?我的想法是它的意思是:对于文件名中的前两个字符,删除任何数字(0 到 9).那是对的吗?例如,在 "48athens" 中,如果前两个字符(4 和 8)是 0 到 9 之间的数字,则删除它们.

Thankfully, I found another way using someone's assistance. However, I'm not really sure how it works. Can someone explain the str.maketrans function to me, and what the first two blank arguments in quotes are for? My thought is that it's saying: for the first two characters in the file name, remove any numbers (0 through 9). Is that correct? For instance, in "48athens", remove the first two characters (4 and 8) if they are numbers between 0 and 9.

import os

def rename_files(): #Obtain the file names from a folder.
    file_list = os.listdir(r"C:\Users\Dennis\Desktop\OOP\prank\prank")
    print (file_list)
    saved_path = os.getcwd()
    os.chdir(r"C:\Users\Dennis\Desktop\OOP\prank\prank")
    for file_name in file_list: #Rename the files inside of the folder.
        os.rename(file_name, file_name.translate(str.maketrans('','','0123456789')))
    os.chdir(saved_path)

rename_files()

我对文档的理解:

静态 str.maketrans(x[, y[, z]])此静态方法返回可用于 str.translate() 的转换表.

static str.maketrans(x[, y[, z]]) This static method returns a translation table usable for str.translate().

它是说传递给 str.maketrans 的参数,连同实际的函数 str.maketrans,将创建​​一个表格,上面写着如果这个字符出现,用这个字符替换它."但是,我不确定括号的用途.

It's saying that the arguments passed to str.maketrans, along with the actual function str.maketrans, will make a table that says, "If this character appears, replace it with this character." However, I'm not sure what the brackets are for.

如果只有一个参数,则必须是映射Unicode的字典序数(整数)或字符(长度为 1 的字符串)到 Unicode序数,字符串(任意长度)或无.字符键将然后转换为序数.

If there is only one argument, it must be a dictionary mapping Unicode ordinals (integers) or characters (strings of length 1) to Unicode ordinals, strings (of arbitrary lengths) or None. Character keys will then be converted to ordinals.

它是说它只能将整数或长度为 1 的字符串中的字符更改为其他整数或字符串(您想要的任何长度).但我相信我有三个论点,而不是一个.

It's saying that it can only change integers, or characters in strings of length one, to other integers or strings (of any length you want). But I believe I have three arguments, not one.

如果有两个参数,它们必须是等长的字符串,并且在生成的字典中,x 中的每个字符都将映射到y 中相同位置的字符.如果有第三个参数,它必须是一个字符串,其字符将被映射到 None 中结果.

If there are two arguments, they must be strings of equal length, and in the resulting dictionary, each character in x will be mapped to the character at the same position in y. If there is a third argument, it must be a string, whose characters will be mapped to None in the result.

我有三个参数 ('', '', '0123456789').我认为 x 是第一个 '',而 y 是第二个 ''.我有第三个参数,它是一个字符串 '0123456789',但我不明白映射到 'None' 意味着什么.

I have three arguments ('', '', '0123456789'). I think x is the first '', and y is the second ''. I have the third argument, which is a string '0123456789', but I don't understand what it means to be mapped to 'None'.

推荐答案

str.maketrans 建立一个转换表,它是一个整数或字符到整数、字符串或 None 的映射.把它想象成一个字典,其中的键代表输入字符串中的字符,而它们映射的值代表输出字符串中的字符.

str.maketrans builds a translation table, which is a mapping of integers or characters to integers, strings, or None. Think of it like a dictionary where the keys represent characters in the input string and the values they map to represent characters in the output string.

我们遍历字符串以翻译并替换在映射中作为键出现的所有内容,无论其在映射中的值是什么,或者如果该值是 None,则将其删除.

We go through the string to translate and replace everything that appears as a key in the mapping with whatever its value in the map is, or remove it if that value is None.

您可以使用一个、两个或三个参数构建一个转换表(我认为这可能会让您感到困惑).有一个论点:

You can build a translation table with one, two, or three arguments (I think this may be what's confusing you). With one argument:

str.maketrans({'a': 'b', 'c': None})

您为函数提供了一个遵循转换表规则的映射,它返回该映射的等效表.映射到 None 的东西被删除

You give the function a mapping that follows the rules for translation tables and it returns an equivalent table for that mapping. Things that map to None are removed

有两个参数:

str.maketrans('abc', 'xyz')

你给它两个字符串.第一个字符串中的每个字符都被第二个字符串中该索引处的字符替换.所以 'a' 映射到 'x''b' 映射到 'y''c''z'.

You give it two strings. Each character in the first string is replaced by the character at that index in the second string. So 'a' maps to 'x', 'b' to 'y', and 'c' to 'z'.

您正在使用的带有三个参数的参数与两个参数的工作方式相同,但具有第三个字符串.

The one you're using, with three arguments, works the same as two arguments, but has a third string.

str.maketrans('abc', 'xyz', 'hij')

这与两个参数版本相同,只是删除了第三个字符串中的字符,就像它们被映射到 None 一样.所以你的表说不要替换任何东西,但删除出现在这个字符串中的字符".

This is the same as the two argument version, except that the characters from the third string are removed, as if they were mapped to None. So your table is saying "Don't replace anything, but remove the characters that show up in this string".

这篇关于如何解释Python 3.6中的str.maketrans函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆