小编Val*_*ntz的帖子

为什么Python的数组会变慢?

我希望array.array比列表更快,因为数组似乎是未装箱的.

但是,我得到以下结果:

In [1]: import array

In [2]: L = list(range(100000000))

In [3]: A = array.array('l', range(100000000))

In [4]: %timeit sum(L)
1 loop, best of 3: 667 ms per loop

In [5]: %timeit sum(A)
1 loop, best of 3: 1.41 s per loop

In [6]: %timeit sum(L)
1 loop, best of 3: 627 ms per loop

In [7]: %timeit sum(A)
1 loop, best of 3: 1.39 s per loop
Run Code Online (Sandbox Code Playgroud)

可能是造成这种差异的原因是什么?

python arrays performance boxing python-internals

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

导入任意python源文件.(Python 3.3+)

如何.pyPython 3.3+中导入任意python源文件(其文件名可以包含任何字符,并不总是以其结尾)?

我使用imp.load_module如下:

>>> import imp
>>> path = '/tmp/a-b.txt'
>>> with open(path, 'U') as f:
...     mod = imp.load_module('a_b', f, path, ('.py', 'U', imp.PY_SOURCE))
...
>>> mod
<module 'a_b' from '/tmp/a-b.txt'>
Run Code Online (Sandbox Code Playgroud)

它仍然适用于Python 3.3,但根据imp.load_module文档,它已被弃用:

从版本3.3开始不推荐使用:不需要使用加载器来加载模块,并且不推荐使用find_module().

imp模块文档建议使用importlib:

注意新程序应使用importlib而不是此模块.

在不使用已弃用的imp.load_module函数的情况下,在Python 3.3+中加载任意python源文件的正确方法是什么?

python python-3.x python-3.3

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

C中的位域操作

在C中用单位测试和设置单个位的经典问题可能是最常见的中级编程技能之一.您可以使用简单的位掩码进行设置和测试

unsigned int mask = 1<<11;

if (value & mask) {....} // Test for the bit
value |= mask;    // set the bit
value &= ~mask;   // clear the bit
Run Code Online (Sandbox Code Playgroud)

一篇有趣的博客文章认为,这容易出错,难以维护,而且做法不佳.C语言本身提供了类型安全和可移植的位级访问:

typedef unsigned int boolean_t;
#define FALSE 0
#define TRUE !FALSE
typedef union {
        struct {
                boolean_t user:1;
                boolean_t zero:1;
                boolean_t force:1;
                int :28;                /* unused */
                boolean_t compat:1;     /* bit 31 */
        };
        int raw;
} flags_t;

