我认为classPython 中的语句创建了一个范围.但是尝试这个简单的代码我得到一个错误:
class A():
foo = 1
def __init__(self):
print foo
a = A()
NameError: global name 'foo' is not defined
Run Code Online (Sandbox Code Playgroud)
为什么foo无法从初始化程序访问?访问它的正确方法是什么?
我正在学习Python,我需要编写一个程序来确定一首诗中出现次数最多的单词.困扰我的问题是将一首诗的一行解析成包含该诗的单词的单一列表.当我解决它时,我将毫不费力地确定出现次数最多的单词.
我可以通过重复调用input()来访问诗的行,最后一行包含三个字符###.
所以,我写道:
while True:
y = input()
if y == "###":
break
y = y.lower()
y = y.split()
Run Code Online (Sandbox Code Playgroud)
并输入:
Here is a line like sparkling wine
Line up now behind the cow
###
Run Code Online (Sandbox Code Playgroud)
得到的结果:
['here', 'is', 'a', 'line', 'like', 'sparkling', 'wine']
['line', 'up', 'now', 'behind', 'the', 'cow']
Run Code Online (Sandbox Code Playgroud)
如果我试着打电话给y [0],我得到:
here
line
Run Code Online (Sandbox Code Playgroud)
如何在同一个变量中连接两个列表,或者如何将每一行分配给不同的变量?
任何提示都表示赞赏.谢谢.
例:
a = [1,2,3,2,4,2,5]
for key in a:
if key == 2:
key = 10
print key
Run Code Online (Sandbox Code Playgroud)
结果: [1,10,3,10,4,10,5]
我需要Python手动"询问"要替换哪些元素.
我需要从列表中提取一些元素(在本例中为n)我还有另一个列表(列表)
n=['A','B','C','D','E']
o=['A','B','C','D','E','AS','sd','Z','R']
n.sort()
Run Code Online (Sandbox Code Playgroud)
现在,如果我使用此代码:
for o in sorted(n):
Run Code Online (Sandbox Code Playgroud)
代码运行'A','B','C','D','E'.
我需要运行for代码但只运行'AS','sd','Z','R'项目.
类似于:运行o列表中的项目的代码,而不包含n列表中的项目
我想做这样的事情,是否有可能在python中.
f = 'free'
p = 'paid'
if version is f:
APP_VER = 'APP_FREE'
elif version is p:
APP_VER = 'APP_PAID'
s_email = webapp2.get_app().config[APP_VER]['SUPPORT_EMAIL']
Error: UnboundLocalError: local variable 'APP_VER' referenced before assignment
Run Code Online (Sandbox Code Playgroud)
任何的想法!
我以为我发现要通过清除它来排序字典,然后按照我想要的顺序重新组装它,但由于某种原因它按照它开始的方式重新排序.
这是代码,如果有人可以帮助我
from operator import itemgetter
n = {}
d = {
'a': ['2', 'ova', 'no'],
'b': ['23', 'movie', 'yes'],
'c': ['5', 'show', 'yes'],
'd': ['17', 'ova', 'yes'],
'e': ['1', 'movie', 'no']
}
for i in d:
print i, d[i]
print '\n'
l = d.items()
l.sort(key=itemgetter(1)) #l is now sorted by the value of the string holding the integers
d.clear()
for i in l:
print i[0], i[1]
d[i[0]] = i[1]
print '\n'
for i in d:
print i, d[i] #Why …Run Code Online (Sandbox Code Playgroud) 所以我有一个列表,其中包含一组字母值,即字典中的名称,例如:
['H', 'I', 'B']
Run Code Online (Sandbox Code Playgroud)
单个变量名称是我在字典中使用的键,但我的问题是,HIB列表一起表示另一个变量,我希望在字典中将其定义为HIB的关键字.所以我的问题是,如何将此列表转换为变量,最好是:
nodeName = 'HIB'
Run Code Online (Sandbox Code Playgroud)
所以我可以使用以下方法在字典中为其分配一个键:
Tree[nodeName] = ...
Run Code Online (Sandbox Code Playgroud)
因为obv.你不能在词典中调用列表.
谢谢!
我怎么能理解字典中没有键和值?如果可能的话,我将使用好像新的值不在字典中添加它.例如
d = {1:"k", 2:"l"}
Run Code Online (Sandbox Code Playgroud)
如果3不在列表中,则代码应该理解并将其作为具有空值的字典的新项
d = {1:"k", 2:"l", 3:"null"}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用主定理及其重现概念来解决递归关系以找出算法的复杂性,我如何证明:
T(n) = T(n/2)+O(1)
是
T(n) = O(log(n)) ?
任何解释都会得到赞赏!
我正在创建一个字符串的所有版本的列表,可以通过使用理解只删除一个字符来实现.我可以删除每个字符但不能保留其他字符.
wrd = 'superstar'
list2 = [(wrd[:1-1] + wrd[:i+1]) for i in range(len(wrd))]
print(list2)
Run Code Online (Sandbox Code Playgroud) python ×9
list ×4
dictionary ×3
algorithm ×1
big-o ×1
parsing ×1
python-2.7 ×1
python-2.x ×1
python-3.x ×1
recurrence ×1
scope ×1
slice ×1
sorting ×1
string ×1
variables ×1