小编Eri*_*ans的帖子

列表推导和功能函数是否比"for loops"更快?

在Python的性能方面,是一个列表理解,还是比for循环更快的map(),filter()和reduce()等函数?从技术上讲,为什么它们"以C速度运行",而"for循环以python虚拟机速度运行"?

假设在我正在开发的游戏中,我需要使用for循环绘制复杂且巨大的地图.这个问题肯定是相关的,因为如果列表理解确实更快,那么为了避免滞后(尽管代码的视觉复杂性),这将是一个更好的选择.

python performance for-loop list-comprehension map-function

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

与Python 3.4,Unicode,不同语言和Windows有什么关系?

快乐的例子:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

czech = u'Leoš Janá?ek'.encode("utf-8")
print(czech)

pl = u'Zdzis?aw Beksi?ski'.encode("utf-8")
print(pl)

jp = u'??? ?? ??'.encode("utf-8")
print(jp)

chinese = u'??'.encode("utf-8")
print(chinese)

MIR = u'?????? ??? ?????????? ????????'.encode("utf-8")
print(MIR)

pt = u'Minha Língua Portuguesa: çáà'.encode("utf-8")
print(pt)
Run Code Online (Sandbox Code Playgroud)

不愉快的输出:

b'Leo\xc5\xa1 Jan\xc3\xa1\xc4\x8dek'
b'Zdzis\xc5\x82aw Beksi\xc5\x84ski'
b'\xe3\x83\xaa\xe3\x83\xb3\xe3\x82\xb0 \xe5\xb1\xb1\xe6\x9d\x91 \xe8\xb2\x9e\xe5\xad\x90'
b'\xe4\xba\x94\xe8\xa1\x8c'
b'\xd0\x9c\xd0\xb0\xd1\x88\xd0\xb8\xd0\xbd\xd0\xb0 \xd0\xb4\xd0\xbb\xd1\x8f \xd0\x98\xd0\xbd\xd0\xb6\xd0\xb5\xd0\xbd\xd0\xb5\xd1\x80\xd0\xbd\xd1\x8b\xd1\x85 \xd0\xa0\xd0\xb0\xd1\x81\xd1\x87\xd1\x91\xd1\x82\xd0\xbe\xd0\xb2'
b'Minha L\xc3\xadngua Portuguesa: \xc3\xa7\xc3\xa1\xc3\xa0'
Run Code Online (Sandbox Code Playgroud)

如果我像这样打印它们:

jp = u'??? ?? ??'
print(jp)
Run Code Online (Sandbox Code Playgroud)

我明白了:

Traceback (most recent call last):
  File "x.py", line 5, in <module>
    print(jp)
  File …
Run Code Online (Sandbox Code Playgroud)

python unicode

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

如何在Windows 10上安装后打开GitKraken?

这似乎是一个真正的问题.在如何安装GitKraken中,他们指向一个%APPDATA%/.gitkraken文件夹.在那里,你有"配置文件"和"服务"文件夹和"配置","日志"和"secFile"文件.在这些文件夹中,没有可执行文件的迹象.它不在Program Files下,也不在PATH环境变量中,它无处可寻.如果再次安装它,它会正常打开并记录在您的配置文件中,但在关闭它之后,无法再次打开它.没有捷径.

我错过了什么?

windows-10 gitkraken

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

如何按第一个元素对元组列表进行排序?

我有一个元组列表:

self.gridKeys = self.gridMap.keys() # The keys of the instance of the GridMap (It returns the product of every possible combination of positions in the specified grid, in tuples.)
print self.gridKeys
Run Code Online (Sandbox Code Playgroud)

self.gridKeys:

[(7, 3), (6, 9), (0, 7), (1, 6), (3, 7), (2, 5), (8, 5), (5, 8), (4, 0), (9, 0), (6, 7), (5, 5), (7, 6), (0, 4), (1, 1), (3, 2), (2, 6), (8, 2), (4, 5), (9, 3), (6, 0), (7, 5), (0, 1), (3, 1), …
Run Code Online (Sandbox Code Playgroud)

python sorting tuples list

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

ServerSocket - 是否真的有必要关闭()它?

我有这个该死的结构:

public void run() {

        try {
            if (!portField.getText().equals("")) {              
                String p = portField.getText();
                CharSequence numbers = "0123456789";

            btnRun.setEnabled(false);

            if (p.contains(numbers)) {
                ServerSocket listener = new ServerSocket(Integer.parseInt(p));

                while (true) {
                    Socket socket = listener.accept();
                    try {
                        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                        out.println("Hi there, human.");    
                    } finally {
                        socket.close(); 
                    }
                }} else {
                    JOptionPane.showMessageDialog(null, "Only numbers are allowed.");
                }
            }

        } catch (NumberFormatException | HeadlessException | IOException e) {
            e.printStackTrace();
        } 
    }
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我需要完全像使用套接字一样关闭侦听器.问题是,如果我在循环后尝试这样做,代码将"无法访问",如果我尝试在ServerSocket的任何地方声明一个字段,我会得到一个NullPointerException.我不想与socket/client一起关闭ServerSocket,因为我想建立新的连接.

这是我的问题:

在这种情况下关闭ServerSocket真的是必要的吗?当软件关闭时,ServerSocket会自行关闭?(System.exit(0)).如果我关闭软件时继续运行ServerSocket只是因为我没有关闭它,那么我有一个问题,因为我无法达到那个血腥的代码来关闭它:).

