字典元组列表

问题:字典元组列表

这是我目前在Python中将元组列表转换为字典的方式:

l = [('a',1),('b',2)]
h = {}
[h.update({k:v}) for k,v in l]
> [None, None]
h
> {'a': 1, 'b': 2}

有没有更好的办法?似乎应该有一种做法。

Here’s how I’m currently converting a list of tuples to dictionary in Python:

l = [('a',1),('b',2)]
h = {}
[h.update({k:v}) for k,v in l]
> [None, None]
h
> {'a': 1, 'b': 2}

Is there a better way? It seems like there should be a one-liner to do this.


回答 0

只需dict()直接调用元组列表

>>> my_list = [('a', 1), ('b', 2)]
>>> dict(my_list)
{'a': 1, 'b': 2}

Just call dict() on the list of tuples directly

>>> my_list = [('a', 1), ('b', 2)]
>>> dict(my_list)
{'a': 1, 'b': 2}

回答 1

dict构造函数接受输入的完全一样,你把它(键/值元组)。

>>> l = [('a',1),('b',2)]
>>> d = dict(l)
>>> d
{'a': 1, 'b': 2}

文档中

例如,所有这些都返回等于{“ one”:1,“ two”:2}的字典:

dict(one=1, two=2)
dict({'one': 1, 'two': 2})
dict(zip(('one', 'two'), (1, 2)))
dict([['two', 2], ['one', 1]])

The dict constructor accepts input exactly as you have it (key/value tuples).

>>> l = [('a',1),('b',2)]
>>> d = dict(l)
>>> d
{'a': 1, 'b': 2}

From the documentation:

For example, these all return a dictionary equal to {“one”: 1, “two”: 2}:

dict(one=1, two=2)
dict({'one': 1, 'two': 2})
dict(zip(('one', 'two'), (1, 2)))
dict([['two', 2], ['one', 1]])

回答 2

具有dict理解力:

h = {k:v for k,v in l}

With dict comprehension:

h = {k:v for k,v in l}

回答 3

似乎每个人都假定元组列表在键和值之间具有一对一的映射关系(例如,它没有字典的重复键)。由于这是在该主题上搜索的第一个问题,因此我针对一个更常见的情况发布了答案,在这种情况下,我们必须处理重复项:

mylist = [(a,1),(a,2),(b,3)]    
result = {}
for i in mylist:  
   result.setdefault(i[0],[]).append(i[1])
print(result)
>>> result = {a:[1,2], b:[3]}

It seems everyone here assumes the list of tuples have one to one mapping between key and values (e.g. it does not have duplicated keys for the dictionary). As this is the first question coming up searching on this topic, I post an answer for a more general case where we have to deal with duplicates:

mylist = [(a,1),(a,2),(b,3)]    
result = {}
for i in mylist:  
   result.setdefault(i[0],[]).append(i[1])
print(result)
>>> result = {a:[1,2], b:[3]}

如何从Python函数调用中捕获标准输出?

问题:如何从Python函数调用中捕获标准输出?

我正在使用对对象执行某些操作的Python库

do_something(my_object)

并更改它。这样做时,它会向stdout打印一些统计信息,我希望掌握这些信息。正确的解决方案是更改do_something()以返回相关信息,

out = do_something(my_object)

但是开发人员需要一段时间才能do_something()解决此问题。作为一种解决方法,我考虑过解析do_something()对stdout的任何写入。

如何捕获代码两点之间的stdout输出,例如

start_capturing()
do_something(my_object)
out = end_capturing()

I’m using a Python library that does something to an object

do_something(my_object)

and changes it. While doing so, it prints some statistics to stdout, and I’d like to get a grip on this information. The proper solution would be to change do_something() to return the relevant information,

out = do_something(my_object)

but it will be a while before the devs of do_something() get to this issue. As a workaround, I thought about parsing whatever do_something() writes to stdout.

How can I capture stdout output between two points in the code, e.g.,

start_capturing()
do_something(my_object)
out = end_capturing()

?


回答 0

试试这个上下文管理器:

from io import StringIO 
import sys

class Capturing(list):
    def __enter__(self):
        self._stdout = sys.stdout
        sys.stdout = self._stringio = StringIO()
        return self
    def __exit__(self, *args):
        self.extend(self._stringio.getvalue().splitlines())
        del self._stringio    # free up some memory
        sys.stdout = self._stdout

用法:

with Capturing() as output:
    do_something(my_object)

output 现在是一个包含函数调用打印的行的列表。

高级用法:

可能不明显的是,此操作可以执行一次以上,并将结果连接在一起:

with Capturing() as output:
    print('hello world')

print('displays on screen')

with Capturing(output) as output:  # note the constructor argument
    print('hello world2')

print('done')
print('output:', output)

输出:

displays on screen                     
done                                   
output: ['hello world', 'hello world2']

更新:它们已添加redirect_stdout()contextlibPython 3.4中(以及redirect_stderr())。因此,您可以使用io.StringIO它来达到类似的结果(尽管Capturing列表和上下文管理器可能更方便)。

Try this context manager:

from io import StringIO 
import sys

class Capturing(list):
    def __enter__(self):
        self._stdout = sys.stdout
        sys.stdout = self._stringio = StringIO()
        return self
    def __exit__(self, *args):
        self.extend(self._stringio.getvalue().splitlines())
        del self._stringio    # free up some memory
        sys.stdout = self._stdout

Usage:

with Capturing() as output:
    do_something(my_object)

output is now a list containing the lines printed by the function call.

Advanced usage:

What may not be obvious is that this can be done more than once and the results concatenated:

with Capturing() as output:
    print('hello world')

print('displays on screen')

with Capturing(output) as output:  # note the constructor argument
    print('hello world2')

print('done')
print('output:', output)

Output:

displays on screen                     
done                                   
output: ['hello world', 'hello world2']

Update: They added redirect_stdout() to contextlib in Python 3.4 (along with redirect_stderr()). So you could use io.StringIO with that to achieve a similar result (though Capturing being a list as well as a context manager is arguably more convenient).


回答 1

在python> = 3.4中,contextlib包含一个redirect_stdout装饰器。它可以像这样回答您的问题:

import io
from contextlib import redirect_stdout

f = io.StringIO()
with redirect_stdout(f):
    do_something(my_object)
out = f.getvalue()

文档

上下文管理器,用于临时将sys.stdout重定向到另一个文件或类似文件的对象。

该工具为现有功能或类的输出增加了灵活性,这些功能或类的输出被硬连线到stdout。

例如,通常将help()的输出发送到sys.stdout。您可以通过将输出重定向到io.StringIO对象来捕获该输出的字符串:

  f = io.StringIO() 
  with redirect_stdout(f):
      help(pow) 
  s = f.getvalue()

要将help()的输出发送到磁盘上的文件,请将输出重定向到常规文件:

 with open('help.txt', 'w') as f:
     with redirect_stdout(f):
         help(pow)

要将help()的输出发送到sys.stderr:

with redirect_stdout(sys.stderr):
    help(pow)

请注意,对sys.stdout的全局副作用意味着此上下文管理器不适合在库代码和大多数线程应用程序中使用。它还对子流程的输出没有影响。但是,对于许多实用程序脚本,它仍然是一种有用的方法。

该上下文管理器是可重入的。

In python >= 3.4, contextlib contains a redirect_stdout decorator. It can be used to answer your question like so:

import io
from contextlib import redirect_stdout

f = io.StringIO()
with redirect_stdout(f):
    do_something(my_object)
out = f.getvalue()

From the docs:

Context manager for temporarily redirecting sys.stdout to another file or file-like object.

This tool adds flexibility to existing functions or classes whose output is hardwired to stdout.

For example, the output of help() normally is sent to sys.stdout. You can capture that output in a string by redirecting the output to an io.StringIO object:

  f = io.StringIO() 
  with redirect_stdout(f):
      help(pow) 
  s = f.getvalue()

To send the output of help() to a file on disk, redirect the output to a regular file:

 with open('help.txt', 'w') as f:
     with redirect_stdout(f):
         help(pow)

To send the output of help() to sys.stderr:

with redirect_stdout(sys.stderr):
    help(pow)

Note that the global side effect on sys.stdout means that this context manager is not suitable for use in library code and most threaded applications. It also has no effect on the output of subprocesses. However, it is still a useful approach for many utility scripts.

This context manager is reentrant.


回答 2

这是使用文件管道的异步解决方案。

import threading
import sys
import os

class Capturing():
    def __init__(self):
        self._stdout = None
        self._stderr = None
        self._r = None
        self._w = None
        self._thread = None
        self._on_readline_cb = None

    def _handler(self):
        while not self._w.closed:
            try:
                while True:
                    line = self._r.readline()
                    if len(line) == 0: break
                    if self._on_readline_cb: self._on_readline_cb(line)
            except:
                break

    def print(self, s, end=""):
        print(s, file=self._stdout, end=end)

    def on_readline(self, callback):
        self._on_readline_cb = callback

    def start(self):
        self._stdout = sys.stdout
        self._stderr = sys.stderr
        r, w = os.pipe()
        r, w = os.fdopen(r, 'r'), os.fdopen(w, 'w', 1)
        self._r = r
        self._w = w
        sys.stdout = self._w
        sys.stderr = self._w
        self._thread = threading.Thread(target=self._handler)
        self._thread.start()

    def stop(self):
        self._w.close()
        if self._thread: self._thread.join()
        self._r.close()
        sys.stdout = self._stdout
        sys.stderr = self._stderr

