相关疑难解决方法(0)

是否可以仅在没有在Python中赋值的情况下声明变量?

是否可以在Python中声明变量,如此?:

var
Run Code Online (Sandbox Code Playgroud)

所以它初始化为None?似乎Python允许这样做,但是一旦你访问它,它就会崩溃.这可能吗?如果没有,为什么?

编辑:我想这样做的情况:

value

for index in sequence:

   if value == None and conditionMet:
       value = index
       break
Run Code Online (Sandbox Code Playgroud)

重复

有关

python variable-assignment variable-declaration

260
推荐指数
4
解决办法
45万
查看次数

Python 2如何比较字符串和int?为什么列表比较大于数字,而元组大于列表?

以下代码段使用输出进行注释(如ideone.com上所示):

print "100" < "2"      # True
print "5" > "9"        # False

print "100" < 2        # False
print 100 < "2"        # True

print 5 > "9"          # False
print "5" > 9          # True

print [] > float('inf') # True
print () > []          # True
Run Code Online (Sandbox Code Playgroud)

有人可以解释为什么输出是这样的?


实施细节

  • 这种行为是由语言规范强制执行的,还是由实现者决定的?
  • 任何主要的Python实现之间是否存在差异?
  • Python语言版本之间是否存在差异?

python comparison types python-2.x

172
推荐指数
2
解决办法
8万
查看次数

Python:min(无,x)

我想执行以下操作:

a=max(a,3)
b=min(b,3)
Run Code Online (Sandbox Code Playgroud)

但有时ab可能None.
我很高兴地发现,如果max它很好地工作,给出我所需的结果3,但如果bNone,b仍然None...

任何人都可以想到一个优雅的小技巧min,如果其中一个参数在None中,则返回数字?

python python-2.x

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

在Python中,None的计算结果小于零?

在Python中,None评估小于零?

ActivePython 2.7.2.5 (ActiveState Software Inc.) based on
Python 2.7.2 (default, Jun 24 2011, 12:21:10) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> None < 0
True
>>> None == 0
False
>>> None > 0
False
>>>
Run Code Online (Sandbox Code Playgroud)

这是预期的吗?

我猜想这None将等于零(通过类型强制),或者所有这些语句都会返回False.

python python-2.x

30
推荐指数
2
解决办法
2万
查看次数

使用算术运算符将None与内置类型进行比较?

Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> None > 0
False
>>> None == 0
False
>>> None < 0
True
Run Code Online (Sandbox Code Playgroud)
  • None使用为内置类型定义的算术运算符(在本例中为整数)进行比较?
  • 是语言规范的前两个和第三个比较部分之间的区别(Python的规范 - 你一定是开玩笑:) :)或者它是CPython的实现细节?

python comparison cpython

27
推荐指数
2
解决办法
1万
查看次数

为什么None是python中最小的?

我从python中学到了什么:

None is frequently used to represent the absence of a value

当我放入一个列表并用数字和字符串排序.我得到了以下结果,这意味着它是最小的数字?

相反:

>>> sorted([1, 2, None, 4.5, (-sys.maxint - 1), (sys.maxint - 1), 'abc'], reverse=True)
['abc', 9223372036854775806, 4.5, 2, 1, -9223372036854775808, None]
>>>
Run Code Online (Sandbox Code Playgroud)

正常排序:

>>> sorted([1, 2, None, 4.5, (-sys.maxint - 1), (sys.maxint - 1), 'abc'])
[None, -9223372036854775808, 1, 2, 4.5, 9223372036854775806, 'abc']
>>>
Run Code Online (Sandbox Code Playgroud)

python排序函数如何使用None?

python python-internals

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

排序dicts列表时如何使用operator.itemgetter忽略None值?

我需要按特定值对字典列表进行排序。不幸的是,有些值是 None 并且排序在 Python 3 中不起作用,因为它不支持 None 与非 None 值的比较。我还需要保留 None 值并将它们作为最低值放置在新的排序列表中。

编码:

import operator

list_of_dicts_with_nones = [
    {"value": 1, "other_value": 4},
    {"value": 2, "other_value": 3},
    {"value": 3, "other_value": 2},
    {"value": 4, "other_value": 1},
    {"value": None, "other_value": 42},
    {"value": None, "other_value": 9001}
]

# sort by first value but put the None values at the end
new_sorted_list = sorted(
    (some_dict for some_dict in list_of_dicts_with_nones),
    key=operator.itemgetter("value"), reverse=True
)

print(new_sorted_list)
Run Code Online (Sandbox Code Playgroud)

我在 Python 3.6.1 中得到了什么:

Traceback (most recent call last):
  File "/home/bilan/PycharmProjects/py3_tests/py_3_sorting.py", line …
Run Code Online (Sandbox Code Playgroud)

python sorting python-2.7 python-3.x

5
推荐指数
2
解决办法
3260
查看次数

Python,最大的奇数

我是编码的新手,现在已经学习了几天.我在Python中编写了这个程序,同时还参加了一些MIT OpenCourseware讲座和一些书籍.反正有没有更容易表达的程序?

手指练习:编写一个程序,要求用户输入10个整数,然后打印输入的最大奇数.如果没有输入奇数,则应该打印一条消息.

a = int(raw_input('Enter your first integer: '))
b = int(raw_input('Enter your second integer: '))
c = int(raw_input('Enter your third integer: '))
d = int(raw_input('Enter your fourth integer: '))
e = int(raw_input('Enter your fifth integer: '))
f = int(raw_input('Enter your sixth integer: '))
g = int(raw_input('Enter your seventh integer: '))
h = int(raw_input('Enter your eighth integer: '))
i = int(raw_input('Enter your ninth integer: '))
j = int(raw_input('Enter your tenth integer: '))

if a%2 ==0:
    a = 0  
else: …
Run Code Online (Sandbox Code Playgroud)

python

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

python中小于负无穷大的数字?

这在python2中是可能的:

None < float('-inf')
Run Code Online (Sandbox Code Playgroud)

而且,它总是返回

True
Run Code Online (Sandbox Code Playgroud)

但是,在python3上,这会抛出

TypeError: unorderable types: NoneType() < int()
Run Code Online (Sandbox Code Playgroud)

为什么None与python2的整数/浮点数相当?None在python2中可以订购任何好处或应用程序吗?

python python-2.x

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