目录指南中的Python列表文件-listdir VS system(“ ls”)通过示例进行解释

🔹欢迎 (🔹 Welcome)

If you want to learn how these functions work behind the scenes and how you can use their full power, then this article is for you.

如果您想了解这些功能在后台如何工作以及如何充分利用它们的功能,那么本文适合您。

We will start by diving into concepts that are essential to work with listdir and system:

我们将首先探讨对于使用listdirsystem必不可少的概念:

  • The built-in Python os module and how to import it.

    内置的Python os模块以及如何导入它。

  • The concepts of "directory" and "current working directory".

    “目录”和“当前工作目录”的概念。
  • How to check and change your current working directory.

    如何检查和更改您当前的工作目录。
  • The difference between an absolute path and a relative path.

    绝对路径和相对路径之间的差异。

Then, we will dive into the functions themselves:

然后,我们将深入研究这些函数:

  • How to work with the listdir function and when to use it.

    如何使用listdir函数以及何时使用它。

  • How to work with the system("ls") function and when to use it.

    如何使用system("ls")函数以及何时使用它。

  • Examples of both of them and how they work behind the scenes.

    两者的示例以及它们在后台的工作方式。

Let's begin! ⭐

让我们开始! ⭐

OS操作系统模块 (🔸 The OS Module)

The two functions that we will discuss: listdir() and system() belong to the os module. This module includes functions that are used to interact with your operating system, performing actions like:

我们将讨论的两个函数: listdir()system()属于os模块。 该模块包括用于与操作系统交互的功能,它们执行以下操作:

  • Making a new directory.

    制作一个新目录。
  • Renaming an existing directory.

    重命名现有目录。
  • Removing a directory.

    删除目录。
  • Displaying the path to your current working directory.

    显示当前工作目录的路径。
  • Much more!

    多得多!

💡 Tips:

💡 提示:

  • A directory is what we commonly know as a "folder", where we usually store related files and/or other directories, creating a hierarchy of directories within directories that are called subdirectories. An example of a directory is your "Documents" folder.

    目录是我们通常所说的“文件夹”,我们通常在其中存储相关文件和/或其他目录,从而在称为子目录的目录内创建目录的层次结构。 目录的一个示例是“文档”文件夹。

  • A module is a file that contains related Python code.

    模块是包含相关Python代码的文件。

如何导入操作系统模块 (How to Import the OS Module)

To use the os module in your script, you need to "import" it. Importing a module means gaining access to all the functions and variables that are stored within the module. We import a module when we want to use its code in our script.

要在脚本中使用os模块,您需要“导入”它。 导入模块意味着可以访问模块中存储的所有功能和变量。 当我们想在脚本中使用其代码时,我们将导入一个模块。

To import the os module, you simply need to include this line at the top of your Python script or run this line in the interactive shell:

要导入os模块,您只需要在Python脚本的顶部包含以下行或在交互式shell中运行此行:

import os

This will give you access to all the functions defined in the os module.

这将使您可以访问os模块中定义的所有功能。

💡 Tip: this module was already installed when you installed Python 3, so you will be able to use it immediately.

💡 提示:安装Python 3时已经安装了此模块,因此您可以立即使用它。

To be able to use the functions from the os module, you will need to add the prefix os. before the name of the function that you want to call, like this:

为了能够使用os模块中的功能,您将需要添加前缀os. 要调用的函数名称之前,如下所示:

os.<function>(<params>)

For example:

例如:

os.mkdir("New Folder")

如何导入单个功能 (How to Import Individual Functions)

If you are only going to work with one or two functions from the module, you can import them individually using this syntax:

如果只打算使用模块中的一个或两个功能,则可以使用以下语法分别导入它们:

from <module> import <function1>, <function2>, ...

For example:

例如:

from os import listdir, system

In this case, you can call the functions in your script as you normally would, without adding the os. prefix, like this:

在这种情况下,您可以像往常一样在脚本中调用函数, 而无需添加os. 前缀,像这样:

<function>(<params>)

For example:

例如:

mkdir("New Folder")

