小编ned*_*004的帖子

点"无法找到满足要求的"

当我尝试安装PyGame时:pip install pygame它说

收集pygame

找不到满足要求pygame的版本(来自版本:)
没有找到匹配的分发

我相信我使用的是最新版本8.1.1.我在Windows 8.1上使用Python 3.5.1.我已经看过这个问题的其他答案,但没有一个对我有用.谢谢你的帮助.

python pip

18
推荐指数
1
解决办法
15万
查看次数

检测项目是列表中的最后一项

我正在使用Python 3,并尝试检测项目是否是列表中的最后一项,但有时会重复.这是我的代码:

a = ['hello', 9, 3.14, 9]
for item in a:
    print(item, end='')
    if item != a[-1]:
        print(', ')
Run Code Online (Sandbox Code Playgroud)

我希望这个输出:

hello,
9,
3.14,
9
Run Code Online (Sandbox Code Playgroud)

但我得到这个输出:

hello, 
93.14, 
9
Run Code Online (Sandbox Code Playgroud)

我理解为什么我得到了我不想要的输出.我宁愿我仍然可以使用循环,但我可以解决它们.(我想用更复杂的代码来使用它)

python list python-3.x

10
推荐指数
4
解决办法
3万
查看次数

Python打破内部功能

我正在使用Python 3.5,我想break在函数内部使用命令,但我不知道如何.我想用这样的东西:

def stopIfZero(a):
    if int(a) == 0:
        break
    else:
        print('Continue')

while True:
    stopIfZero(input('Number: '))
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用这段代码:

while True:
    a = int(input('Number: '))
    if a == 0:
        break
    else:
        print('Continue')
Run Code Online (Sandbox Code Playgroud)

如果你不关心这个print('Continue')部分,你甚至可以做一个单行:( while a != 0: a = int(input('Number: '))只要一个已经分配给0以外的东西)但是,我想使用一个函数,因为其他时候它可以帮助很多.

谢谢你的帮助.

python function break python-3.x

6
推荐指数
2
解决办法
4231
查看次数

Python C扩展缺少功能

在遵循针对Python的C扩展教程时,我的模块似乎缺少其内容。虽然构建和导入模块没有问题,但是在模块中使用该功能失败。我在macOS上使用Python 3.7。

testmodule.c

#define PY_SSIZE_T_CLEAN
#include <Python.h>

static PyObject* add(PyObject *self, PyObject *args) {
    const long long x, y;
    if (!PyArg_ParseTuple(args, "LL", &x, &y)) {
        return NULL;
    }
    return PyLong_FromLongLong(x + y);
}

static PyMethodDef TestMethods[] = {
    {"add", add, METH_VARARGS, "Add two numbers."},
    {NULL, NULL, 0, NULL}
};

static struct PyModuleDef testmodule = {
    PyModuleDef_HEAD_INIT,
    "test",
    NULL,
    -1,
    TestMethods
};

PyMODINIT_FUNC PyInit_test(void)
{
    return PyModule_Create(&testmodule);
}
Run Code Online (Sandbox Code Playgroud)

setup.py

from distutils.core import setup, Extension

module1 = Extension('test', sources=['testmodule.c'])

setup(name='Test',
      version='1.0',
      description='Test …
Run Code Online (Sandbox Code Playgroud)

python python-extensions

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

数组中的 Python Numba 值

我试图检查一个数字是否在int8s 的NumPy 数组中。我试过这个,但它不起作用。

from numba import njit
import numpy as np

@njit
def c(b):
    return 9 in b

a = np.array((9, 10, 11), 'int8')
print(c(a))
Run Code Online (Sandbox Code Playgroud)

我得到的错误是

Invalid use of Function(<built-in function contains>) with argument(s) of type(s): (array(int8, 1d, C), Literal[int](9))
 * parameterized
In definition 0:
    All templates rejected with literals.
In definition 1:
    All templates rejected without literals.
In definition 2:
    All templates rejected with literals.
In definition 3:
    All templates rejected without literals.
In definition 4:
    All …
Run Code Online (Sandbox Code Playgroud)

python arrays numpy numba

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

Python 3中排序字符串的所有不等子集

我想找到排序字符串的所有子集,忽略顺序以及哪些字符彼此相邻.我认为解释这个的最佳方式是一个例子.结果也应该从最长到最短.

这些是结果bell.

bell
bel
bll
ell
be
bl
el
ll
b
e
l
Run Code Online (Sandbox Code Playgroud)

我已经想到了如何做到这一点,但没有任何长度的输入.谢谢!

python combinations substring subset python-3.x

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

int()不适用于浮点数?

我最近在Python 3.5中遇到过这个问题:

>>> flt = '3.14'
>>> integer = '5'
>>> float(integer)
5.0
>>> float(flt)
3.14
>>> int(integer)
5
>>> int(flt)
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    int(flt)
ValueError: invalid literal for int() with base 10: '3.14'
Run Code Online (Sandbox Code Playgroud)

为什么是这样?似乎应该回归3.我做错了什么,或者这是否有充分理由?

python floating-point int python-3.x

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

无法从命令行使用Python 2

我最近尝试卸载Python 3.5.2并安装了Python 3.6.0。我曾经python在命令行中使用来从命令行运行Python 3.5.2,并py运行python 2.7.12。现在,python运行Python 3.5.2,然后py运行Python 3.6.0。我运行Windows 10,和python3python2py2,并py3没有做任何事情。

python windows python-2.7 python-3.5 python-3.6

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

如果在MinGW中导致奇怪的循环

当我在for循环中有一个if,编译器无法优化时,我会得到令人难以置信的奇怪行为.看起来好像只有循环a = 0才能运行,但是没有提前终止.一个例子是:

#include <iostream>

int main() {
    for (int a = -5; a < 5; a++) {
        std::cout << a << std::endl;
        //if (rand() % a) {} // uncommenting this line gives the weird results
    }
}
Run Code Online (Sandbox Code Playgroud)

当如上运行时,我得到预期的输出,这:

-5
-4
-3
-2
-1
0
1
2
3
4
Run Code Online (Sandbox Code Playgroud)

但是,当我取消注释该行(if中没有任何内容)时,我会得到此输出:

-5
-4
-3
-2
-1
0
Run Code Online (Sandbox Code Playgroud)

for循环似乎一直在运行,直到它到达a = 0.当有一个if(有一个或没有)时,我只得到第二个奇怪的结果,优化设置是-O0-O1.我在Windows 10上运行带有GCC版本6.3.0的MinGW.

c++

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