用法示例:

from Capturing import *
import time

capturing = Capturing()

def on_read(line):
    # do something with the line
    capturing.print("got line: "+line)

capturing.on_readline(on_read)
capturing.start()
print("hello 1")
time.sleep(1)
print("hello 2")
time.sleep(1)
print("hello 3")
capturing.stop()

Here is an async solution using file pipes.

import threading
import sys
import os

class Capturing():
    def __init__(self):
        self._stdout = None
        self._stderr = None
        self._r = None
        self._w = None
        self._thread = None
        self._on_readline_cb = None

    def _handler(self):
        while not self._w.closed:
            try:
                while True:
                    line = self._r.readline()
                    if len(line) == 0: break
                    if self._on_readline_cb: self._on_readline_cb(line)
            except:
                break

    def print(self, s, end=""):
        print(s, file=self._stdout, end=end)

    def on_readline(self, callback):
        self._on_readline_cb = callback

    def start(self):
        self._stdout = sys.stdout
        self._stderr = sys.stderr
        r, w = os.pipe()
        r, w = os.fdopen(r, 'r'), os.fdopen(w, 'w', 1)
        self._r = r
        self._w = w
        sys.stdout = self._w
        sys.stderr = self._w
        self._thread = threading.Thread(target=self._handler)
        self._thread.start()

    def stop(self):
        self._w.close()
        if self._thread: self._thread.join()
        self._r.close()
        sys.stdout = self._stdout
        sys.stderr = self._stderr

Example usage:

from Capturing import *
import time

capturing = Capturing()

def on_read(line):
    # do something with the line
    capturing.print("got line: "+line)

capturing.on_readline(on_read)
capturing.start()
print("hello 1")
time.sleep(1)
print("hello 2")
time.sleep(1)
print("hello 3")
capturing.stop()

| =(ior)在Python中做什么?

问题:| =(ior)在Python中做什么?

Google不允许我进行搜索,|=因此我找不到相关文档。有人知道吗

Google won’t let me search |= so I’m having trouble finding relevant documentation. Anybody know?


回答 0

|=在成对的对象之间执行就地+操作。尤其是:

在大多数情况下,它与|操作员有关。请参见下面的示例。

套装

例如,两个分配的集合的s1并并s2共享以下等效表达式:

>>> s1 = s1 | s12                                          # 1
>>> s1 |= s2                                               # 2
>>> s1.__ior__(s2)                                         # 3

其中的最终值s1等于:

  1. 分配的OR操作
  2. 就地或运算
  3. 通过特殊方法++进行就地或运算

在这里,我们将OR(|)和原位OR(|=)应用于集合

>>> s1 = {"a", "b", "c"}
>>> s2 = {"d", "e", "f"}

>>> # OR, | 
>>> s1 | s2
{'a', 'b', 'c', 'd', 'e', 'f'}
>>> s1                                                     # `s1` is unchanged
{'a', 'b', 'c'}

>>> # Inplace OR, |=
>>> s1 |= s2
>>> s1                                                     # `s1` is reassigned
{'a', 'b', 'c', 'd', 'e', 'f'}

辞典

Python 3.9+中,在字典之间提出了新的merge(|)和update(|=)运算符。注意:这些与上述集合运算符不同。

两个指定的dict d1和之间的给定操作d2

>>> d1 = d1 | d2                                           # 1
>>> d1 |= d2                                               # 2

其中d1经由相当于:

  1. 分配的合并权操作
  2. 就地合并权限(更新)操作;相当于d1.update(d2)

在这里,我们将合并(|)和更新(|=)应用于字典

>>> d1 = {"a": 0, "b": 1, "c": 2}
>>> d2 = {"c": 20, "d": 30}

>>> # Merge, | 
>>> d1 | d2
{"a": 0, "b": 1, "c": 20, "d": 30}
>>> d1 
{"a": 0, "b": 1, "c": 2}

>>> # Update, |=
>>> d1 |= d2
>>> d1 
{"a": 0, "b": 1, "c": 20, "d": 30}

专柜

collections.Counter涉及称为数学数据结构多集(MSET)。它基本上是(对象,多重性)键值对的决定。

给定两个分配柜台之间的操作c1c2

>>> c1 = c1 | c2                                           # 1
>>> c1 |= c2                                               # 2

其中c1经由相当于:

  1. 指定的联合操作
  2. 就地联合操作

多重集并集包含每个条目的最大多重性。请注意,这与两个集合之间或两个常规字典之间的行为不同。

在这里,我们将union(|)和就地union(|=)应用于Counters

import collections as ct


>>> c1 = ct.Counter({2: 2, 3: 3})
>>> c2 = ct.Counter({1: 1, 3: 5})

>>> # Union, |    
>>> c1 | c2
Counter({2: 2, 3: 5, 1: 1})
>>> c1
Counter({2: 2, 3: 3})

>>> # Inplace Union, |=
>>> c1 |= c2
>>> c1
Counter({2: 2, 3: 5, 1: 1})

号码

最后,您可以进行二进制数学运算。

给定的两个指定数字之间的运算n1n2

>>> n1 = n1 | n2                                           # 1
>>> n1 |= n2                                               # 2

其中n1经由相当于:

  1. 分配的按位或运算
  2. 就地按位或运算

在这里,我们采用按位OR( |)和就地按位OR( |=),以数字

>>> n1 = 0
>>> n2 = 1

>>> # Bitwise OR, |
>>> n1 | n2
1
>>> n1
0

>>> # Inplace Bitwise OR, |=
>>> n1 |= n2
>>> n1
1

评论

本节简要回顾一些按位数学。在最简单的情况下,按位或运算会比较两个二进制位。1除非两个位均为,否则它将始终返回0

>>> assert 1 == (1 | 1) == (1 | 0) == (0 | 1)
>>> assert 0 == (0 | 0)

现在,我们将这个想法扩展到二进制数之外。给定任意两个整数(缺少小数部分),我们应用按位“或”并得到一个积分结果:

>>> a = 10 
>>> b = 16 
>>> a | b
26

怎么样?通常,按位运算遵循一些“规则”:

  1. 内部比较二进制等效项
  2. 应用操作
  3. 以给定类型返回结果

让我们将这些规则应用于上面的常规整数。

(1)比较二进制等效项,在这里视为字符串(0b表示二进制):

>>> bin(a)
'0b1010'
>>> bin(b)
'0b10000'

(2)对每列应用按位或运算(0当两者均为时0,否则1):

01010
10000
-----
11010

(3)返回给定类型的结果,例如以10为底的十进制数:

>>> int(0b11010)
26

内部二进制比较意味着我们可以将后者应用于任何基数的整数,例如十六进制和八进制:

>>> c = 0xa
>>> d = 0o32
>>> c | d
26

也可以看看

+ 就位按位或运算符不能应用于文字;将对象分配给名称。

++ 特殊方法返回与其对应的运算符相同的操作。

|= performs an in-place+ operation between pairs of objects. In particular, between:

In most cases, it is related to the | operator. See examples below.

Sets

For example, the union of two assigned sets s1 and s2 share the following equivalent expressions:

>>> s1 = s1 | s2                                           # 1
>>> s1 |= s2                                               # 2
>>> s1.__ior__(s2)                                         # 3

where the final value of s1 is equivalent either by:

  1. an assigned OR operation
  2. an in-place OR operation
  3. an in-place OR operation via special method++

Example

Here we apply OR (|) and the in-place OR (|=) to sets:

>>> s1 = {"a", "b", "c"}
>>> s2 = {"d", "e", "f"}

>>> # OR, | 
>>> s1 | s2
{'a', 'b', 'c', 'd', 'e', 'f'}
>>> s1                                                     # `s1` is unchanged
{'a', 'b', 'c'}

>>> # In-place OR, |=
>>> s1 |= s2
>>> s1                                                     # `s1` is reassigned
{'a', 'b', 'c', 'd', 'e', 'f'}

Dictionaries

In Python 3.9+, new merge (|) and update (|=) operators are proposed between dictionaries. Note: these are not the same as set operators mentioned above.

Given operations between two assigned dicts d1 and d2:

>>> d1 = d1 | d2                                           # 1
>>> d1 |= d2                                               # 2

where d1 is equivalent via:

  1. an assigned merge-right operation
  2. an in-place merge-right (update) operation; equivalent to d1.update(d2)

Example

Here we apply merge (|) and update (|=) to dicts:

>>> d1 = {"a": 0, "b": 1, "c": 2}
>>> d2 = {"c": 20, "d": 30}