🔹当前工作目录 (🔹 Current Working Directory)

Now let's see a very important concept that you need to know before you start working with listdir and system. Your current working directory, as the name implies, is the directory (folder) where you are currently working.

现在,让我们看看在开始使用listdirsystem之前需要了解的一个非常重要的概念。 顾名思义,您当前的工作目录就是您当前工作的目录(文件夹)。

You can check your current working directory with this function from the os module:

您可以从os模块中使用此功能检查当前的工作目录:

os.getcwd()

This will show you the path to your current working directory.

这将向您显示当前工作目录的路径。

💡 Tip: cwd means "current working directory."

💡 提示: cwd意思是“当前工作目录”。

从交互式外壳 (From the Interactive Shell)

If I run this command in the interactive shell (Windows), I see this:

如果在交互式外壳程序(Windows)中运行此命令,则会看到以下信息:

>>> os.getcwd()
'C:\\Users\\estef\\AppData\\Local\\Programs\\Python\\Python38-32'

This is the full path to my current working directory:

这是我当前工作目录的完整路径:

'C:\\Users\\estef\\AppData\\Local\\Programs\\Python\\Python38-32'

从脚本 (From a Script)

If I run this command from a script, like this:

如果我从脚本运行此命令,如下所示:

import os
print(os.getcwd())

I see:

我懂了:

C:\Users\estef\Documents\freeCodeCamp\freeCodeCamp News\listdir vs system

The full path to the script (its location in the system, in the hierarchy of directories).

脚本的完整路径(其在系统中的位置,在目录层次结构中)。

💡 Tip: If you run a script (a Python file), your current working directory is the directory where the script is currently in.

💡 提示:如果运行脚本(Python文件),则当前的工作目录是脚本当前所在的目录。

如何更改当前工作目录 (How to Change your Current Working Directory)

You can change your current working directory with this command from the os module:

您可以通过os模块中的以下命令更改当前工作目录:

os.chdir(<path>)

You will need to specify the path to the new working directory, passing it as an argument, formatted as a string. It can be either an absolute path or a relative path.

您将需要指定新工作目录的路径,并将其作为参数传递,格式为字符串。 它可以是绝对路径,也可以是相对路径。

💡 Tip:

💡 提示:

  • An absolute path specifies all the sequence of directories that you need to go through to reach your target directory. This path starts from the root directory of your system.

    绝对路径指定到达目标目录所需的所有目录顺序。 此路径从系统的根目录开始。

For example:

例如:

>>> import os
>>> os.chdir(r"C:\Users\estef\Documents\FreeCodeCamp\freeCodeCamp News\9 - listdir vs system")# Checking current working directory:
>>> os.getcwd()
'C:\\Users\\estef\\Documents\\FreeCodeCamp\\freeCodeCamp News\\9 - listdir vs system'

Notice that I added an r before the absolute path to convert the string into a raw string. If you use \ and you don't add the r, you will get an error because the \ symbol will be treated as a special character.

请注意,我在绝对路径之前添加了r ,以将字符串转换为原始字符串。 如果您使用\而未添加r ,则会出现错误,因为\符号将被视为特殊字符。

Alternatively, you could replace the backslashes  \ with forward slashes / in the path:

或者,您可以在路径中将反斜杠\替换为正斜杠/

>>> os.chdir("C:/Users/estef/Documents/FreeCodeCamp/freeCodeCamp News/9 - listdir vs system")# Checking current working directory
>>> os.getcwd()
'C:\\Users\\estef\\Documents\\FreeCodeCamp\\freeCodeCamp News\\9 - listdir vs system'
  • A relative path specifies the path that you want to follow to find the target directory, but now the path starts from your current working directory. It's shorter and simpler than the absolute path.

    相对路径指定了要查找目标目录所要遵循的路径,但是现在该路径从当前工作目录开始。 它比绝对路径更短,更简单。

For example, if your current working directory contains a subdirectory (folder) Directory 1, you can move to this directory using a relative path (imagine it as a folder within another folder, and we are going deeper and deeper into the hierarchy), like this:

