我正在处理HP Code Wars 2012中的字母分配问题。我不断收到一条错误消息,指出标识符中的无效字符。这是什么意思,以及如何解决。这是带有信息的页面。 hpcodewars.org/past/cw15/problems/2012ProblemsFinalForPrinting.pdf
这是代码
import string
def text_analyzer(text):
'''The text to be parsed and
the number of occurrences of the letters given back
be. Punctuation marks, and I ignore the EOF
simple. The function is thus very limited.
'''
result = {}
# Processing
for a in string.ascii_lowercase:
result [a] = text.lower (). count (a)
return result
def analysis_result (results):
# I look at the data
keys = analysis.keys ()
values \u200b\u200b= list(analysis.values \u200b\u200b())
values.sort (reverse = True )
# I turn to the dictionary and
# Must avoid that letters will be overwritten
w2 = {}
list = []
for key in keys:
item = w2.get (results [key], 0 )
if item = = 0 :
w2 [analysis results [key]] = [key]
else :
item.append (key)
w2 [analysis results [key]] = item
# We get the keys
keys = list (w2.keys ())
keys.sort (reverse = True )
for key in keys:
list = w2 [key]
liste.sort ()
for a in list:
print (a.upper (), "*" * key)
text = """I have a dream that one day this nation will rise up and live out the true
meaning of its creed: "We hold these truths to be self-evident, that all men
are created equal. "I have a dream that my four little children will one day
live in a nation where they will not be Judged by the color of their skin but
by the content of their character.
# # # """
analysis result = text_analyzer (text)
analysis_results (results)
参考方案
错误SyntaxError: invalid character in identifier
表示您在变量名称,函数等中间有一些字符,该字符不是字母,数字或下划线。实际的错误消息如下所示:
File "invalchar.py", line 23
values = list(analysis.values ())
^
SyntaxError: invalid character in identifier
这可以告诉您实际的问题是什么,因此您不必猜测“我的无效字符在哪里”?好吧,如果您看那条线,那儿会有一堆非打印垃圾字符。把它们拿出来,你就会过去的。
如果您想知道实际的垃圾字符是什么,我从您的代码中复制了有问题的行,并将其粘贴到Python解释器中的字符串中:
>>> s=' values = list(analysis.values ())'
>>> s
' values \u200b\u200b= list(analysis.values \u200b\u200b())'
因此,这就是\u200b
或ZERO WIDTH SPACE。这就解释了为什么您无法在页面上看到它。最常见的原因是,您已经从StackOverflow或Wiki之类的网站上复制了某些格式化的(非纯文本)代码,或者从PDF文件中复制了这些代码。
如果您的编辑器没有给您找到和修复这些字符的方法,只需删除并重新输入该行即可。
当然,您还可以通过不缩进来获得至少两个IndentationError
,从停留空间中获得至少一个SyntaxError
(例如= =
而不是==
),或者将下划线变成空格(例如analysis results
而不是analysis_results
)。
问题是,您如何使代码进入这种状态?如果您使用Microsoft Word之类的代码作为代码编辑器,那就是您的问题。使用文本编辑器。如果不是,那么,根本原因是什么导致您最终得到了这些垃圾字符,缩进的缩进和多余的空格,请在尝试修复代码之前对其进行修复。
Python uuid4,如何限制唯一字符的长度 - python在Python中,我正在使用uuid4()方法创建唯一的字符集。但是我找不到将其限制为10或8个字符的方法。有什么办法吗?uuid4()ffc69c1b-9d87-4c19-8dac-c09ca857e3fc谢谢。 参考方案 尝试:x = uuid4() str(x)[:8] 输出:"ffc69c1b" Is there a way to…
Python GPU资源利用 - python我有一个Python脚本在某些深度学习模型上运行推理。有什么办法可以找出GPU资源的利用率水平?例如,使用着色器,float16乘法器等。我似乎在网上找不到太多有关这些GPU资源的文档。谢谢! 参考方案 您可以尝试在像Renderdoc这样的GPU分析器中运行pyxthon应用程序。它将分析您的跑步情况。您将能够获得有关已使用资源,已用缓冲区,不同渲染状态上…
Python-如何检查Redis服务器是否可用 - python我正在开发用于访问Redis Server的Python服务(类)。我想知道如何检查Redis Server是否正在运行。而且如果某种原因我无法连接到它。这是我的代码的一部分import redis rs = redis.Redis("localhost") print rs 它打印以下内容<redis.client.Redis o…
Python:图像处理可产生皱纹纸效果 - python也许很难描述我的问题。我正在寻找Python中的算法,以在带有某些文本的白色图像上创建皱纹纸效果。我的第一个尝试是在带有文字的图像上添加一些真实的皱纹纸图像(具有透明度)。看起来不错,但副作用是文本没有真正起皱。所以我正在寻找更好的解决方案,有什么想法吗?谢谢 参考方案 除了使用透明性之外,假设您有两张相同尺寸的图像,一张在皱纹纸上明亮,一张在白色背景上有深…
Python-crontab模块 - python我正在尝试在Linux OS(CentOS 7)上使用Python-crontab模块我的配置文件如下:{ "ossConfigurationData": { "work1": [ { "cronInterval": "0 0 0 1 1 ?", "attribute&…