>>> # Merge, | 
>>> d1 | d2
{"a": 0, "b": 1, "c": 20, "d": 30}
>>> d1 
{"a": 0, "b": 1, "c": 2}

>>> # Update, |=
>>> d1 |= d2
>>> d1 
{"a": 0, "b": 1, "c": 20, "d": 30}

Counters

The collections.Counter is related to a mathematical datastructure called a multiset (mset). It is basically a dict of (object, multiplicity) key-value pairs.

Given operations between two assigned counters c1 and c2:

>>> c1 = c1 | c2                                           # 1
>>> c1 |= c2                                               # 2

where c1 is equivalent via:

  1. an assigned union operation
  2. an in-place union operation

A union of multisets contains the maximum multiplicities per entry. Note, this does not behave the same way as between two sets or between two regular dicts.

Example

Here we apply union (|) and the in-place union (|=) to Counters:

import collections as ct


>>> c1 = ct.Counter({2: 2, 3: 3})
>>> c2 = ct.Counter({1: 1, 3: 5})

>>> # Union, |    
>>> c1 | c2
Counter({2: 2, 3: 5, 1: 1})
>>> c1
Counter({2: 2, 3: 3})

>>> # In-place Union, |=
>>> c1 |= c2
>>> c1
Counter({2: 2, 3: 5, 1: 1})

Numbers

Lastly, you can do binary math.

Given operations between two assigned numbers n1 and n2:

>>> n1 = n1 | n2                                           # 1
>>> n1 |= n2                                               # 2

where n1 is equivalent via:

  1. an assigned bitwise OR operation
  2. an in-place bitwise OR operation

Example

Here we apply bitwise OR (|) and the in-place bitwise OR (|=) to numbers:

>>> n1 = 0
>>> n2 = 1

>>> # Bitwise OR, |
>>> n1 | n2
1
>>> n1
0

>>> # In-place Bitwise OR, |=
>>> n1 |= n2
>>> n1
1

Review

This section briefly reviews some bitwise math. In the simplest case, the bitwise OR operation compares two binary bits. It will always return 1 except when both bits are 0.

>>> assert 1 == (1 | 1) == (1 | 0) == (0 | 1)
>>> assert 0 == (0 | 0)

We now extend this idea beyond binary numbers. Given any two integral numbers (lacking fractional components), we apply the bitwise OR and get an integral result:

>>> a = 10 
>>> b = 16 
>>> a | b
26

How? In general, the bitwise operations follow some “rules”:

  1. internally compare binary equivalents
  2. apply the operation
  3. return the result as the given type

Let’s apply these rules to our regular integers above.

(1) Compare binary equivalents, seen here as strings (0b denotes binary):

>>> bin(a)
'0b1010'
>>> bin(b)
'0b10000'

(2) Apply a bitwise OR operation to each column (0 when both are 0, else 1):

01010
10000
-----
11010

(3) Return the result in the given type, e.g. base 10, decimal:

>>> int(0b11010)
26

The internal binary comparison means we can apply the latter to integers in any base, e.g. hex and octal:

>>> c = 0xa                                            # 10
>>> d = 0o20                                           # 16 
>>> c | d
26

See Also

+The in-place bitwise OR operator cannot be applied to literals; assign objects to names.

++Special methods return the same operations as their corresponding operators.


回答 1

在Python和其他许多编程语言中,|按位或运算|=是按|原样+=进行的+,即操作和分配的组合。

因此var |= value是的缩写var = var | value

一个常见的用例是合并两个集合:

>>> a = {1,2}; a |= {3,4}; print(a)
{1, 2, 3, 4}

In Python, and many other programming languages, | is the bitwise-OR operation. |= is to | as += is to +, i.e. a combination of operation and asignment.

So var |= value is short for var = var | value.

A common use case is to merge two sets:

>>> a = {1,2}; a |= {3,4}; print(a)
{1, 2, 3, 4}

回答 2

与集合一起使用时,它将执行并集操作。

When used with sets it performs union operation.


回答 3

这只是当前变量和另一个变量之间的或运算。是T=TrueF=False,以图形方式查看输出:

r    s    r|=s
--------------
T    T    T
T    F    T
F    T    T
F    F    F

例如:

>>> r=True
>>> r|=False
>>> r
True
>>> r=False
>>> r|=False
>>> r
False
>>> r|=True
>>> r
True

This is just an OR operation between the current variable and the other one. Being T=True and F=False, see the output graphically:

r    s    r|=s
--------------
T    T    T
T    F    T
F    T    T
F    F    F

For example:

>>> r=True
>>> r|=False
>>> r
True
>>> r=False
>>> r|=False
>>> r
False
>>> r|=True
>>> r
True

回答 4

它对赋值的左侧和右侧执行二进制按位或运算,然后将结果存储在左侧变量中。

http://docs.python.org/reference/expressions.html#binary-bitwise-operations

It performs a binary bitwise OR of the left-hand and right-hand sides of the assignment, then stores the result in the left-hand variable.

http://docs.python.org/reference/expressions.html#binary-bitwise-operations


回答 5

是按位还是。假设我们有32 |= 10,图片32和10是二进制的。

32 = 10 0000
10 = 00 1010

现在因为 是或,按位或对两个数字

即1或0-> 1,0或0-> 0。

10 0000 | 00 1010 = 10 1010.

现在将二进制更改为十进制10 1010 = 42。

对于| =,请考虑已知示例x +=5。这意味着x = x + 5,因此,如果我们有x |= 5,它的意思x = x bitwiseor with 5

It’s bitwise or. Let’s say we have 32 |= 10, picture 32 and 10 is binary.

32 = 10 0000
10 = 00 1010

Now because | is or, do a bitwise or on the two numbers

i.e 1 or 0 –> 1, 0 or 0 –> 0. Continue this down the chain

10 0000 | 00 1010 = 10 1010.

Now change the binary into a decimal, 10 1010 = 42.

For |=, think of the known examples, x +=5. It means x = x + 5, therefore if we have x |= 5, it means x = x bitwiseor with 5.


回答 6

给出用例(在花了一些时间回答其他问题之后):

def process(item):
   return bool(item) # imagine some sort of complex processing taking place above

def any_success(data): # return True if at least one is successful
    at_least_one = False
    for item in data:
       at_least_one |= process(item)
    return at_least_one

>>> any_success([False, False, False])
False
>>> any_success([True, False, False])
True
>>> any_success([False, True, False])
True

基本上any没有短路:如果您需要处理每一项并记录至少一项成功等信息,则可能很有用。

另请参阅此答案中的警告

To give a use-case (after spending time with the other answers):

def process(item):
   return bool(item) # imagine some sort of complex processing taking place above

def any_success(data): # return True if at least one is successful
    at_least_one = False
    for item in data:
       at_least_one |= process(item)
    return at_least_one

>>> any_success([False, False, False])
False
>>> any_success([True, False, False])
True
>>> any_success([False, True, False])
True

Basically any without the short-circuiting: might be useful if you need to process every item and record at least one success etc.

See also the caveats in this answer


回答 7

在Python中,| =(ior)的作用类似于联合操作。就像x = 5和x | = 5一样,这两个值都将首先转换为二进制值,然后执行并运算,并得到答案5。

In Python,|=(ior) works like union operation. like if x=5 and x|=5 then both the value will first convert in binary value then the union operation will perform and we get the answer 5.


如何在Python中将字典转换为查询字符串?

问题:如何在Python中将字典转换为查询字符串?

使用后cgi.parse_qs(),如何将结果(字典)转换回查询字符串?寻找类似的东西 urllib.urlencode()

After using cgi.parse_qs(), how to convert the result (dictionary) back to query string? Looking for something similar to urllib.urlencode().


回答 0

Python 3

urllib.parse.urlencode(查询,doseq = False,[…])

映射对象或可能包含str或bytes对象的两个元素的元组序列转换百分比编码的ASCII文本字符串。

Python的3 urllib.parse文档

A dict是一个映射。

旧版Python

urllib.urlencodequery[,doseq])
映射对象或由两个元素组成的元组序列转换 “百分比编码”字符串…一系列key=value'&'字符分隔的对…

Python 2.7版urllib文档

Python 3

urllib.parse.urlencode(query, doseq=False, [...])

Convert a mapping object or a sequence of two-element tuples, which may contain str or bytes objects, to a percent-encoded ASCII text string.

Python 3 urllib.parse docs

A dict is a mapping.

Legacy Python

urllib.urlencode(query[, doseq])
Convert a mapping object or a sequence of two-element tuples to a “percent-encoded” string… a series of key=value pairs separated by '&' characters…

Python 2.7 urllib docs


回答 1

在python3中,略有不同:

from urllib.parse import urlencode
urlencode({'pram1': 'foo', 'param2': 'bar'})

输出: 'pram1=foo&param2=bar'

为了实现python2和python3的兼容性,请尝试以下操作:

try:
    #python2
    from urllib import urlencode
except ImportError:
    #python3
    from urllib.parse import urlencode

In python3, slightly different:

from urllib.parse import urlencode
urlencode({'pram1': 'foo', 'param2': 'bar'})