例如,如果您当前的工作目录包含一个子目录(文件夹) Directory 1 ,则可以使用相对路径(将其想象为另一个文件夹中的一个文件夹,并将其深入层次结构)移至该目录。这个:

>>> import os
>>> os.chdir(".\Directory 1")# Check current working directory
>>> os.getcwd()
'C:\\Users\\estef\\Documents\\FreeCodeCamp\\freeCodeCamp News\\9 - listdir vs system\\Directory 1'

💡 Tip: The dot (.) at the beginning of the relative path .\Directory 1 represents the current working directory. A double dot ( ..) is used to move up the hierarchy, to the "parent" directory.

💡 提示:相对路径.\Directory 1开头的点( . )表示当前工作目录。 双点( .. )用于将层次结构向上移动到“父”目录。

Now that you have all the background knowledge that you will need to truly understand how listdir and system work, let's see them in detail.

现在,您已经具备了真正了解listdirsystem如何工作所需的所有背景知识,下面让我们对其进行详细介绍。

🔸Listdir (🔸 Listdir)

We will start with the listdir function. Let's reveal its mysteries. 🔮

我们将从listdir函数开始。 让我们揭示它的奥秘。 🔮

目的和返回值 (Purpose and Return Value)

According to the Python Documentation, the purpose of this function is to:

根据Python文档 ,此功能的目的是:

Return a list containing the names of the entries in the directory given by path.

返回包含path指定的目录中条目名称的列表。

Basically, this function returns a list with the names of all files and directories that are currently found within a particular directory that you specify when you call the function.

基本上,此函数返回一个列表,其中包含在调用该函数时指定的特定目录中当前找到的所有文件和目录的名称。

💡 Tip: The list will not have a specific order, even if you usually sort the elements alphabetically.

💡 提示:即使您通常按字母顺序对元素进行排序,列表也没有特定的顺序。

语法和参数 (Syntax and Parameter)

To call listdir, will need to use this syntax:

调用listdir ,将需要使用以下语法:

The parameter path is precisely that, the absolute or relative path to the directory that you want to visualize. In Python 3.2 and above, this parameter is optional. By default, the path will lead to your current working directory if you don't pass an argument.

参数path正是要显示的目录的绝对或相对路径。 在Python 3.2及更高版本中,此参数是可选的。 默认情况下,如果不传递参数,该路径将导致当前工作目录。

Remember that you must import the os module before calling this function.

请记住,在调用此函数之前,必须导入os模块。

💡 Tip: If you use this import statement from os import listdir to import the function individually, you can omit the os. prefix, like this:

提示:如果您使用from os import listdir import语句单独导入函数,则可以省略os. 前缀,像这样:

用例和优势 (Use Cases and Advantages)

The function listdir is very helpful because it works on any operating system where Python runs, so if Python is installed on your device, this function will work correctly.

listdir函数非常有用,因为它可在运行Python的任何操作系统上运行,因此,如果您的设备上安装了Python,则此函数将正常运行。

Now let's talk a little bit about its return value. Since it returns a list, we can store this list in a variable and work with it in our program.

现在让我们谈谈它的返回值。 由于它返回一个列表,因此我们可以将该列表存储在变量中并在程序中使用它。

For example, let's say that we want to do something with all the files from a given directory, such as converting images to black and white or modifying their content. We could do it using a for loop, like this:

例如,假设我们要处理给定目录中的所有文件,例如将图像转换为黑白或修改其内容。 我们可以使用for循环来做到这一点,就像这样:

images = os.listdir(<path>)for image in images:# Do something to the image

Of course, you would need to define what happens within the loop, but this is an example of what you could do with this function.

当然,您需要定义循环中发生的事情,但这是您可以使用此函数进行操作的一个示例。

This is awesome, right?

太棒了吧?

But having files and directories in the same list can be a little bit problematic if we want to work with a for loop, right? We would need to add a conditional to check the type of each element. How can we make a list that only contains file names (no directories) or vice versa?

但是,如果我们要使用for循环,将文件和目录放在同一列表中可能会有些问题,对吧? 我们需要添加一个条件来检查每个元素的类型。 我们如何制作仅包含文件名(无目录)的列表,反之亦然?

