我在阅读参考书中的代码时遇到了涉及感叹号和整数的问题.
让我们说我已经声明了一个名为number的整数变量 - int number = 0;
然后我使用涉及感叹号的while函数 number
while(!number)
{
...
}
Run Code Online (Sandbox Code Playgroud)
我对此感到困惑,因为我不知道什么!number意思,什么可能返回结果?我不确定这是否可以使用,但正如我所说,我在书中看到了它.
因此,如果有人能告诉我它的!number意义是什么以及它的评价是什么呢?
先感谢您.
我只需要过滤掉只包含数字和/或一组标点符号的字符串.
我已经尝试检查每个字符,然后总结布尔条件以检查它是否等于len(str).是否有更多pythonic方式来做到这一点:
>>> import string
>>> x = ['12,523', '3.46', "this is not", "foo bar 42", "23fa"]
>>> [i for i in x if [True if j.isdigit() else False for j in i] ]
['12,523', '3.46', 'this is not', 'foo bar 42']
>>> [i for i in x if sum([True if j.isdigit() or j in string.punctuation else False for j in i]) == len(i)]
['12,523', '3.46']
Run Code Online (Sandbox Code Playgroud) 我知道如何删除字符串中的所有标点符号.
import string
s = '.$ABC-799-99,#'
table = string.maketrans("","") # to remove punctuation
new_s = s.translate(table, string.punctuation)
print(new_s)
# Output
ABC79999
Run Code Online (Sandbox Code Playgroud)
如何在Python中删除所有前导和尾随标点符号?期望的结果'.$ABC-799-99,#'是'ABC-799-99'.
因此,我尝试join()在将字符串拆分为单词和标点符号后使用,但它将字符串与单词和标点符号之间的空格连接起来。
b = ['Hello', ',', 'who', 'are', 'you', '?']
c = " ".join(b)
但这会返回:
c = 'Hello , who are you ?'
而且我要:
c = 'Hello, who are you?'
我发现了几个这方面的主题,我找到了这个解决方案:
sentence=re.sub(ur"[^\P{P}'|-]+",'',sentence)
Run Code Online (Sandbox Code Playgroud)
这应该删除除了'之外的每个标点符号,问题是它还会删除句子中的所有其他标点符号.
例:
>>> sentence="warhol's art used many types of media, including hand drawing, painting, printmaking, photography, silk screening, sculpture, film, and music."
>>> sentence=re.sub(ur"[^\P{P}']+",'',sentence)
>>> print sentence
'
Run Code Online (Sandbox Code Playgroud)
当然我想要的是保持句子没有标点符号,"warhol"保持原样
期望的输出:
"warhol's art used many types of media including hand drawing painting printmaking photography silk screening sculpture film and music"
"austro-hungarian empire"
Run Code Online (Sandbox Code Playgroud)
编辑:我也试过用
tbl = dict.fromkeys(i for i in xrange(sys.maxunicode)
if unicodedata.category(unichr(i)).startswith('P'))
sentence = sentence.translate(tbl)
Run Code Online (Sandbox Code Playgroud)
但这会删除每个标点符号
在编写数学时,似乎通常的做法是在显示的公式中添加标点符号.
是否有任何技巧可以避免在标准公式中加入标点符号?
我想避免
Consider the function
\[ \sin(x).\]
Run Code Online (Sandbox Code Playgroud)
我宁愿有类似的东西:
Consider the function
\[ \sin(x)\].
Run Code Online (Sandbox Code Playgroud)
但当然,完整的停止会显示在公式下方.
是否有一种聪明的方法来分离LaTeX中的公式和标点符号?
这个建议的原因是什么?为什么不与其他使用下划线的编程语言保持一致?
我对Python很新,所以我正在尝试一些简单的代码.但是,在其中一种做法中,我的代码应该在左边显示一些以英寸为单位的数字,在右边显示数字的转换;
count = 1
conv = count * 2.54
print count, conv
Run Code Online (Sandbox Code Playgroud)
我希望打印输出之间有一些空间;
count = 1
conv = count * 2.54
print count, conv
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何做到这一点.我到处搜索,但我能找到的只是人们试图摆脱空间.如果有人能带领我朝着正确的方向前进,我会感激不尽.
哦,我只是意识到,我使用Python 2.7,而不是3.x中.不确定这是否重要.
我无法找到哪些字符复合了"%p"Lua中设置的标点符号的文档.
我试图将utf-8字符串解析成"一口大小"的段.例如,我想将文本分解为"句子".
是否有一个全面的字符集(或正则表达式)对应于所有语言的句子结尾?我正在寻找可以捕捉拉丁时期,感叹号和审讯标记,中国和日本句号等的东西.
像上面这样的东西,但相当于一个逗号也会很棒.
unicode parsing character-encoding punctuation string-parsing