output: 'pram1=foo&param2=bar'

for python2 and python3 compatibility, try this:

try:
    #python2
    from urllib import urlencode
except ImportError:
    #python3
    from urllib.parse import urlencode

回答 2

您正在寻找完全一样的东西urllib.urlencode()

但是,当您调用parse_qs()(与区别parse_qsl())时,字典键是唯一的查询变量名称,而值是每个名称的值列表

为了将此信息传递到中urllib.urlencode(),您必须“展平”这些列表。这是使用元组列表理解的方法:

query_pairs = [(k,v) for k,vlist in d.iteritems() for v in vlist]
urllib.urlencode(query_pairs)

You’re looking for something exactly like urllib.urlencode()!

However, when you call parse_qs() (distinct from parse_qsl()), the dictionary keys are the unique query variable names and the values are lists of values for each name.

In order to pass this information into urllib.urlencode(), you must “flatten” these lists. Here is how you can do it with a list comprehenshion of tuples:

query_pairs = [(k,v) for k,vlist in d.iteritems() for v in vlist]
urllib.urlencode(query_pairs)

回答 3

也许您正在寻找这样的东西:

def dictToQuery(d):
  query = ''
  for key in d.keys():
    query += str(key) + '=' + str(d[key]) + "&"
  return query

它需要一个字典并将其转换为查询字符串,就像urlencode一样。它将在查询字符串后附加一个最终的“&”,但return query[:-1]如果有问题,则将其修复。

Maybe you’re looking for something like this:

def dictToQuery(d):
  query = ''
  for key in d.keys():
    query += str(key) + '=' + str(d[key]) + "&"
  return query

It takes a dictionary and convert it to a query string, just like urlencode. It’ll append a final “&” to the query string, but return query[:-1] fixes that, if it’s an issue.


pip安装中“ X的构建车轮失败”是什么意思?

问题:pip安装中“ X的构建车轮失败”是什么意思?

在SO上,这是一个真正流行的问题,但是我看过的所有答案中,没有一个能清楚地说明此错误的真正含义以及发生的原因。

造成混乱的原因之一是,当您(例如)这样做时pip install pycparser,您首先会得到以下错误:

Failed building wheel for pycparser

然后出现以下消息,说明该软件包是:

Successfully installed pycparser-2.19


# pip3 install pycparser

Collecting pycparser
  Using cached https://files.pythonhosted.org/packages/68/9e/49196946aee219aead1290e00d1e7fdeab8567783e83e1b9ab5585e6206a/pycparser-2.19.tar.gz
Building wheels for collected packages: pycparser
  Running setup.py bdist_wheel for pycparser ... error
  Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-g_v28hpp/pycparser/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/pip-wheel-__w_f6p0 --python-tag cp36:
  Traceback (most recent call last):
    File "<string>", line 1, in <module>
    ...
    File "/usr/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2349, in resolve
      module = __import__(self.module_name, fromlist=['__name__'], level=0)
  ModuleNotFoundError: No module named 'wheel.bdist_wheel'

  ----------------------------------------
  Failed building wheel for pycparser
  Running setup.py clean for pycparser
Failed to build pycparser
Installing collected packages: pycparser
  Running setup.py install for pycparser ... done
Successfully installed pycparser-2.19

这里发生了什么?

(我想了解如何发生某些故障但仍然可以安装,以及是否可以信任此软件包正常运行?)

到目前为止,我已经找到了最好的部分原因是这个

This is a truly popular question here at SO, but none of the many answers I have looked at, clearly explain what this error really mean, and why it occurs.

One source of confusion, is that when (for example) you do pip install pycparser, you first get the error:

Failed building wheel for pycparser

which is then followed by the message that the package was:

Successfully installed pycparser-2.19.


# pip3 install pycparser

Collecting pycparser
  Using cached https://files.pythonhosted.org/packages/68/9e/49196946aee219aead1290e00d1e7fdeab8567783e83e1b9ab5585e6206a/pycparser-2.19.tar.gz
Building wheels for collected packages: pycparser
  Running setup.py bdist_wheel for pycparser ... error
  Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-g_v28hpp/pycparser/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/pip-wheel-__w_f6p0 --python-tag cp36:
  Traceback (most recent call last):
    File "<string>", line 1, in <module>
    ...
    File "/usr/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2349, in resolve
      module = __import__(self.module_name, fromlist=['__name__'], level=0)
  ModuleNotFoundError: No module named 'wheel.bdist_wheel'

  ----------------------------------------
  Failed building wheel for pycparser
  Running setup.py clean for pycparser
Failed to build pycparser
Installing collected packages: pycparser
  Running setup.py install for pycparser ... done
Successfully installed pycparser-2.19

What is going on here?

(I would like to understand how something can fail but still get installed and whether you can trust this package functioning correctly?)

So far the best partial explanation I have found is this.


回答 0

(这里是点子维护者!)

如果包装不是车轮,则pip尝试为其构建一个车轮(通过setup.py bdist_wheel)。如果由于任何原因导致安装失败,您将收到“ pycparser的建筑轮子故障”消息,并且pip退回到直接安装(通过setup.py install)。

装好轮子后,pip可以通过正确打开包装来安装轮子。pip尝试尽可能多地通过轮子安装软件包。这是由于使用滚轮的各种优势(例如更快的安装,可缓存的,不再执行代码等)。


您在这里的错误消息是由于wheel缺少软件包,其中包含构建轮子所需的逻辑setup.py bdist_wheel。(pip install wheel可以解决该问题。)


以上是当前默认的旧行为;我们会默认在将来的某个时间切换到PEP 517,为此我们将转到基于标准的流程。我们还为此提供了隔离的版本,因此,默认情况下,您会在这些环境中安装滚轮。:)

(pip maintainer here!)

If the package is not a wheel, pip tries to build a wheel for it (via setup.py bdist_wheel). If that fails for any reason, you get the “Failed building wheel for pycparser” message and pip falls back to installing directly (via setup.py install).

Once we have a wheel, pip can install the wheel by unpacking it correctly. pip tries to install packages via wheels as often as it can. This is because of various advantages of using wheels (like faster installs, cache-able, not executing code again etc).


Your error message here is due to the wheel package being missing, which contains the logic required to build the wheels in setup.py bdist_wheel. (pip install wheel can fix that.)


The above is the legacy behavior that is currently the default; we’ll switch to PEP 517 by default, sometime in the future, moving us to a standards-based process for this. We also have isolated builds for that so, you’d have wheel installed in those environments by default. :)


回答 1

昨天,我遇到了同样的错误: Failed building wheel for hddfancontrol跑步时pip3 install hddfancontrol。结果是Failed to build hddfancontrol。原因是error: invalid command 'bdist_wheel'Running setup.py bdist_wheel for hddfancontrol ... error。通过运行以下命令修复了该错误:

 pip3 install wheel

(从这里开始。)

另外,也可以从此处直接下载“ wheel” 。下载后,可以通过运行以下命令进行安装:

pip3 install "/the/file_path/to/wheel-0.32.3-py2.py3-none-any.whl"

Yesterday, I got the same error: Failed building wheel for hddfancontrol when I ran pip3 install hddfancontrol. The result was Failed to build hddfancontrol. The cause was error: invalid command 'bdist_wheel' and Running setup.py bdist_wheel for hddfancontrol ... error. The error was fixed by running the following:

 pip3 install wheel

(From here.)

Alternatively, the “wheel” can be downloaded directly from here. When downloaded, it can be installed by running the following:

pip3 install "/the/file_path/to/wheel-0.32.3-py2.py3-none-any.whl"

回答 2

从那以后,似乎没有人提到我自己。我自己解决上述问题是最常见的,以确保禁用缓存使用副本:pip install <package> --no-cache-dir

Since, nobody seem to mention this apart myself. My own solution to the above problem is most often to make sure to disable the cached copy by using: pip install <package> --no-cache-dir.


回答 3

从程序包部署的角度解决此问题可能会有所帮助。

那里有许多教程介绍了如何将程序包发布到PyPi。以下是我使用过的几对;

中型
真正的python

我的经验是,这些教程大多数都只使用源代码的.tar,而不使用转盘。因此,在安装使用这些教程创建的软件包时,我收到了“无法构建轮子”错误。

后来我在PyPi上找到了指向Python软件基金会的文档PSF Docs的链接。我发现它们的设置和构建过程略有不同,并且确实包括构建wheel文件。

使用正式记录的方法后,安装软件包时不再收到错误。

因此,错误可能只是开发人员如何打包和部署项目的问题。我们当中没有一个人天生就知道如何使用PyPi,如果它们是在错误的教程上发生的,那么您可以填补空白。

我敢肯定这不是错误的唯一原因,但我敢打赌这是主要原因。

It might be helpful to address this question from a package deployment perspective.

There are many tutorials out there that explain how to publish a package to PyPi. Below are a couple I have used;

medium
real python

My experience is that most of these tutorials only have you use the .tar of the source, not a wheel. Thus, when installing packages created using these tutorials, I’ve received the “Failed to build wheel” error.

