“#hash”对IDLE有什么意义吗? [英] Does '#hash' mean anything in IDLE?

查看:63
本文介绍了“#hash”对IDLE有什么意义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,

我目前正试图通过优秀的学习Python来学习Python。学习Python书。我写了我的第一个非平凡的程序,

从几个注释行开始。其中一条注释行以

''#hash''开头。 IDLE并没有将它作为注释行着色,而是用紫色代替'b b'这个单词''hash'',就好像它是一个关键词。奇怪的。行为

似乎很容易触发:只需在IDLE中打开一个新窗口并输入

这两行:


#这是一个测试

#hash应该仍然是一个评论行


然后,保存后,第二行没有着色为评论行

虽然第一个是。

这是一个错误,还是以#hash开头的注释行有一些

的特殊含义?

我的程序运行正常,所以似乎解释器本身是

忽略了这条线。


-John Coleman

解决方案



John Coleman写道:

问候,
我目前正在努力学习Python通过优秀的学习Python书。我写了我的第一个非平凡的程序,它以几条注释行开头。其中一条注释行以
''#hash''开头。 IDLE并没有将它作为注释行着色,而是将紫色这个词用'hash'作为关键词。奇怪的。行为似乎很容易触发:只需在IDLE中打开一个新窗口并输入
这两行:

#这是一个测试
#hash应该还是是一个评论专栏

然后,保存后,第二行没有作为评论行着色
虽然第一行是。
这是一个错误,还是评论行从#hash开始有一些特殊意义吗?
我的程序运行正常,所以似乎解释器本身无视该行。

-John Coleman




这不仅仅是#hash,还有像#dict,#n,#在

评论开头的评论这条线击败了IDLE的着色算法。

有趣的是,#while或#for之类的行为符合预期,所以它好像b $ b似乎是内置的而不是关键词是问题。要回答我自己的问题 - 这显然是一个(无害的)错误。


-John Coleman


John Coleman写道:

John Coleman写道:

问候,
我目前正试图通过优秀的学习来学习Python />学习Python预订。



我也是!

这不仅仅是#hash,还有像#dict,#uint,#ele这样的东西/>开始一条评论线,它会击败IDLE的着色算法。
有趣的是,#while或#for之类的行为符合预期,所以它似乎是内置的而不是关键字问题。回答我自己的问题 - 这显然是一个(无害的)bug。




也注意到在#之后放一个空格会阻止问题




John Salerno写道:

John Coleman写道:

John Coleman写道:

问候,
我目前正试图通过优秀的学习Python来学习Python。我也是。



我也是这样!

这不仅仅是#hash,还有像#dict这样的东西,# int,#len在评论行的开头,它击败了IDLE的着色算法。
有趣的是,#while或#for之类的行为符合预期,所以它似乎是建立的-ins而不是问题的关键字。回答我自己的问题 - 这显然是一个(无害的)bug。



也注意到在#停止问题后放置一个空格




到目前为止你觉得Python怎么样?我最喜欢字典对象所以

远。我是从VBScript来到Python的,所以我已经知道了这些东西的价值,但在Python中它们得到了更好的支持。


这是我正在谈论的程序,*真的*显示了功能

的词典:


************* ************************************* ************* ************************** *


#Python程序用大多数单字字谜发现单词


#以下哈希函数有属性

#这些字是彼此的字谜

#hash到同一个串。它假定输入

#是范围''a''到''z'的小写


def letter_hash(word):

代码= 26 * [0]

代表c的单词:

代码[ord(c)-97] + = 1

