为什么以下在Python中出现意外行为?
>>> a = 256
>>> b = 256
>>> a is b
True # This is an expected result
>>> a = 257
>>> b = 257
>>> a is b
False # What happened here? Why is this False?
>>> 257 is 257
True # Yet the literal numbers compare properly
Run Code Online (Sandbox Code Playgroud)
我使用的是Python 2.5.2.尝试一些不同版本的Python,似乎Python 2.3.3显示了99到100之间的上述行为.
基于以上所述,我可以假设Python在内部实现,使得"小"整数以不同于大整数的方式存储,is运算符可以区分.为什么泄漏抽象?当我不知道它们是否是数字时,比较两个任意对象以查看它们是否相同的更好的方法是什么?
我有枚举并使用变量,如myEnum.SomeNameA,myEnum.SomeNameB等.当我从函数返回其中一个变量时,我可以打印它们的名称(例如myEnum.SomeNameA)而不是它们返回的值吗?
如何获取DataFrame的名称并将其作为字符串打印?
例:
boston (分配给csv文件的var名称)
boston = read_csv('boston.csv')
print ('The winner is team A based on the %s table.) % boston
Run Code Online (Sandbox Code Playgroud) 在调试时,我们经常会看到如下的print语句:
print x # easy to type, but no context
print 'x=',x # more context, harder to type
12
x= 12
Run Code Online (Sandbox Code Playgroud)
如何编写一个函数来获取变量或变量的名称并打印其名称和值?我只对调试输出感兴趣,这不会被合并到生产代码中.
debugPrint(x) # or
debugPrint('x')
x=12
Run Code Online (Sandbox Code Playgroud) 有没有办法在运行时知道变量的名称(来自代码)?或编译时遗忘的var名称(字节代码与否)?
例如
>>> vari = 15 >>> print vari.~~name~~() 'vari'
注意:我说的是普通的数据类型变量(int,str,list ...)
假设我在一个小应用程序中有以下字典.
dict = {'one': 1, 'two': 2}
Run Code Online (Sandbox Code Playgroud)
如果我想将具有dict名称和所有内容的确切代码行写入文件,该怎么办?python中有一个函数可以让我这样做吗?或者我是否必须先将其转换为字符串?转换它不是问题,但也许有一种更简单的方法.
我不需要将它转换为字符串的方法,我可以做.但如果有一个内置函数可以为我做这个,我想知道.
为了说清楚,我想写的文件是:
write_to_file("dict = {'one': 1, 'two': 2}")
Run Code Online (Sandbox Code Playgroud) 我有一些代码可以执行大量的字符串格式化.通常,我最终得到的代码如下:
"...".format(x=x, y=y, z=z, foo=foo, ...)
Run Code Online (Sandbox Code Playgroud)
我试图将大量变量插入到一个大字符串中.
是否有充分的理由不编写这样的函数来使用inspect模块来查找要插入的变量?
import inspect
def interpolate(s):
return s.format(**inspect.currentframe().f_back.f_locals)
def generateTheString(x):
y = foo(x)
z = x + y
# more calculations go here
return interpolate("{x}, {y}, {z}")
Run Code Online (Sandbox Code Playgroud) 我正在尝试跟踪Python(2.7)中的内存泄漏.我找到了gc.get_referrers,但是不明白输出.删除后dying_node(除了我作为狩猎的一部分创建的列表之外,应删除所有引用),我的代码中有:
gc.collect()
print "done dying: ", getrefcount(dying_node) #note, includes the reference from getrefcount
referrers = gc.get_referrers(dying_node)
print "referrers: "
for referrer in referrers:
print referrer
Run Code Online (Sandbox Code Playgroud)
产生输出:
> done dying: 4
> referrers:
> [<__main__.Node instance at 0x104e53cb0>, <__main__.Node instance at 0x104e53b90>, <__main__.Node instance at 0x104e53b00>, <__main__.Node instance at 0x104e53d40>, <__main__.Node instance at 0x104e53ab8>, <__main__.Node instance at 0x104e53bd8>, <__main__.Node instance at 0x104e53a70>, <__main__.Node instance at 0x104e53c20>, <__main__.Node instance at 0x104e53c68>, <__main__.Node instance at 0x104e53b48>]
> [<__main__.Node instance at 0x104e53c20>, …Run Code Online (Sandbox Code Playgroud) 我想知道如何找到字典元素的变量名称:
例如:
>>>dict1={}
>>>dict2={}
>>>dict1['0001']='0002'
>>>dict2['nth_dict_item']=dict1
>>>print dict2
{'nth_dict_item': {'0001': '0002'}}
>>>print dict2['nth_dict_item']
{'001': '002'}
Run Code Online (Sandbox Code Playgroud)
我怎样才能告诉我dict2 ['nth_dict_item']是或正在引用"dict1"?我想要它引用的数据结构的名称而不是数据本身.
如果我将id(dict1)的输出与id(dict2 ['nth_dict_item'])进行比较,我发现它们是相同的.
但是,如何将该ID转换为变量名?是否有更直接/更清晰的方法来获取我想知道的信息?
我敢肯定我只是忽略了一个能让我的生活轻松的功能,但我对Python很陌生:)
任何帮助表示赞赏,谢谢!
更新:这就是为什么我希望这个工作:
我正在尝试制作一个使用类似数据库的字典的应用程序.我希望这个伪代码的功能起作用:
dict_1={}
dict_2={}
dict_3={}
dict_1["FooBar1.avi"]=[movie_duration,movie_type,comments]
dict_2["FooBar2.avi"]=[movie_duration,movie_type,comments]
dict_3["FooBar3.avi"]=[movie_duration,movie_type,comments]
dict_database[SomeUniqueIdentifier1]=dict_1
dict_database[SomeUniqueIdentifier2]=dict_2
dict_database[SomeUniqueIdentifier3]=dict_3
Run Code Online (Sandbox Code Playgroud)
SomeUniqueIdentifier#将是一个唯一值,我将其用作数据库键/ unqiueID来查找条目.
我希望能够通过以下方式更新FooBar1.avi的"评论"字段:
WhichDict= dict_database[SomeUniqueIdentifier1]
WhichDict[WhichDict.keys()[0]][2]='newcomment'
Run Code Online (Sandbox Code Playgroud)
而不是必须做:
dict_database['SomeUniqueIdentifier1'][dict_database['SomeUniqueIdentifier1'].keys()[0]][2]='newcomment'
Run Code Online (Sandbox Code Playgroud)
感谢大家.我现在明白我误解了很多基础知识(全脑屁).将返回并修复设计.谢谢大家!
我有几个值,像这样:
value_a = 5
value_b = 10
value_c = 20
Run Code Online (Sandbox Code Playgroud)
我想找到最大的值并打印值的NAME.通常我会用
val = [value_a, value_b, value_c]
print max (val)
Run Code Online (Sandbox Code Playgroud)
但这只给了我价值,而不是名字.
我制作了四个都有一些值的列表,我想对计算机说,以打印出长度最大的列表的名称。
我尝试了这段代码
list1=[values,values,values]
list2=[values,values,values]
list3=[values,values,values]
list4=[values,values,values]
if len(list1)>len(list2) and len(list1)>len(list3) and len(list1)>len(list4):
print(True)
Run Code Online (Sandbox Code Playgroud)
但是这需要花费我的全部时间,因此我需要将list2与他人进行比较,并将list3和list4进行比较,所以有一种方法可以像我这样:
if len(list1)>(len(list2) and len(list3) and len(list4)):
print(True)
Run Code Online (Sandbox Code Playgroud)
或此打印(长度最大的列表)
import random
Diamonds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King"]
Hearts = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King"]
Clubs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King"]
Spades = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King"]
suitvalues = [Diamonds, Hearts, Clubs, Spades]
potentialsuit = random.choice(suitvalues)
potentialcard = random.choice(potentialsuit)
print(potentialcard ,"of" ,potentialsuit.title)
Run Code Online (Sandbox Code Playgroud)
我的问题是,potentialsuit.title部分会打印整个列表,而我只想打印列表名称。我知道我写的那部分不会解决问题,但是那只是一个替代。
python ×12
dictionary ×2
list ×2
python-3.x ×2
variables ×2
arrays ×1
dataframe ×1
debugging ×1
identity ×1
int ×1
lookup ×1
memory-leaks ×1
operators ×1
pandas ×1
python-2.7 ×1