I later found the link on PyPi to the Python Software Foundation’s docs PSF Docs. I discovered that their setup and build process is slightly different, and does indeed included building a wheel file.

After using the officially documented method, I no longer received the error when installing my packages.

So, the error might simply be a matter of how the developer packaged and deployed the project. None of us were born knowing how to use PyPi, and if they happened upon the wrong tutorial — well, you can fill in the blanks.

I’m sure that is not the only reason for the error, but I’m willing to bet that is a major reason for it.


回答 4

试试这个:

sudo apt-get install libpcap-dev libpq-dev

当我安装了这两个时,它对我有用。

有关更多信息,请参见此处的链接。

Try this:

sudo apt-get install libpcap-dev libpq-dev

It has worked for me when I have installed these two.

See the link here for more information


回答 5

在Ubuntu 18.04上,我遇到了此问题,因为的apt软件包wheel不包含wheel命令。我认为pip尝试导入wheelpython包,如果成功,则假定wheel命令也可用。Ubuntu打破了这一假设。

apt python3代码包的名称为python3-wheel。这是自动安装的,因为python3-pip建议您这样做。

apt python3 wheel命令包的名称为python-wheel-common。安装此工具也可以为我解决“建筑轮子故障”错误。

On Ubuntu 18.04, I ran into this issue because the apt package for wheel does not include the wheel command. I think pip tries to import the wheel python package, and if that succeeds assumes that the wheel command is also available. Ubuntu breaks that assumption.

The apt python3 code package is named python3-wheel. This is installed automatically because python3-pip recommends it.

The apt python3 wheel command package is named python-wheel-common. Installing this too fixes the “failed building wheel” errors for me.


回答 6

当我尝试安装pip install django-imagekit时,出现了相同的消息。因此,我运行了pip install wheel(我安装了python 2.7),然后重新运行了pip install django-imagekit,它开始工作了。谢谢

I got the same message when I tried to install pip install django-imagekit. So I ran pip install wheel (I had python 2.7) and then I reran pip install django-imagekit and it worked. Thanks


回答 7

我想补充一点,如果您的系统上只有Python3,则需要开始使用pip3而不是pip。

您可以使用以下命令安装pip3;

sudo apt install python3-pip -y

之后,您可以尝试安装所需的软件包。

sudo pip3 install <package>

I would like to add that if you only have Python3 on your system then you need to start using pip3 instead of pip.

You can install pip3 using the following command;

sudo apt install python3-pip -y

After this you can try to install the package you need with;

sudo pip3 install <package>

回答 8

错误:

系统:AWS EC2实例(T2小)

问题:通过安装opencv python时

pip3 install opencv-python

  Problem with the CMake installation, aborting build. CMake executable is cmake
  
  ----------------------------------------
  Failed building wheel for opencv-python
  Running setup.py clean for opencv-python

什么对我有用

pip3 install --upgrade pip setuptools wheel

在此之后,您仍然可能会收到休闲错误错误

    from .cv2 import *
ImportError: libGL.so.1: cannot open shared object file: No such file or directory

安装libgl为我解决了错误。

sudo apt update
sudo apt install libgl1-mesa-glx

希望这可以帮助

Error :

System : aws ec2 instance (t2 small)

issue : while installing opencv python via

pip3 install opencv-python

  Problem with the CMake installation, aborting build. CMake executable is cmake
  
  ----------------------------------------
  Failed building wheel for opencv-python
  Running setup.py clean for opencv-python

What worked for me

pip3 install --upgrade pip setuptools wheel

After this you still might received fallowing error error

    from .cv2 import *
ImportError: libGL.so.1: cannot open shared object file: No such file or directory

Installing libgl solved the error for me.

sudo apt update
sudo apt install libgl1-mesa-glx

Hope this helps


回答 9

安装Brotli时遇到相同的问题

错误

Failed building wheel for Brotli

我通过.whl此处下载文件 并使用以下命令进行安装来解决了该问题

C:\Users\{user_name}\Downloads>pip install Brotli-1.0.9-cp39-cp39-win_amd64.whl

I had the same problem while installing Brotli

ERROR

Failed building wheel for Brotli

I solved it by downloading the .whl file from here and installing it using the below command

C:\Users\{user_name}\Downloads>pip install Brotli-1.0.9-cp39-cp39-win_amd64.whl

回答 10

这可能对您有帮助!….

卸载pycparser:

pip uninstall pycparser

重新安装pycparser:

pip install pycparser

我在安装termcolor时遇到了同样的错误,并通过重新安装进行了修复。

This may Help you ! ….

Uninstalling pycparser:

pip uninstall pycparser

Reinstall pycparser:

pip install pycparser

I got same error while installing termcolor and I fixed it by reinstalling it .


使用Pandas Data Frame运行OLS回归

问题:使用Pandas Data Frame运行OLS回归

我有一个pandas数据框,我希望能够从B和C列中的值预测A列的值。这是一个玩具示例:

import pandas as pd
df = pd.DataFrame({"A": [10,20,30,40,50], 
                   "B": [20, 30, 10, 40, 50], 
                   "C": [32, 234, 23, 23, 42523]})

理想情况下,我会有类似的东西,ols(A ~ B + C, data = df)但是当我查看算法库中的示例时,看起来好像scikit-learn是用行而不是列的列表将数据提供给模型。这将要求我将数据重新格式化为列表内的列表,这似乎首先使使用熊猫的目的遭到了破坏。在熊猫数据框中的数据上运行OLS回归(或更通用的任何机器学习算法)的最有效方法是什么?

I have a pandas data frame and I would like to able to predict the values of column A from the values in columns B and C. Here is a toy example:

import pandas as pd
df = pd.DataFrame({"A": [10,20,30,40,50], 
                   "B": [20, 30, 10, 40, 50], 
                   "C": [32, 234, 23, 23, 42523]})

Ideally, I would have something like ols(A ~ B + C, data = df) but when I look at the examples from algorithm libraries like scikit-learn it appears to feed the data to the model with a list of rows instead of columns. This would require me to reformat the data into lists inside lists, which seems to defeat the purpose of using pandas in the first place. What is the most pythonic way to run an OLS regression (or any machine learning algorithm more generally) on data in a pandas data frame?


回答 0

我认为您可以使用statsmodels包几乎完成您认为理想的事情,该包是0.20.0版pandas之前的“可选依赖项pandas”之一(在中有一些用途pandas.stats。)

>>> import pandas as pd
>>> import statsmodels.formula.api as sm
>>> df = pd.DataFrame({"A": [10,20,30,40,50], "B": [20, 30, 10, 40, 50], "C": [32, 234, 23, 23, 42523]})
>>> result = sm.ols(formula="A ~ B + C", data=df).fit()
>>> print(result.params)
Intercept    14.952480
B             0.401182
C             0.000352
dtype: float64
>>> print(result.summary())
                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      A   R-squared:                       0.579
Model:                            OLS   Adj. R-squared:                  0.158
Method:                 Least Squares   F-statistic:                     1.375
Date:                Thu, 14 Nov 2013   Prob (F-statistic):              0.421
Time:                        20:04:30   Log-Likelihood:                -18.178
No. Observations:                   5   AIC:                             42.36
Df Residuals:                       2   BIC:                             41.19
Df Model:                           2                                         
==============================================================================
                 coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
Intercept     14.9525     17.764      0.842      0.489       -61.481    91.386
B              0.4012      0.650      0.617      0.600        -2.394     3.197
C              0.0004      0.001      0.650      0.583        -0.002     0.003
==============================================================================
Omnibus:                          nan   Durbin-Watson:                   1.061
Prob(Omnibus):                    nan   Jarque-Bera (JB):                0.498
Skew:                          -0.123   Prob(JB):                        0.780
Kurtosis:                       1.474   Cond. No.                     5.21e+04
==============================================================================

Warnings:
[1] The condition number is large, 5.21e+04. This might indicate that there are
strong multicollinearity or other numerical problems.

I think you can almost do exactly what you thought would be ideal, using the statsmodels package which was one of pandas‘ optional dependencies before pandas‘ version 0.20.0 (it was used for a few things in pandas.stats.)

>>> import pandas as pd
>>> import statsmodels.formula.api as sm
>>> df = pd.DataFrame({"A": [10,20,30,40,50], "B": [20, 30, 10, 40, 50], "C": [32, 234, 23, 23, 42523]})
>>> result = sm.ols(formula="A ~ B + C", data=df).fit()
>>> print(result.params)
Intercept    14.952480
B             0.401182
C             0.000352
dtype: float64
>>> print(result.summary())
                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      A   R-squared:                       0.579
Model:                            OLS   Adj. R-squared:                  0.158
Method:                 Least Squares   F-statistic:                     1.375
Date:                Thu, 14 Nov 2013   Prob (F-statistic):              0.421
Time:                        20:04:30   Log-Likelihood:                -18.178
No. Observations:                   5   AIC:                             42.36
Df Residuals:                       2   BIC:                             41.19
Df Model:                           2                                         
==============================================================================
                 coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