return_string =''''

for i in range(26):

j = codes [i]

如果j> 0:

return_string + =(str(j)+ chr(i + 97))

返回return_string


#main程序:


哈希= {}


#first加载哈希词典


for line在open(''C:\\yawl.txt'')。readlines():

word = line.strip()。lower()#to is safe

my_hash = letter_hash(单词)

hashes.setdefault(my_hash,[])。append(word)


#now找到包含大多数字谜的单词


max_len = 0

max_words = []

for the empty_list in hashes.itervalues():

if len(word_list)> max_len:

max_len = len(word_list)

max_words = word_list

print max_words

***** ********************************************* ***** ***


" yawl"代表又一个单词列表。这是一些文本列表,其中包括一些英语单词,包括各种古老和技术性的b $ b短语。 Google foryawl word list如果你想追查一份副本。

输出是


[''apers'',''apres'',''asper'' ,''削减'',''解析'',''梨'',''prase'',

''presa'',''强奸'',''收获'' ,''spaer'',''spare'',''spear'']


这13个单词是彼此的字谜。它们包含一些漂亮的

晦涩难懂的单词:asper是一个17世纪的土耳其硬币,而spaer是一个古老的苏格兰方言单词,供先知使用(你可以看到发言者)。

如果你眯眼)。


-John Coleman


Greetings,
I am currently trying to learn Python through the excellent
"Learning Python" book. I wrote my first non-trivial program, which
began with several comment lines. One of the comment lines began with
''#hash''. IDLE doesn''t colorize it as a comment line but instead colors
the word ''hash'' in purple as if it were a key word. Wierd. The behavior
seems easy to trigger: Just open up a new window in IDLE and enter
these two lines:

#This is a test
#hash should still be a comment line

Then, after saving, the second line is not colored as a comment line
though the first is.
Is this a bug, or do comment lines which begin with #hash have some
special meaning?
My program ran fine, so it seems that the interpreter itself is
ignoring the line.

-John Coleman

解决方案


John Coleman wrote:

Greetings,
I am currently trying to learn Python through the excellent
"Learning Python" book. I wrote my first non-trivial program, which
began with several comment lines. One of the comment lines began with
''#hash''. IDLE doesn''t colorize it as a comment line but instead colors
the word ''hash'' in purple as if it were a key word. Wierd. The behavior
seems easy to trigger: Just open up a new window in IDLE and enter
these two lines:

#This is a test
#hash should still be a comment line

Then, after saving, the second line is not colored as a comment line
though the first is.
Is this a bug, or do comment lines which begin with #hash have some
special meaning?
My program ran fine, so it seems that the interpreter itself is
ignoring the line.

-John Coleman



It isn''t just #hash, but also things like #dict, #int, #len at the
start of a comment line which defeats IDLE''s colorization algorithm.
Interestingly, things like #while or #for behave as expected so it
seems to be built-ins rather than keywords which are the problem. To
answer my own question - this is pretty clearly a (harmless) bug.

-John Coleman


John Coleman wrote:

John Coleman wrote:

Greetings,
I am currently trying to learn Python through the excellent
"Learning Python" book.


me too!
It isn''t just #hash, but also things like #dict, #int, #len at the
start of a comment line which defeats IDLE''s colorization algorithm.
Interestingly, things like #while or #for behave as expected so it
seems to be built-ins rather than keywords which are the problem. To
answer my own question - this is pretty clearly a (harmless) bug.



also notice that putting a space after # stops the problem



John Salerno wrote:

John Coleman wrote:

John Coleman wrote:

Greetings,
I am currently trying to learn Python through the excellent
"Learning Python" book.



me too!

It isn''t just #hash, but also things like #dict, #int, #len at the
start of a comment line which defeats IDLE''s colorization algorithm.
Interestingly, things like #while or #for behave as expected so it
seems to be built-ins rather than keywords which are the problem. To
answer my own question - this is pretty clearly a (harmless) bug.



also notice that putting a space after # stops the problem



How do you like Python so far? I like dictionary objects the best so
far. I''m coming to Python from VBScript, so I already knew the value of
such things, but in Python they are better supported.

Here is the program I was talking about, which *really* shows the power
of dictionaries:

************************************************** ***************************************

#Python program to discover word with most 1-word anagrams

#The following hash function has the property
#that words which are anagrams of each other
#hash to the same string. It assumes that input
#is lower case in range ''a'' to ''z''

def letter_hash(word):
codes = 26 * [0]
for c in word:
codes[ord(c)-97] +=1
return_string = ''''
for i in range(26):
j = codes[i]
if j > 0:
return_string += (str(j)+chr(i+97))
return return_string

#main program:

hashes = {}

#first load dictionary of hashes

for line in open(''C:\\yawl.txt'').readlines():
word = line.strip().lower() #to be safe
my_hash = letter_hash(word)
hashes.setdefault(my_hash,[]).append(word)

#now find word with most anagrams

max_len = 0
max_words = []
for word_list in hashes.itervalues():
if len(word_list) > max_len:
max_len = len(word_list)
max_words = word_list
print max_words
************************************************** ********

"yawl" stands for "Yet Another Word List". It is a text-list of some
240,000 English words, including all sorts of archaic and technical
phrases. Google for "yawl word list" if you want to track down a copy.
The output is

[''apers'', ''apres'', ''asper'', ''pares'', ''parse'', ''pears'', ''prase'',
''presa'', ''rapes'', ''reaps'', ''spaer'', ''spare'', ''spear'']

These 13 words are anagrams of each other. They contain some pretty
obscure words: asper is a 17th century Turkish coin and spaer is an
archaic Scottish-dialect word word for prophet (you can see "speaker"
if you squint).

-John Coleman


这篇关于“#hash”对IDLE有什么意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