int
create_object(flags_t flags)
{
        boolean_t is_compat = flags.compat;

        if (is_compat) …
Run Code Online (Sandbox Code Playgroud)

c bit-manipulation

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

Scrapy:AttributeError:'list'对象没有属性'iteritems'

这是我关于堆栈溢出的第一个问题.最近我想使用Linked-in-scraper,所以我下载并指示"scrapy crawl linkedin.com"并获得以下错误消息.为了您的信息,我使用anaconda 2.3.0和python 2.7.11.在执行程序之前,所有相关的包(包括scrapy和6)都会由pip更新.

Traceback (most recent call last):
  File "/Users/byeongsuyu/anaconda/bin/scrapy", line 11, in <module>
    sys.exit(execute())
File "/Users/byeongsuyu/anaconda/lib/python2.7/site-packages/scrapy/cmdline.py", line 108, in execute
settings = get_project_settings()
File "/Users/byeongsuyu/anaconda/lib/python2.7/site-packages/scrapy/utils/project.py", line 60, in get_project_settings
settings.setmodule(settings_module_path, priority='project')
File "/Users/byeongsuyu/anaconda/lib/python2.7/site-packages/scrapy/settings/__init__.py", line 285, in setmodule
self.set(key, getattr(module, key), priority)
  File "/Users/byeongsuyu/anaconda/lib/python2.7/site-packages/scrapy/settings/__init__.py", line 260, in set
self.attributes[name].set(value, priority)
  File "/Users/byeongsuyu/anaconda/lib/python2.7/site-packages/scrapy/settings/__init__.py", line 55, in set
value = BaseSettings(value, priority=priority)
  File "/Users/byeongsuyu/anaconda/lib/python2.7/site-packages/scrapy/settings/__init__.py", line 91, in __init__
self.update(values, priority)
  File "/Users/byeongsuyu/anaconda/lib/python2.7/site-packages/scrapy/settings/__init__.py", line 317, in update
for name, …
Run Code Online (Sandbox Code Playgroud)

python scrapy-spider six

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

ListView中的重复条目

我在ListView中获得了重复的项目.向后和向下滚动有时会更改项目顺序.我用谷歌搜索并发现许多线程报告了这个错误,但没有一个帮助我解决我的问题.

这是我的代码:

活动:

package com.github.progval.SeenDroid;

import java.util.ArrayList;
import java.util.List;

import com.github.progval.SeenDroid.lib.Connection;
import com.github.progval.SeenDroid.lib.Message;
import com.github.progval.SeenDroid.lib.MessageFetcher;
import com.github.progval.SeenDroid.lib.Query.ParserException;

import android.app.Activity;
import android.app.ListActivity;
import android.content.SharedPreferences;
import android.os.Bundle;

public class ShowUserActivity extends ListActivity {
    private Connection connection;

    public ArrayList<Message> listMessages = new ArrayList<Message>();
    public MessageAdapter adapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile);
        this.connection = new Connection();
        this.setTitle(R.string.homefeed_title);


        this.listMessages = new MessageFetcher(this.connection).fetchUser();
        this.bindUi();
    }

    private void bindUi() {
        this.adapter = new MessageAdapter(this, this.listMessages); …
Run Code Online (Sandbox Code Playgroud)

java android listview listactivity android-arrayadapter

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

java.lang.UnsupportedClassVersionError:org/openqa/gr id/selenium/GridLauncher(不支持的major.minor版本50.0)

我在Selenium文件夹中获得了这个selenium服务器jar文件.当我尝试使用java - jar selenium-server-standalone-2.0b3.jar命令执行此操作时,我收到此版本差异错误.有谁能告诉我我在哪里犯了错误?

Exception in thread "main" java.lang.UnsupportedClassVersionError: org/openqa/gr
id/selenium/GridLauncher (Unsupported major.minor version 50.0)
        at java.lang.ClassLoader.defineClass0(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Run Code Online (Sandbox Code Playgroud)

java selenium selenium-server

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

Matplotlib - Fail to allocate bitmap

Basically I'm just running a for-loop that plots and saves a bunch of figures as PNG and when I'm up to like 25 figures to save in total I get this "Fail to allocate bitmap" error however I make sure to clear the axis, figure and figure window in between each one, so what gives?

Here's my code just in case:

def update_trade_graphs(instruments):
    print('chokepoint 13')
    for i in range(0, len(instruments)):
        if instruments[i].linked_trade is False:
            continue

        # Update variables
        bid = …
Run Code Online (Sandbox Code Playgroud)

python memory-leaks matplotlib

11
推荐指数
0
解决办法
8217
查看次数

仅使用g ++可以工作,但不能使用"g ++ -c"和ld

我在main.cpp中有以下源代码:

#include <iostream>
#include <iomanip>

int main() {
    std::cout << "Hi" << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

使用此命令可以工作,并创建可执行文件:

g++ -o main main.cpp
Run Code Online (Sandbox Code Playgroud)

但是这个命令不起作用:

g++ -c main.cpp
ld -o main main.o
Run Code Online (Sandbox Code Playgroud)

第二个错误:

ld: warning: cannot find entry symbol _start; defaulting to 00000000004000e8
main.o: In function `main':
main.cpp:(.text+0xa): undefined reference to `std::cout'
main.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
main.cpp:(.text+0x14): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
main.cpp:(.text+0x1c): undefined reference to `std::ostream::operator<<(std::ostream& …
Run Code Online (Sandbox Code Playgroud)

c++ linker g++

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

打开不同目录python中的所有文件

我需要在不使用它的路径的同时打开来自不同目录的文件,同时保留在当前目录中.

当我执行以下代码时:

for file in os.listdir(sub_dir):
    f = open(file, "r")
    lines = f.readlines()
    for line in lines:
        line.replace("dst=", ", ")
        line.replace("proto=", ", ")
        line.replace("dpt=", ", ")
Run Code Online (Sandbox Code Playgroud)

我收到错误消息,FileNotFoundError: [Errno 2] No such file or directory:因为它在子目录中.

问题:我是否可以使用os命令找到并打开文件sub_dir

谢谢!我知道如果这是一个重复,我搜索,找不到一个,但可能已经错过了它.

python python-3.x

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

存储超过4位数年份的日期

例如,我需要一种方法来序列化和反序列化过去可能很远的日期 -10000

我首先看一下ISO8601,但它似乎不支持超过四位数的年份.(至少,我试过的python库没有.)

我能想到的不同解决方案:

  • 更改序列化/反序列化之前的年份,将其提供给解析/格式化库,然后将其修复(听起来很糟糕)
  • 定义我自己的格式,比如year:month:day:hour:minute:second(重新发明轮子,因为我必须处理时区等)
  • 使用没有边界或等效的UNIX时间戳(可能在某些编程语言中溢出,仍然是时区的东西)
  • 由于当时没有时区问题/闰年/ ...问题,因此存储日期之前-9999(或0)与之后的日期不同.(两个不同的格式在同一个地方)

你有没有看到比这些更好的其他方式?或者推荐其中一个?

javascript php python datetime date

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