Intercept     14.9525     17.764      0.842      0.489       -61.481    91.386
B              0.4012      0.650      0.617      0.600        -2.394     3.197
C              0.0004      0.001      0.650      0.583        -0.002     0.003
==============================================================================
Omnibus:                          nan   Durbin-Watson:                   1.061
Prob(Omnibus):                    nan   Jarque-Bera (JB):                0.498
Skew:                          -0.123   Prob(JB):                        0.780
Kurtosis:                       1.474   Cond. No.                     5.21e+04
==============================================================================

Warnings:
[1] The condition number is large, 5.21e+04. This might indicate that there are
strong multicollinearity or other numerical problems.

回答 1

注意: pandas.stats 已被 0.20.0 删除


可以使用pandas.stats.ols

>>> from pandas.stats.api import ols
>>> df = pd.DataFrame({"A": [10,20,30,40,50], "B": [20, 30, 10, 40, 50], "C": [32, 234, 23, 23, 42523]})
>>> res = ols(y=df['A'], x=df[['B','C']])
>>> res
-------------------------Summary of Regression Analysis-------------------------

Formula: Y ~ <B> + <C> + <intercept>

Number of Observations:         5
Number of Degrees of Freedom:   3

R-squared:         0.5789
Adj R-squared:     0.1577

Rmse:             14.5108

F-stat (2, 2):     1.3746, p-value:     0.4211

Degrees of Freedom: model 2, resid 2

-----------------------Summary of Estimated Coefficients------------------------
      Variable       Coef    Std Err     t-stat    p-value    CI 2.5%   CI 97.5%
--------------------------------------------------------------------------------
             B     0.4012     0.6497       0.62     0.5999    -0.8723     1.6746
             C     0.0004     0.0005       0.65     0.5826    -0.0007     0.0014
     intercept    14.9525    17.7643       0.84     0.4886   -19.8655    49.7705
---------------------------------End of Summary---------------------------------

请注意,您需要statsmodels安装软件包,该软件包在内部使用pandas.stats.ols

Note: pandas.stats has been removed with 0.20.0


It’s possible to do this with pandas.stats.ols:

>>> from pandas.stats.api import ols
>>> df = pd.DataFrame({"A": [10,20,30,40,50], "B": [20, 30, 10, 40, 50], "C": [32, 234, 23, 23, 42523]})
>>> res = ols(y=df['A'], x=df[['B','C']])
>>> res
-------------------------Summary of Regression Analysis-------------------------

Formula: Y ~ <B> + <C> + <intercept>

Number of Observations:         5
Number of Degrees of Freedom:   3

R-squared:         0.5789
Adj R-squared:     0.1577

Rmse:             14.5108

F-stat (2, 2):     1.3746, p-value:     0.4211

Degrees of Freedom: model 2, resid 2

-----------------------Summary of Estimated Coefficients------------------------
      Variable       Coef    Std Err     t-stat    p-value    CI 2.5%   CI 97.5%
--------------------------------------------------------------------------------
             B     0.4012     0.6497       0.62     0.5999    -0.8723     1.6746
             C     0.0004     0.0005       0.65     0.5826    -0.0007     0.0014
     intercept    14.9525    17.7643       0.84     0.4886   -19.8655    49.7705
---------------------------------End of Summary---------------------------------

Note that you need to have statsmodels package installed, it is used internally by the pandas.stats.ols function.


回答 2

我不知道这是否是新的sklearn还是pandas,但我能直接传递数据帧sklearn没有数据帧转换为numpy的阵列或任何其它数据类型。

from sklearn import linear_model

reg = linear_model.LinearRegression()
reg.fit(df[['B', 'C']], df['A'])

>>> reg.coef_
array([  4.01182386e-01,   3.51587361e-04])

I don’t know if this is new in sklearn or pandas, but I’m able to pass the data frame directly to sklearn without converting the data frame to a numpy array or any other data types.

from sklearn import linear_model

reg = linear_model.LinearRegression()
reg.fit(df[['B', 'C']], df['A'])

>>> reg.coef_
array([  4.01182386e-01,   3.51587361e-04])

回答 3

这将要求我将数据重新格式化为列表内的列表,这似乎首先使使用熊猫的目的无法实现。

不,不是,只是转换为NumPy数组:

>>> data = np.asarray(df)

这会花费固定的时间,因为它只会创建数据视图。然后将其提供给scikit-learn:

>>> from sklearn.linear_model import LinearRegression
>>> lr = LinearRegression()
>>> X, y = data[:, 1:], data[:, 0]
>>> lr.fit(X, y)
LinearRegression(copy_X=True, fit_intercept=True, normalize=False)
>>> lr.coef_
array([  4.01182386e-01,   3.51587361e-04])
>>> lr.intercept_
14.952479503953672

This would require me to reformat the data into lists inside lists, which seems to defeat the purpose of using pandas in the first place.

No it doesn’t, just convert to a NumPy array:

>>> data = np.asarray(df)

This takes constant time because it just creates a view on your data. Then feed it to scikit-learn:

>>> from sklearn.linear_model import LinearRegression
>>> lr = LinearRegression()
>>> X, y = data[:, 1:], data[:, 0]
>>> lr.fit(X, y)
LinearRegression(copy_X=True, fit_intercept=True, normalize=False)
>>> lr.coef_
array([  4.01182386e-01,   3.51587361e-04])
>>> lr.intercept_
14.952479503953672

回答 4

Statsmodels可使用直接引用熊猫数据框的列引用来构建OLS模型

简短而甜美:

model = sm.OLS(df[y], df[x]).fit()


代码详细信息和回归摘要:

# imports
import pandas as pd
import statsmodels.api as sm
import numpy as np

# data
np.random.seed(123)
df = pd.DataFrame(np.random.randint(0,100,size=(100, 3)), columns=list('ABC'))

# assign dependent and independent / explanatory variables
variables = list(df.columns)
y = 'A'
x = [var for var in variables if var not in y ]

# Ordinary least squares regression
model_Simple = sm.OLS(df[y], df[x]).fit()

# Add a constant term like so:
model = sm.OLS(df[y], sm.add_constant(df[x])).fit()

model.summary()

输出:

                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      A   R-squared:                       0.019
Model:                            OLS   Adj. R-squared:                 -0.001
Method:                 Least Squares   F-statistic:                    0.9409
Date:                Thu, 14 Feb 2019   Prob (F-statistic):              0.394
Time:                        08:35:04   Log-Likelihood:                -484.49
No. Observations:                 100   AIC:                             975.0
Df Residuals:                      97   BIC:                             982.8
Df Model:                           2                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const         43.4801      8.809      4.936      0.000      25.996      60.964
B              0.1241      0.105      1.188      0.238      -0.083       0.332
C             -0.0752      0.110     -0.681      0.497      -0.294       0.144
==============================================================================
Omnibus:                       50.990   Durbin-Watson:                   2.013
Prob(Omnibus):                  0.000   Jarque-Bera (JB):                6.905
Skew:                           0.032   Prob(JB):                       0.0317
Kurtosis:                       1.714   Cond. No.                         231.
==============================================================================

如何直接获得R平方,系数和p值:

# commands:
model.params
model.pvalues
model.rsquared

# demo:
In[1]: 
model.params
Out[1]:
const    43.480106
B         0.124130
C        -0.075156
dtype: float64

In[2]: 
model.pvalues
Out[2]: 
const    0.000003
B        0.237924
C        0.497400
dtype: float64

Out[3]:
model.rsquared
Out[2]:
0.0190

Statsmodels kan build an OLS model with column references directly to a pandas dataframe.

Short and sweet:

model = sm.OLS(df[y], df[x]).fit()


Code details and regression summary:

# imports
import pandas as pd
import statsmodels.api as sm
import numpy as np

# data
np.random.seed(123)
df = pd.DataFrame(np.random.randint(0,100,size=(100, 3)), columns=list('ABC'))

# assign dependent and independent / explanatory variables
variables = list(df.columns)
y = 'A'
x = [var for var in variables if var not in y ]

# Ordinary least squares regression
model_Simple = sm.OLS(df[y], df[x]).fit()

# Add a constant term like so:
model = sm.OLS(df[y], sm.add_constant(df[x])).fit()

model.summary()

Output:

                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      A   R-squared:                       0.019
Model:                            OLS   Adj. R-squared:                 -0.001
Method:                 Least Squares   F-statistic:                    0.9409
Date:                Thu, 14 Feb 2019   Prob (F-statistic):              0.394
Time:                        08:35:04   Log-Likelihood:                -484.49
No. Observations:                 100   AIC:                             975.0
Df Residuals:                      97   BIC:                             982.8
Df Model:                           2                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const         43.4801      8.809      4.936      0.000      25.996      60.964
B              0.1241      0.105      1.188      0.238      -0.083       0.332
C             -0.0752      0.110     -0.681      0.497      -0.294       0.144
==============================================================================
Omnibus:                       50.990   Durbin-Watson:                   2.013
Prob(Omnibus):                  0.000   Jarque-Bera (JB):                6.905
Skew:                           0.032   Prob(JB):                       0.0317
Kurtosis:                       1.714   Cond. No.                         231.
==============================================================================

