`if file.find('freq-') != -1` 是什么意思?
What does `if file.find('freq-') != -1` mean?
我是一名化学专业的学生,想编写一个脚本来从高斯输出文件中提取一些数据(如耦合常数和质子间距离)。
我找到了一个从高斯输出文件中提取化学位移的脚本。但是,我不明白 if file.find('freq-') !=-1
在脚本中是什么意思。
这是脚本的一部分(因为脚本还做其他事情,所以我刚刚播下了与我的问题相关的部分):
def read_gaussian_freq_outfiles(list_of_files):
list_of_freq_outfiles = []
for file in list_of_files:
if file.find('freq-') !=-1:
list_of_freq_outfiles.append([file,int(get_conf_number(file)),open(file,"r").readlines()])
return list_of_freq_outfiles
def read_gaussian_outputfiles():
list_of_files = []
for file in glob.glob('*.out'):
list_of_files.append(file)
return list_of_files
我认为在 def read_gaussian_outputfiles()
位中,我们创建了一个文件列表,并将所有扩展名为“.out”的文件简单地添加到列表中。
read_gaussian_freq_outfiles(list_of_files)
位可能列出了文件名中包含 "freq-" 的文件。但是 file.find('freq-')!=-1
是什么意思?
这是否意味着我们在文件名中找到的内容不等于 -1 或其他内容?
一些其他附加信息:高斯输出文件名的格式为:xxxx-opt_freq-conf-yyyy.out
其中 xxxx
是您的分子名称,yyyy
是一个数字。
当 s.find(foo)
在 s
中找不到 foo
时,它 returns -1
。因此,当 s.find(foo)
不是 return -1
时,我们知道它没有失败。
read_gaussian_freq_outfiles
在 list_of_files
中的每个文件名中查找术语 "freq-"
。如果它成功地在文件名中找到这个短语,它会附加一个包含这个文件的列表,一个 "conf number" (不确定这是什么),以及文件的内容,到一个名为 [=20 的列表=].
我创建了三个文件,goodbye.txt
、hello.txt
和 helloworld.txt
来演示用法。
在此示例中,我将打印所有以 .txt
结尾的文件,创建一个文件列表,然后打印文件名中包含短语 "goodbye"
的所有文件。这应该只打印 goodbye.txt
.
09:53 $ ls
goodbye.txt hello.txt helloworld.txt
(venv) ✔ ~/Desktop/ex
09:53 $ python
Python 2.7.11 (default, Dec 5 2015, 14:44:47)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.1.76)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import glob
>>> for file in glob.glob('*.txt'):
... print(file)
...
goodbye.txt
hello.txt
helloworld.txt
>>> list_of_files = [ file for file in glob.glob('*.txt') ]
>>> print(list_of_files)
['goodbye.txt', 'hello.txt', 'helloworld.txt']
>>> for file in list_of_files:
... if file.find('goodbye') != -1:
... print(file)
...
goodbye.txt
确实,goodbye.txt
是唯一打印的文件。
python 中的字符串查找方法查看给定字符串中出现的 sub-string (ref http://www.tutorialspoint.com/python/string_find.htm)
这里是查找所有包含'freq-' sub-string的文件名。
其他答案也显示:如果 .find() 检索到 -1,则无法找到您要查找的内容。这与 .find 将 return 作为它可以找到您的查询的第一个索引这一事实有关。所以在下面的句子中
The cat is on the mat
和 sentence.find('cat'),它将 return 4(因为 'cat' 从索引 4 开始(它从 0 开始!))。
但是,sentence.find('dog') 将 return 如果找不到它唯一可以 return 的东西:-1。如果它 returned 0 作为 'cannot find',您可能认为您的查询从索引 0 开始。使用 -1,您知道它找不到它。
我是一名化学专业的学生,想编写一个脚本来从高斯输出文件中提取一些数据(如耦合常数和质子间距离)。
我找到了一个从高斯输出文件中提取化学位移的脚本。但是,我不明白 if file.find('freq-') !=-1
在脚本中是什么意思。
这是脚本的一部分(因为脚本还做其他事情,所以我刚刚播下了与我的问题相关的部分):
def read_gaussian_freq_outfiles(list_of_files):
list_of_freq_outfiles = []
for file in list_of_files:
if file.find('freq-') !=-1:
list_of_freq_outfiles.append([file,int(get_conf_number(file)),open(file,"r").readlines()])
return list_of_freq_outfiles
def read_gaussian_outputfiles():
list_of_files = []
for file in glob.glob('*.out'):
list_of_files.append(file)
return list_of_files
我认为在 def read_gaussian_outputfiles()
位中,我们创建了一个文件列表,并将所有扩展名为“.out”的文件简单地添加到列表中。
read_gaussian_freq_outfiles(list_of_files)
位可能列出了文件名中包含 "freq-" 的文件。但是 file.find('freq-')!=-1
是什么意思?
这是否意味着我们在文件名中找到的内容不等于 -1 或其他内容?
一些其他附加信息:高斯输出文件名的格式为:xxxx-opt_freq-conf-yyyy.out
其中 xxxx
是您的分子名称,yyyy
是一个数字。
当 s.find(foo)
在 s
中找不到 foo
时,它 returns -1
。因此,当 s.find(foo)
不是 return -1
时,我们知道它没有失败。
read_gaussian_freq_outfiles
在 list_of_files
中的每个文件名中查找术语 "freq-"
。如果它成功地在文件名中找到这个短语,它会附加一个包含这个文件的列表,一个 "conf number" (不确定这是什么),以及文件的内容,到一个名为 [=20 的列表=].
我创建了三个文件,goodbye.txt
、hello.txt
和 helloworld.txt
来演示用法。
在此示例中,我将打印所有以 .txt
结尾的文件,创建一个文件列表,然后打印文件名中包含短语 "goodbye"
的所有文件。这应该只打印 goodbye.txt
.
09:53 $ ls
goodbye.txt hello.txt helloworld.txt
(venv) ✔ ~/Desktop/ex
09:53 $ python
Python 2.7.11 (default, Dec 5 2015, 14:44:47)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.1.76)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import glob
>>> for file in glob.glob('*.txt'):
... print(file)
...
goodbye.txt
hello.txt
helloworld.txt
>>> list_of_files = [ file for file in glob.glob('*.txt') ]
>>> print(list_of_files)
['goodbye.txt', 'hello.txt', 'helloworld.txt']
>>> for file in list_of_files:
... if file.find('goodbye') != -1:
... print(file)
...
goodbye.txt
确实,goodbye.txt
是唯一打印的文件。
python 中的字符串查找方法查看给定字符串中出现的 sub-string (ref http://www.tutorialspoint.com/python/string_find.htm)
这里是查找所有包含'freq-' sub-string的文件名。
其他答案也显示:如果 .find() 检索到 -1,则无法找到您要查找的内容。这与 .find 将 return 作为它可以找到您的查询的第一个索引这一事实有关。所以在下面的句子中
The cat is on the mat
和 sentence.find('cat'),它将 return 4(因为 'cat' 从索引 4 开始(它从 0 开始!))。
但是,sentence.find('dog') 将 return 如果找不到它唯一可以 return 的东西:-1。如果它 returned 0 作为 'cannot find',您可能认为您的查询从索引 0 开始。使用 -1,您知道它找不到它。