我刚刚切换到Pycharm,我很高兴它提供了所有警告和提示,以改进我的代码.除了这个我不明白的:
This inspection detects shadowing names defined in outer scopes.
我知道从外部作用域访问变量是不好的做法但是遮蔽外部作用域的问题是什么?
这是一个例子,Pycharm给了我警告信息:
data = [4, 5, 6]
def print_data(data): # <-- Warning: "Shadows 'data' from outer scope
print data
print_data(data)
Run Code Online (Sandbox Code Playgroud) 码:
import urllib2 as u
import os as o
inn = 'dword.txt'
w = open(inn)
z = w.readline()
b = w.readline()
c = w.readline()
x = w.readline()
m = w.readline()
def Dict(Let, Mod):
global str
inn = 'dword.txt'
den = 'definitions.txt'
print 'reading definitions...'
dell =open(den, 'w')
print 'getting source code...'
f = u.urlopen('http://dictionary.reference.com/browse/' + Let)
a = f.read(800)
print 'writing source code to file...'
f = open("dic1.txt", "w")
f.write(a)
f.close()
j = open('defs.txt', 'w')
print 'finding definition is source …Run Code Online (Sandbox Code Playgroud) 我是 Python 和编程的新手,我很难记住这些东西。因为我开始读的书完全无聊,我开始玩弄一些想法。
这是我想要做的:打开文本文件,计算每个值的频率(只是一个系统名称列表),按频率对列表进行排序,然后返回结果。在网上搜索一些代码后,我在这里得到了这个:
file = open('C:\\Temp\\Test2.txt', 'r')
text = file.read()
file.close()
word_list = text.lower().split(None)
word_freq = {}
for word in word_list:
word_freq[word] = word_freq.get(word, 0) + 1
list = sorted(word_freq.keys())
for word in list:
print ("%-10s %d" % (word, word_freq[word]))
Run Code Online (Sandbox Code Playgroud)
它有效,但它按列表中的单词/系统名称排序:
pc05010 3
pc05012 1
pc05013 8
pc05014 2
Run Code Online (Sandbox Code Playgroud)
我想要这样:
pc05013 8
pc05010 3
pc05014 2
pc05012 1
Run Code Online (Sandbox Code Playgroud)
现在我正在搜索按值排序功能数小时。我打赌它很容易,但我什么也没找到。
对于我初学者的观点,它与这条线有关:
list = sorted(word_freq.keys())
Run Code Online (Sandbox Code Playgroud)
我想也许是:
list = sorted(word_freq.values())
Run Code Online (Sandbox Code Playgroud)
但不.... 看到关于这种语言的大量信息让我感到非常沮丧,但无法让如此简单的事情发挥作用。
请帮忙 :)
多谢!