相关疑难解决方法(0)

Python:为什么运算符"is"和"=="有时可以互换为字符串?

可能重复:
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)

为什么在一种情况下操作符可以互换而在另一种情况下不可以?

python operators python-2.7

5
推荐指数
1
解决办法
1242
查看次数

为什么这个字符串比较返回False?

可能重复:
Python中的字符串比较:is = ==

algorithm = str(sys.argv[1])
print(algorithm)
print(algorithm is "first")
Run Code Online (Sandbox Code Playgroud)

我正在使用参数从命令行运行它first,那么为什么代码输出:

first
False
Run Code Online (Sandbox Code Playgroud)

python string

4
推荐指数
2
解决办法
7019
查看次数

Python是vs ==

可能重复:
Python中的字符串比较:is = == 运算符
何时==不等于is运算符?(蟒蛇)

我仍然是Python的新手.我听到有人说使用is,不是==因为"这不是C".但我有一些代码x is 5,它没有按预期工作.

因此,遵循正确的Python/PEP样式,何时使用is以及何时使用==

python operators

3
推荐指数
1
解决办法
2285
查看次数

自然三元逻辑?

可能重复:
如何更简洁地找到缺失值?

是否有一种使用Python语言T在字母表中表达交换运算符的好方法a b c,其中

  • a T b == c
  • b T c == a
  • c T a == b

我最好的尝试是硬编码:

def T(first, second):
    if first is 'a' and second is 'b':
        return 'c'
    if first is 'a' and second is 'c':
        return 'c'
    if first is 'b' and second is 'c':
        return 'a'
    if first is 'b' and second is 'a':
        return 'c'
    if first is 'c' and second is 'a':
        return 'b'
    if first …
Run Code Online (Sandbox Code Playgroud)

python logic

3
推荐指数
1
解决办法
348
查看次数

关键字:is和=在python中有什么区别

根据python样式指南,python关键字is应该用于代替==运算符.

然而,它们并不总是与此处所示完全相同.为什么?实际差异是什么,适当的用法是什么?

import unittest

class testIS(unittest.TestCase):
    def test_is(self):
        self.assertEqual(1,1)

if __name__ == '__main__':
    unittest.main()
Run Code Online (Sandbox Code Playgroud)

哪个有效...但以下不...

import unittest

class testIS(unittest.TestCase):
    def test_is(self):
        self.assertEqual(1,1)

if __name__ is '__main__':
    unittest.main()
Run Code Online (Sandbox Code Playgroud)

python syntax keyword

3
推荐指数
2
解决办法
343
查看次数

Python if语句,只显示一个选项

我正在尝试使用这段代码来确定基于距离恒星距离的行星类型,并且唯一一个它将打印出来的信息是地球行星.

如果我在第三个if语句之前打印行星类型它将打印它选择的所有行星类型,但它似乎贯穿的唯一if语句是"如果planet_type是"terraform":

for distance in (a):
    while True:
        random_x = random.randint(-distance,distance)
        random_y = random.randint(-distance,distance)

        if(random_x <distance and random_x >-distance and random_y <distance and random_y >-distance):
            continue
        else:
            print "Creating planets"
            time.sleep(.5)
            print distance

       if star_size*100 >= distance:
           possible_planet_type = ('mineral' , 'gas', 'organic', 'terraform', 'ice')
           planet_type = random.choice(possible_planet_type)
       if planet_type is "mineral":
           planet_diameter = random.randint(3000,8000)
           iron = planet_diameter*random.randint(10,100)    
           carbon = planet_diameter*random.randint(5,50)
           oxygen = planet_diameter*random.randint(0,0)
           h2o = planet_diameter*random.randint(0,1)
           deuterium = planet_diameter*random.randint(0,5)
           helium_3 = planet_diameter*random.randint(0,2)
           print planet_type, planet_diameter, iron, carbon, oxygen, h2o, …
Run Code Online (Sandbox Code Playgroud)

python mongodb

1
推荐指数
1
解决办法
102
查看次数

为什么我的条件返回"无?"

每当我运行它时,我会得到第三个选项,因为它应该返回第一个,因为s ='yes'.这里出了什么问题?

def shut_down(s):
    if s is 'yes':
        return 'Shutting down...'
    elif s is 'no':
        return 'Shutdown aborted!'
    else:
        return "Sorry, I didn't understand you"

ans = 'Yes'
s = ans.lower()
shut_down(s)
Run Code Online (Sandbox Code Playgroud)

python

1
推荐指数
1
解决办法
117
查看次数

如何将变量与python中的字符串进行比较?

我是新手,所以我很抱歉,如果这不是提出问题的最佳方式......

这是代码 -

import sys

print("What is ur name?")
name = sys.stdin.readline()

answer = "jack"

if name is answer :
    print("ur awesome")
exit();
Run Code Online (Sandbox Code Playgroud)

现在,当我在cmd中运行它时,即使我输入 - 杰克,我也没有打印任何东西?先感谢您

python string

1
推荐指数
1
解决办法
5613
查看次数

在python中为什么列表与list [:]不一样?

我有一个列表,ls = [0 1 2 3 4]我正在运行以下内容:

print(ls is ls[:])
Run Code Online (Sandbox Code Playgroud)

我得到的输出为False.为什么他们不是同一个名单?当我打印两个版本时,我会打印相同的列表.

python list

1
推荐指数
1
解决办法
130
查看次数

为什么'list is list(list)'在python中是假的?

num = [1,2,3,4]
num == list(num) 
Run Code Online (Sandbox Code Playgroud)

它给出True,其中,

num is list(num)
Run Code Online (Sandbox Code Playgroud)

给出False

这两者有什么区别?python在这两个语句中做了什么?

python identity list python-3.x

1
推荐指数
1
解决办法
1408
查看次数

在python 2.7中,"x!= y"和"not x == y"之间的区别是什么?

这可能是非常基本但是:

作为XY同一个类的对象,调用not x == y会导致我的调试器停止类__eq__的方法,但调用x != y不会?

是什么!=检查?它等同于is not(参考检查)吗?

python python-2.7

0
推荐指数
2
解决办法
115
查看次数

为什么这个简单的断言在python中失败了?

所以我有一个页面,预计会返回内容或返回django中的403页面

response = self.client.get(...)
print response.status_code
print type(response.status_code)
assert response.status_code is 200
print "WHAT IS GOING ON!?!?!?!"
response = self.client.get(...)
code = response.status_code
print code
print type(code)
assert code is 403
print "hmm"
Run Code Online (Sandbox Code Playgroud)

返回输出:

200
<type 'int'>
WHAT IS GOING ON!?!?!?!
403
<type 'int'>
Run Code Online (Sandbox Code Playgroud)

显然代码失败assert code is 403但我无法想象为什么.我甚至通过更改线路assert 403 is 403和测试通过来检查自己.我是Python和Django的新手,所以我可能会遗漏一些明显的东西.

python django assertions

-1
推荐指数
1
解决办法
88
查看次数

实习python值

我正在尝试学习python中的作业我对此感到困惑.

>>> a=343434;b=343434
>>> a is b
True
>>> a=343434
>>> b=343434
>>> a is b
False
Run Code Online (Sandbox Code Playgroud)

python python-3.x

-3
推荐指数
1
解决办法
192
查看次数