How to directly get R-squared, Coefficients and p-value:

# commands:
model.params
model.pvalues
model.rsquared

# demo:
In[1]: 
model.params
Out[1]:
const    43.480106
B         0.124130
C        -0.075156
dtype: float64

In[2]: 
model.pvalues
Out[2]: 
const    0.000003
B        0.237924
C        0.497400
dtype: float64

Out[3]:
model.rsquared
Out[2]:
0.0190

使用virtualenvwrapper重命名环境

问题:使用virtualenvwrapper重命名环境

我有一个名为的环境doors,我想将其重命名djangovirtualenvwrapper

我注意到,如果仅将文件夹重命名~/.virtualenvs/doorsdjango,我现在可以呼叫workon django,但是环境仍然提示(doors)hobbes3@hobbes3

I have an environment called doors and I would like to rename it to django for the virtualenvwrapper.

I’ve noticed that if I just rename the folder ~/.virtualenvs/doors to django, I can now call workon django, but the environment still says (doors)hobbes3@hobbes3.


回答 0

您可以使用:

cpvirtualenv oldenv newenv
rmvirtualenv oldenv

因此,在您的情况下:

cpvirtualenv doors django
rmvirtualenv doors

You can use:

cpvirtualenv oldenv newenv
rmvirtualenv oldenv

So in your case:

cpvirtualenv doors django
rmvirtualenv doors

回答 1

如果您这样做:

$ ack-grep -ai doors ~/.virtualenvs/django/bin

您会注意到,该文件将doors作为位置而不是django,您将使用新位置更改每个文件。

解决方案:重命名文件夹后,执行以下命令。

$ sed -i "s/doors/django/g" ~/.virtualenvs/django/bin/*

现在,如果您这样做:

$ workon django
(django)hobbes3@hobbes3

if you do:

$ ack-grep -ai doors ~/.virtualenvs/django/bin

you’ll notice that will have doors as location and not django, you’ll to change each file with the new location.

solution: after renamed the folder execute the command below.

$ sed -i "s/doors/django/g" ~/.virtualenvs/django/bin/*

now if you do:

$ workon django
(django)hobbes3@hobbes3

python中的复数用法

问题:python中的复数用法

我是数学新手。现在,我将更深入地了解Python数据类型。我不明白如何使用复数。请给我示例在Python中使用复数的示例。

I’m a math newbie. Now I’m getting deeper into Python data types. I can’t understand how to use a complex number. Please give me examples of usage of complex numbers in Python.


回答 0

在python中,您可以在数字后面加上’j’或’J’以使其虚构,因此您可以轻松地编写复杂的文字:

>>> 1j
1j
>>> 1J
1j
>>> 1j * 1j
(-1+0j)

后缀“ j”来自电气工程,其中变量“ i”通常用于电流。(推理在这里找到。

复数的类型为complex,并且您可以根据需要将其用作构造函数:

>>> complex(2,3)
(2+3j)

复数具有一些内置访问器:

>>> z = 2+3j
>>> z.real
2.0
>>> z.imag
3.0
>>> z.conjugate()
(2-3j)

几个内置函数支持复数:

>>> abs(3 + 4j)
5.0
>>> pow(3 + 4j, 2)
(-7+24j)

标准模块cmath具有更多处理复数的功能:

>>> import cmath
>>> cmath.sin(2 + 3j)
(9.15449914691143-4.168906959966565j)

In python, you can put ‘j’ or ‘J’ after a number to make it imaginary, so you can write complex literals easily:

>>> 1j
1j
>>> 1J
1j
>>> 1j * 1j
(-1+0j)

The ‘j’ suffix comes from electrical engineering, where the variable ‘i’ is usually used for current. (Reasoning found here.)

The type of a complex number is complex, and you can use the type as a constructor if you prefer:

>>> complex(2,3)
(2+3j)

A complex number has some built-in accessors:

>>> z = 2+3j
>>> z.real
2.0
>>> z.imag
3.0
>>> z.conjugate()
(2-3j)

Several built-in functions support complex numbers:

>>> abs(3 + 4j)
5.0
>>> pow(3 + 4j, 2)
(-7+24j)

The standard module cmath has more functions that handle complex numbers:

>>> import cmath
>>> cmath.sin(2 + 3j)
(9.15449914691143-4.168906959966565j)

回答 1

下面的复数示例应易于说明,最后包括错误消息

>>> x=complex(1,2)
>>> print x
(1+2j)
>>> y=complex(3,4)
>>> print y
(3+4j)
>>> z=x+y
>>> print x
(1+2j)
>>> print z
(4+6j)
>>> z=x*y
>>> print z
(-5+10j)
>>> z=x/y
>>> print z
(0.44+0.08j)
>>> print x.conjugate()
(1-2j)
>>> print x.imag
2.0
>>> print x.real
1.0
>>> print x>y

Traceback (most recent call last):
  File "<pyshell#149>", line 1, in <module>
    print x>y
TypeError: no ordering relation is defined for complex numbers
>>> print x==y
False
>>> 

The following example for complex numbers should be self explanatory including the error message at the end

>>> x=complex(1,2)
>>> print x
(1+2j)
>>> y=complex(3,4)
>>> print y
(3+4j)
>>> z=x+y
>>> print x
(1+2j)
>>> print z
(4+6j)
>>> z=x*y
>>> print z
(-5+10j)
>>> z=x/y
>>> print z
(0.44+0.08j)
>>> print x.conjugate()
(1-2j)
>>> print x.imag
2.0
>>> print x.real
1.0
>>> print x>y

Traceback (most recent call last):
  File "<pyshell#149>", line 1, in <module>
    print x>y
TypeError: no ordering relation is defined for complex numbers
>>> print x==y
False
>>> 

为什么Python 3允许“ 00”作为0的文字,却不允许“ 01”作为1的文字?

问题:为什么Python 3允许“ 00”作为0的文字,却不允许“ 01”作为1的文字?

为什么Python 3允许“ 00”作为原义的0,却不允许“ 01”作为原义的1?有充分的理由吗?这种矛盾使我感到困惑。(我们正在谈论的是Python 3,它故意打破了向后兼容性以实现诸如一致性之类的目标。)

例如:

>>> from datetime import time
>>> time(16, 00)
datetime.time(16, 0)
>>> time(16, 01)
  File "<stdin>", line 1
    time(16, 01)
              ^
SyntaxError: invalid token
>>>

Why does Python 3 allow “00” as a literal for 0 but not allow “01” as a literal for 1? Is there a good reason? This inconsistency baffles me. (And we’re talking about Python 3, which purposely broke backward compatibility in order to achieve goals like consistency.)

For example:

>>> from datetime import time
>>> time(16, 00)
datetime.time(16, 0)
>>> time(16, 01)
  File "<stdin>", line 1
    time(16, 01)
              ^
SyntaxError: invalid token
>>>

回答 0

根据https://docs.python.org/3/reference/lexical_analysis.html#integer-literals

整数文字由以下词汇定义描述:

integer        ::=  decimalinteger | octinteger | hexinteger | bininteger
decimalinteger ::=  nonzerodigit digit* | "0"+
nonzerodigit   ::=  "1"..."9"
digit          ::=  "0"..."9"
octinteger     ::=  "0" ("o" | "O") octdigit+
hexinteger     ::=  "0" ("x" | "X") hexdigit+
bininteger     ::=  "0" ("b" | "B") bindigit+
octdigit       ::=  "0"..."7"
hexdigit       ::=  digit | "a"..."f" | "A"..."F"
bindigit       ::=  "0" | "1"

除了可以存储在可用内存中的整数之外,整数文字的长度没有限制。

请注意,不允许使用非零十进制数字开头的零。这是为了消除C样式八进制文字的歧义,Python在3.0版之前使用了这些样式。

如此处所述,不允许使用非零十进制数字开头的零"0"+作为一个非常特殊的情况是合法的,这在Python 2中是不存在的

integer        ::=  decimalinteger | octinteger | hexinteger | bininteger
decimalinteger ::=  nonzerodigit digit* | "0"
octinteger     ::=  "0" ("o" | "O") octdigit+ | "0" octdigit+

SVN commit r55866在令牌生成器中实现了PEP 3127,它禁止使用旧0<octal>数字。但是,奇怪的是,它也添加了以下注释:

/* in any case, allow '0' as a literal */

带有nonzeroSyntaxError在以下数字序列包含非零数字时抛出的特殊标志。

这很奇怪,因为PEP 3127不允许这种情况:

该PEP建议,将使用Python 3.0(和2.6的Python 3.0预览模式)从语言中删除使用前导零指定八进制数的功能,并且每当前导“ 0”为紧跟着另一个数字

(强调我的)

因此,允许多个零的事实在技术上违反了PEP,并且基本上由Georg Brandl实施为特殊情况。他进行了相应的文档更改,以注意这"0"+是的有效案例decimalinteger(以前已在中进行了介绍octinteger)。