Let's see! ✨

让我们来看看! ✨

仅包含文件 (Only Include Files)

If you want to "filter" the list returned by os.listdir() to include only files (no directories) you can use this line of code:

如果要“过滤” os.listdir()返回的列表以仅包含文件 (不包含目录),则可以使用以下代码行:

list(filter(os.path.isfile, os.listdir(<path>)))

💡 Tip: You can customize the <path> argument or omit it to use your current working directory.

💡 提示:您可以自定义<path>参数或忽略它以使用当前工作目录。

Let's see an example with my current working directory (I'm using Windows):

让我们来看一个当前工作目录的示例(我正在使用Windows):

My directory (folder) has:

我的目录(文件夹)有:

  • Two subdirectories (folders within the main folder)

    两个子目录(主文件夹中的文件夹)
  • One PowerPoint file

    一个PowerPoint文件
  • One image (.png file)

    一幅图片(.png文件)
  • One Python script

    一个Python脚本

If I call the listdir function from the script.py file and print the list returned:

如果我从script.py文件调用listdir函数并打印返回的列表:

print(os.listdir())

This is the output:

这是输出:

['Diagrams.ppt', 'Directory 1', 'Directory 2', 'listdir vs system.png', 'script.py']

You can see that all files and directories from my current working directory were included.

您可以看到包含了当前工作目录中的所有文件和目录。

To filter the list to only contain files, we can use this statement:

要过滤列表以仅包含文件,我们可以使用以下语句:

print(list(filter(os.path.isfile, os.listdir())))

Now the output is:

现在的输出是:

['Diagrams.ppt', 'listdir vs system.png', 'script.py']

Notice how the directories were "filtered", exactly what we wanted.

注意目录是如何被“过滤”的,正是我们想要的。

仅包括目录 (Only Include Directories)

Similarly, if you want to "filter" the list to include only directories, you can use this line of code:

同样,如果您要“过滤”列表以仅包含目录 ,则可以使用以下代码行:

list(filter(os.path.isdir, os.listdir(<path>)))

Now the output is:

现在的输出是:

['Directory 1', 'Directory 2']

Exactly what we wanted. But how does this statement work behind the scenes? Let's see.

正是我们想要的。 但是,这种说法在幕后如何运作? 让我们来看看。

filter()在幕后的工作方式 (How filter() Works Behind the Scenes)

The filter function is called using this syntax:

使用以下语法调用filter函数:

filter(<function>, <list>)

It basically "filters" the elements of the second argument (the list) based on the truth value returned by calling the function passed as the first argument (os.path.isfile() or os.path.isdir() in their respective commands):

基本上,它基于通过调用作为第一个参数传递的函数( os.path.isfile()os.path.isdir()在各自的命令中os.path.isdir()返回的真值而“过滤”第二个参数(列表)的元素。 ):

print(list(filter(os.path.isfile, os.listdir())))
list(filter(os.path.isdir, os.listdir()))

These two functions:

这两个功能:

os.path.isfile(<path>)os.path.isdir(<path>)

Return True if the argument is a file or a directory, respectively.  

如果参数分别是文件或目录,则返回True

Based on these truth values, the elements of the list will be included (or not) in the final "filtered" list. The elements of the list returned by os.listdir() are passed one by one to these functions to check if they are files (or directories, respectively).

基于这些真值,列表的元素将包含(或不包含)在最终的“过滤”列表中。 os.listdir()返回的列表元素将一一传递给这些函数,以检查它们是否分别是文件(或目录)。

For example: If we have this line of code:

例如:如果我们有以下代码行:

filter(os.path.isfile, os.listdir())))

And os.listdir() returns this list:

os.listdir()返回以下列表:

['Diagrams.ppt', 'Directory 1', 'Directory 2', 'script.py']

The first element of the list ('Diagrams.ppt') is passed as argument to os.path.isfile() to check if it's a file :

列表的第一个元素( 'Diagrams.ppt' )作为参数传递给os.path.isfile()以检查它是否为文件:

