我有一个Python程序,其中两个变量设置为该值'public'.在条件表达式中,我的比较var1 is var2失败了,但是如果我将其更改为var1 == var2它则返回True.
现在,如果我打开我的Python解释器并进行相同的"是"比较,它就会成功.
>>> s1 = 'public'
>>> s2 = 'public'
>>> s2 is s1
True
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?
如何在python中表示无限数?无论您在程序中输入哪个数字,任何数字都不应大于此无穷大的表示.
在python中使用链表最简单的方法是什么?在方案中,链接列表简单地定义'(1 2 3 4 5).事实上,Python的列表[1, 2, 3, 4, 5]和元组(1, 2, 3, 4, 5)并不是链表,链表有一些很好的属性,例如常量时间连接,并且能够引用它们的不同部分.让它们一成不变,它们真的很容易合作!
我在Python解释器中运行以下代码:
>>> foo = 10
>>> dir(foo) == dir(10)
True
>>> dir(foo) is dir(10)
False
>>>
Run Code Online (Sandbox Code Playgroud)
为什么是这样?
... is可用于字符串中相等的关键字.
>>> s = 'str'
>>> s is 'str'
True
>>> s is 'st'
False
Run Code Online (Sandbox Code Playgroud)
我试过了两个__is__(),__eq__()但他们没有工作.
>>> class MyString:
... def __init__(self):
... self.s = 'string'
... def __is__(self, s):
... return self.s == s
...
>>>
>>>
>>> m = MyString()
>>> m is 'ss'
False
>>> m is 'string' # <--- Expected to work
False
>>>
>>> class MyString:
... def __init__(self):
... self.s = 'string'
... def __eq__(self, s):
... return …Run Code Online (Sandbox Code Playgroud) 我的印象是,在NumPy中,两个阵列可以共享相同的内存.请看以下示例:
import numpy as np
a=np.arange(27)
b=a.reshape((3,3,3))
a[0]=5000
print (b[0,0,0]) #5000
#Some tests:
a.data is b.data #False
a.data == b.data #True
c=np.arange(27)
c[0]=5000
a.data == c.data #True ( Same data, not same memory storage ), False positive
Run Code Online (Sandbox Code Playgroud)
所以显然b没有复制a; 它只是创建了一些新的元数据并将其附加到a正在使用的相同内存缓冲区中.有没有办法检查两个数组是否引用相同的内存缓冲区?
我的第一印象是使用a.data is b.data,但返回false.我能做的a.data == b.data,返回真,但我不认为检查,以确保a和b共享相同的内存缓冲区,只有内存块被引用a和引用的一个b具有相同的字节.
>>> item = 2
>>> seq = [1,2,3]
>>> print (item in seq)
True
>>> print (item in seq is True)
False
Run Code Online (Sandbox Code Playgroud)
为什么第二个print语句输出False?
根据文件:
当前实现保持整数对象对之间的所有整数数组
-5和256,当您创建在该范围内的int你其实只是回到现有对象的引用.所以应该可以改变它的值1.我怀疑在这种情况下Python的行为是未定义的.:-)
所以以下行为是正常的.
>>> a = 256
>>> b = 256
>>> a is b
True
>>> c = 257
>>> d = 257
>>> c is d
False
Run Code Online (Sandbox Code Playgroud)
但是当我宣布这样的两个变量时,我变得正确 -
>>> e = 258; f=258;
>>> e is f
True
Run Code Online (Sandbox Code Playgroud)
我检查了e和f-引用的对象的身份
>>> id(e)
43054020
>>> id(f)
43054020
Run Code Online (Sandbox Code Playgroud)
他们是一样的.
我的问题是当我们用分号分隔e和f时发生了什么?为什么它们引用同一个对象(尽管这些值超出了Python的整数对象数组的范围)?
如果你能解释它,就像你向初学者解释一样,那就更好了.
可能重复:
Python中的字符串比较:is = = =
Python string interning 为什么使用'=='或'is'比较Python中的字符串
有时会产生不同的结果?
我偶然使用is和 ==字符串交替使用,但我发现并不总是一样的.
>>> Folder = "locales/"
>>> Folder2 = "locales/"
>>> Folder is Folder2
False
>>> Folder == Folder2
True
>>> File = "file"
>>> File2 = "file"
>>> File is File2
True
>>> File == File2
True
>>>
Run Code Online (Sandbox Code Playgroud)
为什么在一种情况下操作符可以互换而在另一种情况下不可以?
我知道这is用于比较两个对象是否相同但是==是否相等.根据我的经验is总是为数字工作,因为Python重用数字.例如:
>>>a = 3
>>>a is 3
True
Run Code Online (Sandbox Code Playgroud)
is每当我将某些东西与数字进行比较时,我习惯使用它.但是is下面这个程序不起作用:
from collections import namedtuple
# Code taken directly from [Udacity site][1].
# make a basic Link class
Link = namedtuple('Link', ['id', 'submitter_id', 'submitted_time', 'votes',
'title', 'url'])
# list of Links to work with
links = [
Link(0, 60398, 1334014208.0, 109,
"C overtakes Java as the No. 1 programming language in the TIOBE index.",
"http://pixelstech.net/article/index.php?id=1333969280"),
Link(1, 60254, 1333962645.0, 891,
"This explains why technical …Run Code Online (Sandbox Code Playgroud) python ×10
comparison ×2
equality ×2
python-2.7 ×2
boolean ×1
built-in ×1
equals ×1
identity ×1
infinite ×1
infinity ×1
integer ×1
linked-list ×1
numpy ×1
operators ×1
string ×1