java serversocket

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

如何解决本地主机的“idpiframe_initialization_failed”?

这是我的初始化代码:

            function HandleGoogleApiLibrary() {
                // Load "client" & "auth2" libraries
                gapi.load('client:auth2', {
                    callback: function () {
                        // Initialize client & auth libraries
                        gapi.client.init({
                            apiKey: 'My API Key',
                            clientId: 'My Client ID',
                            scope: 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.me'
                        }).then(
                            function (success) {
                                console.log("Yaaay");
                            },
                            function (error) {
                                console.log("Nope");
                                console.log(error);
                            }
                        );
                    },
                    onerror: function () {
                        // Failed to load libraries
                    }
                });
            }
Run Code Online (Sandbox Code Playgroud)

这给了我以下错误消息:

details: "Not a valid origin for the client: http://127.0.0.1 has not been whitelisted for client ID 388774754090-o7o913o81ldb2jcfu939cfu36kh9h0q6.apps.googleusercontent.com. Please …
Run Code Online (Sandbox Code Playgroud)

javascript google-api google-oauth

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

如何在Pygame中退出全屏模式?

这可能是一个愚蠢的问题,但这是一个愚蠢的问题,我无法找到它的文档.

Pygame为display.set.mode()提供了这些标志:

pygame.FULLSCREEN    create a fullscreen display
pygame.DOUBLEBUF     recommended for HWSURFACE or OPENGL
pygame.HWSURFACE     hardware accelerated, only in FULLSCREEN
pygame.OPENGL        create an OpenGL renderable display
pygame.RESIZABLE     display window should be sizeable
pygame.NOFRAME       display window will have no border or controls
Run Code Online (Sandbox Code Playgroud)

好的,我可以进入全屏模式..现在这是我的代码:

__author__ = 'EricsonWillians'

from pygame import *
import ctypes
init()
user32 = ctypes.windll.user32
screenSize = user32.GetSystemMetrics(0)/2, user32.GetSystemMetrics(1)/2

size = (screenSize)
screen = display.set_mode(size)
display.set_caption("Game")
done = False
clock = time.Clock()

def keyPressed(inputKey):
    keysPressed = key.get_pressed()
    if keysPressed[inputKey]:
        return True …
Run Code Online (Sandbox Code Playgroud)

python pygame fullscreen exit toggle

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

如何在不使用密码的情况下连接MySQL?(PyMySQL)

这是我的代码的相关部分:

        try:
            if self.pass_entry.get():
                self.connection = pymysql.connect(
                    host=self.host_entry.get(),
                    user=self.user_entry.get(),
                    password=self.pass_entry.get(),
                    db=self.db_entry.get(),
                    charset="utf8mb4",
                    cursorclass=pymysql.cursors.DictCursor
                )
            else:
                self.connection = pymysql.connect(
                    host=self.host_entry.get(),
                    user=self.user_entry.get(),
                    db=self.db_entry.get(),
                    charset="utf8mb4",
                    cursorclass=pymysql.cursors.DictCursor
                )
        except Exception as e:
            self.console.insert(tk.END, "Error: {err}\n".format(err=str(e)))
Run Code Online (Sandbox Code Playgroud)

这是错误:

Connecting to 127.0.0.1...
Error: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")
Run Code Online (Sandbox Code Playgroud)

这里"root"上没有密码,PyMySQL假设它有一个密码.如何在不使用密码的情况下连接?(当密码字段/字符串为空时,我试图省略密码选项,但PyMySQL不考虑它).

python mysql

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

如何在抽象语法树上递归执行"树步行"?

我的语言分配的简单示例:

x = 3 ->
Run Code Online (Sandbox Code Playgroud)

这是解析后生成的AST(在Python中):

[('statement', ('assignment', 'x', ('assignment_operator', '='), ('expr', ('term', ('factor', '3')))), '->')]
Run Code Online (Sandbox Code Playgroud)

我怎样才能递归访问任何可能的深度,以便在最简单的情况下打印所有这些深度?(或者将文本转换成其他内容?).这样做有特定的算法吗?如果有,您是否推荐任何特定材料?

python recursion

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

如何使用Jython/PyDev修复Eclipse Kepler/Luna中的UnsupportedCharsetException?

示例代码:

from java.lang import System

if __name__ == '__main__':
    [System.out.print(x) for x in "Python-powered Java Hello World from within a List-Comprehension."]
Run Code Online (Sandbox Code Playgroud)

烦人的输出:

console: Failed to install 'org.python.util.JLineConsole': java.nio.charset.UnsupportedCharsetException: cp0.
console: Failed to install 'org.python.util.JLineConsole': java.nio.charset.UnsupportedCharsetException: cp0.
Python-powered Java Hello World from within a List-Comprehension.
Run Code Online (Sandbox Code Playgroud)

我试过这里这里描述的解决方案.两个解决方案都失败了(我已经将-Dpython.console.encoding = UTF-8参数添加到JVM和PyDev交互式控制台).

从4个月前开始还有另外一个问题,没有人回答.那么,我该如何解决呢?

编辑:我刚安装了新的Eclipse Luna,用Jython安装了PyDev,同样的事情发生了.

python java eclipse jython pydev

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