os.path.isfile('Diagrams.ppt') # True

The function call returns True, so it's a file and it's included in the list.

该函数调用返回True ,因此它是一个文件,并且包含在列表中。

But if the element is a directory:

但是,如果元素是目录:

os.path.isfile('Directory 1') # False

The function call returns False, so it's not included in the list. This process continues for every element in the list until the new list only contains file names.

该函数调用返回False ,因此它不包含在列表中。 对于列表中的每个元素,此过程将继续进行,直到新列表仅包含文件名为止。

Then, since filter() returns an iterable, we make a list from this iterable using list():

然后,由于filter()返回一个可迭代对象,因此我们使用list()从该可迭代对象中创建一个列表:

list(filter(os.path.isfile, os.listdir()))

And we print it since we are working with a Python file (script):

由于我们正在使用Python文件(脚本),因此我们将其打印出来:

print(list(filter(os.path.isfile, os.listdir())))

💡 Tip: You can visually identify if an element of the list represents a file or a directory by seeing if it has an extension (type) after its name. For example: Diagrams.ppt has a .ppt extension that tells you that it's a PowerPoint file but a directory doesn't have an extension, like 'Directory 1'.

提示:您可以通过查看列表中元素的名称后是否具有扩展名(类型)来直观地识别列表中的元素代表文件还是目录。 例如: Diagrams.ppt具有.ppt扩展名,它告诉您它是PowerPoint文件,但目录没有扩展名,如'Directory 1'

🔹系统(“ ls”) (🔹 System("ls"))

Now that you know how to work with listdir, let's see how the system() function works behind the scenes and how you can use it. 🔮

现在,您知道如何使用listdir ,让我们看看system()函数如何在后台工作以及如何使用它。 🔮

目的 (Purpose )

According to the Python Documentation, the purpose of the system() function is to:

根据Python文档 , system()函数的目的是:

Execute the command (a string) in a subshell
在子shell中执行命令(字符串)

Basically, this function takes a command (as a string) and executes it.

基本上,此函数接受命令(作为字符串)并执行它。

In this case, the command that we are passing is 'ls' , a Unix command used in Linux to display the content of a directory as standard output.

在这种情况下,我们传递的命令是'ls' ,这是Linux中使用的Unix命令,用于将目录的内容显示为标准输出。

Unlike listdir, the system() function will not return a list if we pass the 'ls' command, it will only display the list of files and directories as standard output. Therefore, you should use it if you only want to visualize the list without actually working with it in your program.

listdir不同,如果我们通过'ls'命令, system()函数将不会返回列表 ,它只会将文件和目录的列表显示为标准输出。 因此,如果您只想可视化列表而不在程序中实际使用它,则应使用它。

语法和参数 (Syntax and Parameter)

To call this function, you will need to use this syntax:

要调用此函数,您将需要使用以下语法:

Its only argument is the command that you want to execute formatted as a string (surrounded by double quotes or single quotes).

它唯一的参数是要执行的命令,格式为字符串(用双引号或单引号引起来)。

Particularly, the ls command lets you see the content of your current working directory.

特别是,通过ls命令,您可以查看当前工作目录的内容。

For example: if this is my working directory (three Python files and one subdirectory):

例如:如果这是我的工作目录(三个Python文件和一个子目录):

And I call the system() function, like this:

然后调用system()函数,如下所示:

>>> import os
>>> os.system("ls")

This is the output:

这是输出:

'Directory 1'  'file 1.py'  'file 2.py'   main.py
0

We can see the standard output in the console (the list of files and directories):

我们可以在控制台中看到标准输出(文件和目录的列表):

'Directory 1'  'file 1.py'  'file 2.py'   main.py

and the return value:

和返回值:

0

💡 Note: For these examples of the system() function, I'm working with an online command line tool called Repl.it since my computer has Windows installed and the command ls is not recognized by the default command prompt.

💡 注意:对于这些system()函数的示例,由于我的计算机已安装Windows,并且默认命令提示符无法识别ls命令,因此我正在使用名为Repl.it的在线命令行工具。

局限性 (Limitations)