我们可能永远不会确切知道为什么Georg选择使之"0"+有效-在Python中它可能永远是一个奇怪的情况。


更新 [2015年7月28日]:这个问题引发了关于python-ideas 的热烈讨论Georg在其中进行了讨论

史蒂文·达普拉诺(Steven D’Aprano)写道:

为什么这样定义?[…]为什么我们写0000以得到零?

我可以告诉你,但后来我不得不杀了你。

格奥尔格

后来,该线程生成了此错误报告,旨在摆脱这种特殊情况。乔治在这里

我不记得有意进行更改的原因(从文档更改中可以看出)。

我现在无法提出更改的充分理由[…]

因此,我们有了它:这种不一致背后的确切原因已不复存在。

最后,请注意,该错误报告已被拒绝:对于Python 3.x的其余部分,前导零将仅在零整数上继续被接受。

Per https://docs.python.org/3/reference/lexical_analysis.html#integer-literals:

Integer literals are described by the following lexical definitions:

integer        ::=  decimalinteger | octinteger | hexinteger | bininteger
decimalinteger ::=  nonzerodigit digit* | "0"+
nonzerodigit   ::=  "1"..."9"
digit          ::=  "0"..."9"
octinteger     ::=  "0" ("o" | "O") octdigit+
hexinteger     ::=  "0" ("x" | "X") hexdigit+
bininteger     ::=  "0" ("b" | "B") bindigit+
octdigit       ::=  "0"..."7"
hexdigit       ::=  digit | "a"..."f" | "A"..."F"
bindigit       ::=  "0" | "1"

There is no limit for the length of integer literals apart from what can be stored in available memory.

Note that leading zeros in a non-zero decimal number are not allowed. This is for disambiguation with C-style octal literals, which Python used before version 3.0.

As noted here, leading zeros in a non-zero decimal number are not allowed. "0"+ is legal as a very special case, which wasn’t present in Python 2:

integer        ::=  decimalinteger | octinteger | hexinteger | bininteger
decimalinteger ::=  nonzerodigit digit* | "0"
octinteger     ::=  "0" ("o" | "O") octdigit+ | "0" octdigit+

SVN commit r55866 implemented PEP 3127 in the tokenizer, which forbids the old 0<octal> numbers. However, curiously, it also adds this note:

/* in any case, allow '0' as a literal */

with a special nonzero flag that only throws a SyntaxError if the following sequence of digits contains a nonzero digit.

This is odd because PEP 3127 does not allow this case:

This PEP proposes that the ability to specify an octal number by using a leading zero will be removed from the language in Python 3.0 (and the Python 3.0 preview mode of 2.6), and that a SyntaxError will be raised whenever a leading “0” is immediately followed by another digit.

(emphasis mine)

So, the fact that multiple zeros are allowed is technically violating the PEP, and was basically implemented as a special case by Georg Brandl. He made the corresponding documentation change to note that "0"+ was a valid case for decimalinteger (previously that had been covered under octinteger).

We’ll probably never know exactly why Georg chose to make "0"+ valid – it may forever remain an odd corner case in Python.


UPDATE [28 Jul 2015]: This question led to a lively discussion thread on python-ideas in which Georg chimed in:

Steven D’Aprano wrote:

Why was it defined that way? […] Why would we write 0000 to get zero?

I could tell you, but then I’d have to kill you.

Georg

Later on, the thread spawned this bug report aiming to get rid of this special case. Here, Georg says:

I don’t recall the reason for this deliberate change (as seen from the docs change).

I’m unable to come up with a good reason for this change now […]

and thus we have it: the precise reason behind this inconsistency is lost to time.

Finally, note that the bug report was rejected: leading zeros will continue to be accepted only on zero integers for the rest of Python 3.x.


回答 1

这是特例("0"+

2.4.4。整数文字

整数文字由以下词汇定义描述:

整数:: =十进制整数| 八进制| hexinteger | 二进制整数
十进制整数:: =非零数字* “ 0” +
非零数字:: =“ 1” ...“ 9”
数字:: =“ 0” ...“ 9”
八位整数:: =“ 0”(“ o” |“ O”)八位数字+
hexinteger :: =“ 0”(“ x” |“ X”)十六进制+
bininteger :: =“ 0”(“ b” |“ B”)bindigit +
八位数字:: =“ 0” ...“ 7”
十六进制::: digit | “ a” ...“ f” | “ A” ...“ F”
bindigit :: =“ 0” | “ 1”

如果您查看语法,则很容易看到0需要特殊情况。我不确定为什么在+那里需要’ ‘。是时候浏览一下开发邮件列表了…


有趣的是,在Python2中,有多个0解析为octinteger(最终结果仍然0是)

十进制整数:: =非零数字* “ 0”
八位整数:: =“ 0”(“ o” |“ O”)八位数字+ | “ 0”八位数字+

It’s a special case ("0"+)

2.4.4. Integer literals

Integer literals are described by the following lexical definitions:

integer        ::=  decimalinteger | octinteger | hexinteger | bininteger
decimalinteger ::=  nonzerodigit digit* | "0"+
nonzerodigit   ::=  "1"..."9"
digit          ::=  "0"..."9"
octinteger     ::=  "0" ("o" | "O") octdigit+
hexinteger     ::=  "0" ("x" | "X") hexdigit+
bininteger     ::=  "0" ("b" | "B") bindigit+
octdigit       ::=  "0"..."7"
hexdigit       ::=  digit | "a"..."f" | "A"..."F"
bindigit       ::=  "0" | "1"

If you look at the grammar, it’s easy to see that 0 need a special case. I’m not sure why the ‘+‘ is considered necessary there though. Time to dig through the dev mailing list…


Interesting to note that in Python2, more than one 0 was parsed as an octinteger (the end result is still 0 though)

decimalinteger ::=  nonzerodigit digit* | "0"
octinteger     ::=  "0" ("o" | "O") octdigit+ | "0" octdigit+

回答 2

Python2使用前导零指定八进制数:

>>> 010
8

为了避免这种情况(?误导性)行为,Python3需要明确的前缀0b0o0x

>>> 0o10
8

Python2 used the leading zero to specify octal numbers:

>>> 010
8

To avoid this (misleading?) behaviour, Python3 requires explicit prefixes 0b, 0o, 0x:

>>> 0o10
8

Android Python编程[关闭]

问题:Android Python编程[关闭]

我可以使用Python为Android编程吗?我在搜索时似乎偶然发现了许多链接…但是它们都不是具体的。

有什么建议?我想为Android编写应用程序,但实际上并不想为此而涉足Java。

PS:我的问题是我是否可以编写适用于Android的功能完善的应用程序。

Can I program for Android using Python? I seem to have stumbled upon many links while searching… however neither of them is concrete.

Any suggestions? I want to write apps for Android but really don’t want to get into Java for all this.

PS: My question is whether I can write proper, full fledged apps for Android.


回答 0

结帐Kivy。到目前为止,他们的工作非常出色,我非常喜欢他们的工作。它仍然缺少一些提供程序,但是他们每天都在增加新的内容。您需要做的第一件事是根据文档提供的内容检查需求。他们创建了一个惊人的输入框架,例如多点触摸或笔处理。他们在内部使用OpenGL ES,因此与应用程序进行交互时,复杂的图形和可视化可以非常快速地运行。他们创建apk的过程也非常简单。

Checkout Kivy. They have done a really great job so far, and I am a big fan of their work. It is still lacking some providers, but they keep adding new stuff to it everyday. First thing you need to do is to check your requirement against what they can offer based on their documentation. They have create an amazing framework for input such as multi-touch or pen handling. They use OpenGL ES internally, as a result complex graphics and visualizations can run very fast when interacting with the the application. Their process for creating an apk is also very straight forward.


回答 1

检查新的Android版Python项目。

编辑:这不是Kivy,这是一个单独的项目,旨在成为可用于其他工具箱的工具链。该架构是模块化的,并且您可以包括用于包含新的python扩展(例如brew,macports,cygwin等)的新配方。

编辑:这不是Py4A,而是python-for-android。

Check the new Python for Android project.

Edit: This is not Kivy, this is a seperate project, intended to be a toolchain usable for other toolkit. The architecture is modular, and you can include new recipe for including new python extensions (as brew, macports, cygwin etc.).

Edit: This is not Py4A, but python-for-android.


回答 2


回答 3

不,目前不行。ASE(Android脚本环境)允许您执行简单的脚本应用,但是您只能用Java编写适当的Android应用。

No, not currently. ASE (Android Scripting Environment) allows you to do simple script apps, but you can only write proper Android apps in Java.


回答 4

是的,可以。检查ASE

编辑:好的,经过评论:我没有正确阅读问题。不,您不能编写适用于Android的功能完善的成熟应用程序,但是无论如何都要检查ASE。这真的是很酷的项目。

Yep, you can. Check ASE

Edit: Ok, after comments: I haven’t read the question properly. No you can’t write write proper, full fledged apps for Android, but anyway check ASE. It is really cool project.


有趣好用的Python教程

退出移动版
微信支付
请使用 微信 扫码支付