'and', 'or' for python 字符串之间的不等语句
'and', 'or' for python unequal statement between strings
我正在使用 python 读取文本文件。如果第一个字符不等于 #
或 @
,则将读取每一行。文中内容贴在最后
这个有效:
if line[0] != '#' and line[0] != '@':
value=float(line[3:8])
但这不起作用:
if line[0] != ('#' and '@'):
value=float(line[3:8])
ValueError: could not convert string to float: 'his f'
这也不行:
if line[0] != ('#' or '@'):
value=float(line[3:8])
ValueError: could not convert string to float: ' tit'
那么为什么 or
和 and
都不起作用?
文本文件内容:
# This file was created Wed Feb 21 12:32:25 2018
# Created by:
# :-) GROMACS - gmx do_dssp, VERSION 5.1.1 (-:
#
# Executable: /shared/ucl/apps/gromacs/5.1.1/intel-2015-update2/bin//gmx
# Data prefix: /shared/ucl/apps/gromacs/5.1.1/intel-2015-update2
# Command line:
# gmx do_dssp -f md_0_1_noPBC.xtc -s md_0_1.tpr -ssdump ssdump.dat -map ss.map -o ss.xpm -sc scount.xvg -a area.xpm -ta totarea.xvg -aa averarea.xvg -tu ns
# gmx do_dssp is part of G R O M A C S:
#
# Gnomes, ROck Monsters And Chili Sauce
#
@ title "Secondary Structure"
@ xaxis label "Time (ns)"
@ yaxis label "Number of Residues"
@TYPE xy
@ subtitle "Structure = A-Helix + B-Sheet + B-Bridge + Turn"
@ view 0.15, 0.15, 0.75, 0.85
@ legend on
@ legend box on
@ legend loctype view
@ legend 0.78, 0.8
@ legend length 2
@ s0 legend "Structure"
@ s1 legend "Coil"
@ s2 legend "B-Sheet"
@ s3 legend "B-Bridge"
@ s4 legend "Bend"
@ s5 legend "Turn"
@ s6 legend "A-Helix"
@ s7 legend "3-Helix"
@ s8 legend "5-Helix"
0 278 106 199 10 44 60 9 15 0
0.1 267 118 203 7 52 47 10 6 0
0.2 245 144 175 8 54 51 11 0 0
0.3 264 118 192 8 58 56 8 3 0
line[0] not in ['#' , '@']
就可以了。
它检查给定数组中是否存在 line[0]
。
我正在使用 python 读取文本文件。如果第一个字符不等于 #
或 @
,则将读取每一行。文中内容贴在最后
这个有效:
if line[0] != '#' and line[0] != '@':
value=float(line[3:8])
但这不起作用:
if line[0] != ('#' and '@'):
value=float(line[3:8])
ValueError: could not convert string to float: 'his f'
这也不行:
if line[0] != ('#' or '@'):
value=float(line[3:8])
ValueError: could not convert string to float: ' tit'
那么为什么 or
和 and
都不起作用?
文本文件内容:
# This file was created Wed Feb 21 12:32:25 2018
# Created by:
# :-) GROMACS - gmx do_dssp, VERSION 5.1.1 (-:
#
# Executable: /shared/ucl/apps/gromacs/5.1.1/intel-2015-update2/bin//gmx
# Data prefix: /shared/ucl/apps/gromacs/5.1.1/intel-2015-update2
# Command line:
# gmx do_dssp -f md_0_1_noPBC.xtc -s md_0_1.tpr -ssdump ssdump.dat -map ss.map -o ss.xpm -sc scount.xvg -a area.xpm -ta totarea.xvg -aa averarea.xvg -tu ns
# gmx do_dssp is part of G R O M A C S:
#
# Gnomes, ROck Monsters And Chili Sauce
#
@ title "Secondary Structure"
@ xaxis label "Time (ns)"
@ yaxis label "Number of Residues"
@TYPE xy
@ subtitle "Structure = A-Helix + B-Sheet + B-Bridge + Turn"
@ view 0.15, 0.15, 0.75, 0.85
@ legend on
@ legend box on
@ legend loctype view
@ legend 0.78, 0.8
@ legend length 2
@ s0 legend "Structure"
@ s1 legend "Coil"
@ s2 legend "B-Sheet"
@ s3 legend "B-Bridge"
@ s4 legend "Bend"
@ s5 legend "Turn"
@ s6 legend "A-Helix"
@ s7 legend "3-Helix"
@ s8 legend "5-Helix"
0 278 106 199 10 44 60 9 15 0
0.1 267 118 203 7 52 47 10 6 0
0.2 245 144 175 8 54 51 11 0 0
0.3 264 118 192 8 58 56 8 3 0
line[0] not in ['#' , '@']
就可以了。
它检查给定数组中是否存在 line[0]
。