One of the main limitation of this function is that the command passed as argument has to be recognized by the operating system or environment that you are working with.

此功能的主要限制之一是作为参数传递的命令必须由您使用的操作系统或环境识别。

For example, the ls command will not be recognized in Windows by default in the command prompt. You will see this error if you try to run it:

例如,默认情况下,在命令提示符下,Windows无法识别ls命令。 如果尝试运行它,将会看到此错误:

'ls' is not recognized as an internal or external command, operable program or batch file.
无法将“ ls”识别为内部或外部命令,可操作程序或批处理文件。

A similar command in Windows would be the 'dir' command:

Windows中类似的命令是'dir'命令:

os.system('dir')

💡 Tip: There are alternative ways to run the ls command on Windows, such as using terminal programs that recognize Unix commands, but by default Windows does not recognize the 'ls' command.

💡提示:还有其他方法可以在Windows上运行ls命令,例如使用识别Unix命令的终端程序,但是默认情况下Windows无法识别'ls'命令。

返回值 (Return Value)

According to the Python documentation:

根据Python文档 :

On Unix, the return value is the exit status of the process encoded in the format specified for wait().

在Unix上,返回值是以为wait()指定的格式编码的进程的退出状态。

and...

和...

On Windows, the return value is that returned by the system shell after running command.

在Windows上,返回值是在运行command之后由系统外壳返回的值。

💡  Tip: Note that this function does not return a list. It simply displays the list as standard output, so you can't store it in a variable like you did with listdir.

💡 提示:请注意,此功能不会返回列表。 它只是将列表显示为标准输出,因此您不能像使用listdir一样将其存储在变量中。

ls命令的变体 (Variations of the ls command)

A key feature of os.system('ls') is that it has many helpful and interesting options to customize how present the output. Let's see some of them.

os.system('ls')的关键功能是它具有许多有用且有趣的选项,可自定义输出的显示方式。 让我们来看一些。

Option 1: We can show more information about files and directories such as their size, location, and modification date and time using the command ls -l.

选项1:我们可以使用命令ls -l显示有关文件和目录的更多信息,例如文件和目录的大小,位置以及修改日期和时间。

>>> import os
>>> os.system('ls -l')
total 12
drwxr-xr-x 1 runner runner  0 Apr  3 18:23 'Directory 1'
-rw-r--r-- 1 runner runner 11 Apr  3 18:38 'file 1.py'
-rw-r--r-- 1 runner runner 11 Apr  3 18:38 'file 2.py'
-rw-r--r-- 1 runner runner 11 Apr  3 18:38  main.py
0

Option 2: To be able to visually recognize directories faster, we can use ls -F, which adds a forward slash / to the end of their names (see 'Directory 1/' below).

选项2:为了能够在视觉上更快地识别目录,我们可以使用ls -F ,在其名称的末尾添加一个斜杠/ (请参见下面的'Directory 1/' )。

>>> import os
>>> os.system('ls -F')
'Directory 1'/  'file 1.py'  'file 2.py'   main.py
0

Option 3: To sort the files by size, we can use the command ls -lS.

选项3:要按大小对文件排序,我们可以使用命令ls -lS

>>> import os
>>> os.system('ls -lS')
total 12
-rw-r--r-- 1 runner runner 11 Apr  3 18:38 'file 1.py'
-rw-r--r-- 1 runner runner 11 Apr  3 18:38 'file 2.py'
-rw-r--r-- 1 runner runner 11 Apr  3 18:38  main.py
drwxr-xr-x 1 runner runner  0 Apr  3 18:23 'Directory 1'
0

There are many more options for customization that can be helpful for your particular goal. Here you can find more information about the -ls command and how you can use its full power.

还有许多自定义选项可以对您的特定目标有所帮助。 在这里,您可以找到有关-ls命令以及如何使用其全部功能的更多信息 。

list listdir与system(“ ls”)的摘要 (🔸 Summary of listdir vs. system("ls"))

  • Purpose: listdir returns the list of file names and directories in the path specified (by default, the current working directory) while system("ls") only displays them as standard output.

    用途: listdir返回指定路径(默认情况下为当前工作目录)中文件名和目录的列表,而system("ls")仅将它们显示为标准输出。

  • Operating System: listdir can be used independently of the operating system that you are working with. In contrast, system('ls') has to be executed in an operating system or environment that recognizes the 'ls' command.

    操作系统: listdir可以独立于所使用的操作系统使用。 相反,必须在可识别'ls'命令的操作系统或环境中执行system('ls')

  • Customization: you can filter the list returned by listdir if you need to remove files or directories using the filter() function and you can pass options to customize the output of system('ls').

    自定义:如果需要使用filter()函数删除文件或目录,则可以过滤listdir返回的列表,并且可以传递选项以自定义system('ls')的输出。

I really hope that you liked my article and found it helpful. Now you can work with these functions in your Python projects. Check out my online courses. Follow me on Twitter. 👍

我真的希望您喜欢我的文章并发现它对您有所帮助。 现在,您可以在Python项目中使用这些功能。 查看我的在线课程 。 在Twitter上关注我。 👍

翻译自: https://www.freecodecamp.org/news/python-list-files-in-a-directory-guide-listdir-vs-system-ls-explained-with-examples/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390550.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java多线程并发学习-进阶大纲

1、synchronized 的实现原理以及锁优化&#xff1f;2、volatile 的实现原理&#xff1f;3、Java 的信号灯&#xff1f;4、synchronized 在静态方法和普通方法的区别&#xff1f;5、怎么实现所有线程在等待某个事件的发生才会去执行&#xff1f;6、CAS&#xff1f;CAS 有什么缺陷…

大数据定律与中心极限定理_为什么中心极限定理对数据科学家很重要?

大数据定律与中心极限定理数据科学 (Data Science) The Central Limit Theorem is at the center of statistical inference what each data scientist/data analyst does every day.中心极限定理是每个数据科学家/数据分析师每天所做的统计推断的中心。 Central Limit Theore…

useEffect语法讲解

useEffect语法讲解 用法 useEffect(effectFn, deps)能力 useEffect Hook 相当于 componentDidMount&#xff0c;componentDidUpdate 和 componentWillUnmount 这三个函数的组合。 可以模拟渲染后、更新后、销毁三个动作。 案例演示 渲染后更新标题 useEffect(()>{doc…

leetcode 726. 原子的数量

给定一个化学式formula&#xff08;作为字符串&#xff09;&#xff0c;返回每种原子的数量。 原子总是以一个大写字母开始&#xff0c;接着跟随0个或任意个小写字母&#xff0c;表示原子的名字。 如果数量大于 1&#xff0c;原子后会跟着数字表示原子的数量。如果数量等于 1…

web相关基础知识1

2017-12-13 09:47:11 关于HTML 1.绝对路径和相对路径 相对路径&#xff1a;相对于文件自身为参考。 &#xff08;工作中一般是使用相对路径&#xff09; 这里我们用html文件为参考。如果说html和图片平级&#xff0c;那直接使用src 如果说图片在和html平级的文件夹里面&#xf…

JavaScript循环:标签语句,继续语句和中断语句说明

标签声明 (Label Statement) The Label Statement is used with the break and continue statements and serves to identify the statement to which the break and continue statements apply. Label语句与break和continue语句一起使用&#xff0c;用于标识break和continue语…

马约拉纳费米子:推动量子计算的“天使粒子”

据《人民日报》报道&#xff0c;以华人科学家为主体的科研团队找到了正反同体的“天使粒子”——马约拉纳费米子&#xff0c;从而结束了国际物理学界对这一神秘粒子长达80年的漫长追寻。该成果由加利福尼亚大学洛杉矶分校何庆林、王康隆课题组&#xff0c;美国斯坦福大学教授张…

leetcode 1711. 大餐计数

大餐 是指 恰好包含两道不同餐品 的一餐&#xff0c;其美味程度之和等于 2 的幂。 你可以搭配 任意 两道餐品做一顿大餐。 给你一个整数数组 deliciousness &#xff0c;其中 deliciousness[i] 是第 i​​​​​​​​​​​​​​ 道餐品的美味程度&#xff0c;返回你可以用…

您的第一个简单的机器学习项目

This article is for those dummies like me, who’ve never tried to know what machine learning was or have left it halfway for the sole reason of being overwhelmed. Follow through every line and stay along. I promise you’d be quite acquainted with giving yo…

eclipse报Access restriction: The type 'BASE64Decoder' is not API处理方法

今天从svn更新代码之后&#xff0c;由于代码中使用了BASE64Encoder 更新之后报如下错误&#xff1a; Access restriction: The type ‘BASE64Decoder’ is not API (restriction on required library ‘D:\java\jdk1.7.0_45\jre\lib\rt.jar’) 解决其实很简单&#xff0c;把JR…

【跃迁之路】【451天】程序员高效学习方法论探索系列(实验阶段208-2018.05.02)...

(跃迁之路)专栏 实验说明 从2017.10.6起&#xff0c;开启这个系列&#xff0c;目标只有一个&#xff1a;探索新的学习方法&#xff0c;实现跃迁式成长实验期2年&#xff08;2017.10.06 - 2019.10.06&#xff09;我将以自己为实验对象。我将开源我的学习方法&#xff0c;方法不断…

react jest测试_如何使用React测试库和Jest开始测试React应用

react jest测试Testing is often seen as a tedious process. Its extra code you have to write, and in some cases, to be honest, its not needed. But every developer should know at least the basics of testing. It increases confidence in the products they build,…

面试题 17.10. 主要元素

题目 数组中占比超过一半的元素称之为主要元素。给你一个 整数 数组&#xff0c;找出其中的主要元素。若没有&#xff0c;返回 -1 。请设计时间复杂度为 O(N) 、空间复杂度为 O(1) 的解决方案。 示例 1&#xff1a; 输入&#xff1a;[1,2,5,9,5,9,5,5,5] 输出&#xff1a;5 …

简单团队-爬取豆瓣电影T250-项目进度

本次主要讲解一下我们的页面设计及展示最终效果&#xff1a; 页面设计主要用到的软件是&#xff1a;html&#xff0c;css&#xff0c;js&#xff0c; 主要用的编译器是&#xff1a;sublime&#xff0c;dreamweaver&#xff0c;eclipse&#xff0c;由于每个人使用习惯不一样&…

鸽子为什么喜欢盘旋_如何为鸽子回避系统设置数据收集

鸽子为什么喜欢盘旋鸽子回避系统 (Pigeon Avoidance System) Disclaimer: You are reading Part 2 that describes the technical setup. Part 1 gave an overview of the Pigeon Avoidance System and Part 3 provides details about the Pigeon Recognition Model.免责声明&a…

scrum认证费用_如何获得专业Scrum大师的认证-快速和慢速方式

scrum认证费用A few months ago, I got the Professional Scrum Master Certification (PSM I). 几个月前&#xff0c;我获得了专业Scrum Master认证(PSM I)。 This is a trending certification nowadays, because most companies operate with some sort of agile methodolo…

981. 基于时间的键值存储

创建一个基于时间的键值存储类 TimeMap&#xff0c;它支持下面两个操作&#xff1a; set(string key, string value, int timestamp) 存储键 key、值 value&#xff0c;以及给定的时间戳 timestamp。 get(string key, int timestamp) 返回先前调用 set(key, value, timesta…

前端开发-DOM

文档对象模型&#xff08;Document Object Model&#xff0c;DOM&#xff09;是一种用于HTML和XML文档的编程接口。它给文档提供了一种结构化的表示方法&#xff0c;可以改变文档的内容和呈现方式。我们最为关心的是&#xff0c;DOM把网页和脚本以及其他的编程语言联系了起来。…

css 绘制三角形_解释CSS形状:如何使用纯CSS绘制圆,三角形等

css 绘制三角形Before we start. If you want more free content but in video format. Dont miss out on my Youtube channel where I publish weekly videos on FrontEnd coding. https://www.youtube.com/user/Weibenfalk----------Are you new to web development and CSS?…