分类目录归档:知识问答

Python对象是否“可订阅”是什么意思?

问题:Python对象是否“可订阅”是什么意思?

哪些类型的对象属于“可订阅”范围?

Which types of objects fall into the domain of “subscriptable”?


回答 0

它基本上意味着对象实现了该__getitem__()方法。换句话说,它描述的是“容器”对象,这意味着它们包含其他对象。这包括字符串,列表,元组和字典。

It basically means that the object implements the __getitem__() method. In other words, it describes objects that are “containers”, meaning they contain other objects. This includes strings, lists, tuples, and dictionaries.


回答 1

在我脑海中,以下是唯一可以下标的内置程序:

string:  "foobar"[3] == "b"
tuple:   (1,2,3,4)[3] == 4
list:    [1,2,3,4][3] == 4
dict:    {"a":1, "b":2, "c":3}["c"] == 3

但是mipadi的答案是正确的-实现的任何类都可以下__getitem__

Off the top of my head, the following are the only built-ins that are subscriptable:

string:  "foobar"[3] == "b"
tuple:   (1,2,3,4)[3] == 4
list:    [1,2,3,4][3] == 4
dict:    {"a":1, "b":2, "c":3}["c"] == 3

But mipadi’s answer is correct – any class that implements __getitem__ is subscriptable


回答 2

可编写脚本的对象是记录对其执行的操作的对象,并且可以将它们存储为“脚本”并可以重播。

例如,请参见:应用程序脚本框架

现在,如果Alistair不知道他问的是什么(真正由他人编辑的)“可订阅的”对象,那么(正如mipadi也回答的那样),这是正确的:

下标对象是实现__getitem__特殊方法的任何对象(例如列表,字典)。

A scriptable object is an object that records the operations done to it and it can store them as a “script” which can be replayed.

For example, see: Application Scripting Framework

Now, if Alistair didn’t know what he asked and really meant “subscriptable” objects (as edited by others), then (as mipadi also answered) this is the correct one:

A subscriptable object is any object that implements the __getitem__ special method (think lists, dictionaries).


回答 3

下标在计算中的含义是:“在程序中单独或与其他一起使用的符号(名义上写为下标,但实际上通常不使用)来指定数组的元素之一。”

现在,在@ user2194711给出的简单示例中,我们可以看到,由于两个原因,appending元素不能成为列表的一部分:

1)我们并不是真正地将方法称为append;因为它需要()调用它。

2)错误表明该函数或方法不可下标;表示它们不可像列表或序列那样进行索引。

现在看到这个:

>>> var = "myString"
>>> def foo(): return 0
... 
>>> var[3]
't'
>>> foo[3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'function' object is not subscriptable

这意味着没有下标或说元素function按顺序出现。并且我们无法在的帮助下访问它们[]

也; 正如米帕迪在回答中所说的;它基本上意味着对象实现了该__getitem__()方法。(如果可以下标)。因此产生了错误:

arr.append["HI"]

TypeError:“ builtin_function_or_method”对象不可下标

The meaning of subscript in computing is: “a symbol (notionally written as a subscript but in practice usually not) used in a program, alone or with others, to specify one of the elements of an array.”

Now, in the simple example given by @user2194711 we can see that the appending element is not able to be a part of the list because of two reasons:-

1) We are not really calling the method append; because it needs () to call it.

2) The error is indicating that the function or method is not subscriptable; means they are not indexable like a list or sequence.

Now see this:-

>>> var = "myString"
>>> def foo(): return 0
... 
>>> var[3]
't'
>>> foo[3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'function' object is not subscriptable

That means there are no subscripts or say elements in function like they occur in sequences; and we cannot access them like we do, with the help of [].

Also; as mipadi said in his answer; It basically means that the object implements the __getitem__() method. (if it is subscriptable). Thus the error produced:

arr.append["HI"]

TypeError: ‘builtin_function_or_method’ object is not subscriptable


回答 4

我有同样的问题。我在做

arr = []
arr.append["HI"]

因此,使用[会导致错误。它应该是arr.append("HI")

I had this same issue. I was doing

arr = []
arr.append["HI"]

So using [ was causing error. It should be arr.append("HI")


回答 5

作为此处较早答案的推论,通常这表示您认为没有列表(或字典或其他可下标对象)的列表。

例如,假设您有一个返回列表的函数。

def gimme_things():
    if something_happens():
        return ['all', 'the', 'things']

现在,当您调用该函数并且something_happens()由于某种原因未返回True值时,会发生什么?的if失败,这样你就告吹; gimme_things不会明确显示return任何内容-因此实际上,它会隐式地显示return None。然后这段代码:

things = gimme_things()
print("My first thing is {0}".format(things[0]))

会失败,并显示“ NoneType对象不可下标”,因为,thingsNone,所以您正在尝试这样做None[0]是没有意义的,因为…错误消息说了什么。

有两种方法可以在代码中修复此错误-第一种方法是通过things在尝试使用该错误之前检查其是否有效来避免该错误;

things = gimme_things()
if things:
    print("My first thing is {0}".format(things[0]))
else:
    print("No things")  # or raise an error, or do nothing, or ...

或等效地捕获TypeError异常;

things = gimme_things()
try:
    print("My first thing is {0}".format(things[0]))
except TypeError:
    print("No things")  # or raise an error, or do nothing, or ...

另一个是重新设计gimme_things,以确保您始终返回列表。在这种情况下,这可能是更简单的设计,因为这意味着,如果在很多地方存在类似的错误,可以将它们保持简单且惯用。

def gimme_things():
    if something_happens():
        return ['all', 'the', 'things']
    else:  # make sure we always return a list, no matter what!
        logging.info("Something didn't happen; return empty list")
        return []

当然,放在else:分支中的内容取决于您的用例。也许您应该在something_happens()失败时引发异常,以使其更明显,更明确地指出实际出问题的地方?将异常添加到自己的代码中是一种重要的方式,可以让您自己确切地知道发生故障时发生了什么!

(还请注意,后一个修复程序仍无法完全解决该错误-它阻止您尝试下标,Nonethings[0]仍然是IndexErrorwhen things是空列表。如果有,try也可以except (TypeError, IndexError)进行捕获。)

As a corollary to the earlier answers here, very often this is a sign that you think you have a list (or dict, or other subscriptable object) when you do not.

For example, let’s say you have a function which should return a list;

def gimme_things():
    if something_happens():
        return ['all', 'the', 'things']

Now when you call that function, and something_happens() for some reason does not return a True value, what happens? The if fails, and so you fall through; gimme_things doesn’t explicitly return anything — so then in fact, it will implicitly return None. Then this code:

things = gimme_things()
print("My first thing is {0}".format(things[0]))

will fail with “NoneType object is not subscriptable” because, well, things is None and so you are trying to do None[0] which doesn’t make sense because … what the error message says.

There are two ways to fix this bug in your code — the first is to avoid the error by checking that things is in fact valid before attempting to use it;

things = gimme_things()
if things:
    print("My first thing is {0}".format(things[0]))
else:
    print("No things")  # or raise an error, or do nothing, or ...

or equivalently trap the TypeError exception;

things = gimme_things()
try:
    print("My first thing is {0}".format(things[0]))
except TypeError:
    print("No things")  # or raise an error, or do nothing, or ...

Another is to redesign gimme_things so that you make sure it always returns a list. In this case, that’s probably the simpler design because it means if there are many places where you have a similar bug, they can be kept simple and idiomatic.

def gimme_things():
    if something_happens():
        return ['all', 'the', 'things']
    else:  # make sure we always return a list, no matter what!
        logging.info("Something didn't happen; return empty list")
        return []

Of course, what you put in the else: branch depends on your use case. Perhaps you should raise an exception when something_happens() fails, to make it more obvious and explicit where something actually went wrong? Adding exceptions to your own code is an important way to let yourself know exactly what’s up when something fails!

(Notice also how this latter fix still doesn’t completely fix the bug — it prevents you from attempting to subscript None but things[0] is still an IndexError when things is an empty list. If you have a try you can do except (TypeError, IndexError) to trap it, too.)


如何使用泡菜保存字典?

问题:如何使用泡菜保存字典?

我已经仔细阅读了Python文档提供的信息,但仍然有些困惑。有人可以张贴示例代码来编写新文件,然后使用pickle将字典转储到其中吗?

I have looked through the information that the Python docs give, but I’m still a little confused. Could somebody post sample code that would write a new file then use pickle to dump a dictionary into it?


回答 0

尝试这个:

import pickle

a = {'hello': 'world'}

with open('filename.pickle', 'wb') as handle:
    pickle.dump(a, handle, protocol=pickle.HIGHEST_PROTOCOL)

with open('filename.pickle', 'rb') as handle:
    b = pickle.load(handle)

print a == b

Try this:

import pickle

a = {'hello': 'world'}

with open('filename.pickle', 'wb') as handle:
    pickle.dump(a, handle, protocol=pickle.HIGHEST_PROTOCOL)

with open('filename.pickle', 'rb') as handle:
    b = pickle.load(handle)

print a == b

回答 1

import pickle

your_data = {'foo': 'bar'}

# Store data (serialize)
with open('filename.pickle', 'wb') as handle:
    pickle.dump(your_data, handle, protocol=pickle.HIGHEST_PROTOCOL)

# Load data (deserialize)
with open('filename.pickle', 'rb') as handle:
    unserialized_data = pickle.load(handle)

print(your_data == unserialized_data)

的优点HIGHEST_PROTOCOL是文件变小。这使得脱皮有时更快。

重要提示:泡菜的最大文件大小约为2GB。

替代方式

import mpu
your_data = {'foo': 'bar'}
mpu.io.write('filename.pickle', data)
unserialized_data = mpu.io.read('filename.pickle')

替代格式

对于您的应用程序,以下内容可能很重要:

  • 其他编程语言的支持
  • 阅读/写作表现
  • 紧凑度(文件大小)

另请参阅:数据序列化格式的比较

如果您想寻找一种制作配置文件的方法,则可能需要阅读我的短文《Python中的配置文件》。

import pickle

your_data = {'foo': 'bar'}

# Store data (serialize)
with open('filename.pickle', 'wb') as handle:
    pickle.dump(your_data, handle, protocol=pickle.HIGHEST_PROTOCOL)

# Load data (deserialize)
with open('filename.pickle', 'rb') as handle:
    unserialized_data = pickle.load(handle)

print(your_data == unserialized_data)

The advantage of HIGHEST_PROTOCOL is that files get smaller. This makes unpickling sometimes much faster.

Important notice: The maximum file size of pickle is about 2GB.

Alternative way

import mpu
your_data = {'foo': 'bar'}
mpu.io.write('filename.pickle', data)
unserialized_data = mpu.io.read('filename.pickle')

Alternative Formats

For your application, the following might be important:

  • Support by other programming languages
  • Reading / writing performance
  • Compactness (file size)

See also: Comparison of data serialization formats

In case you are rather looking for a way to make configuration files, you might want to read my short article Configuration files in Python


回答 2

# Save a dictionary into a pickle file.
import pickle

favorite_color = {"lion": "yellow", "kitty": "red"}  # create a dictionary
pickle.dump(favorite_color, open("save.p", "wb"))  # save it into a file named save.p

# -------------------------------------------------------------
# Load the dictionary back from the pickle file.
import pickle

favorite_color = pickle.load(open("save.p", "rb"))
# favorite_color is now {"lion": "yellow", "kitty": "red"}
# Save a dictionary into a pickle file.
import pickle

favorite_color = {"lion": "yellow", "kitty": "red"}  # create a dictionary
pickle.dump(favorite_color, open("save.p", "wb"))  # save it into a file named save.p

# -------------------------------------------------------------
# Load the dictionary back from the pickle file.
import pickle

favorite_color = pickle.load(open("save.p", "rb"))
# favorite_color is now {"lion": "yellow", "kitty": "red"}

回答 3

通常,dict除非仅包含简单的对象(例如字符串和整数),否则酸洗a 将失败。

Python 2.7.9 (default, Dec 11 2014, 01:21:43) 
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from numpy import *
>>> type(globals())     
<type 'dict'>
>>> import pickle
>>> pik = pickle.dumps(globals())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1374, in dumps
    Pickler(file, protocol).dump(obj)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 224, in dump
    self.save(obj)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 663, in _batch_setitems
    save(v)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 306, in save
    rv = reduce(self.proto)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy_reg.py", line 70, in _reduce_ex
    raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle module objects
>>> 

即使是非常简单的方法dict也会经常失败。它仅取决于内容。

>>> d = {'x': lambda x:x}
>>> pik = pickle.dumps(d)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1374, in dumps
    Pickler(file, protocol).dump(obj)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 224, in dump
    self.save(obj)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 663, in _batch_setitems
    save(v)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 748, in save_global
    (obj, module, name))
pickle.PicklingError: Can't pickle <function <lambda> at 0x102178668>: it's not found as __main__.<lambda>

但是,如果使用更好的序列化器(例如dill或)cloudpickle,则可以对大多数词典进行腌制:

>>> import dill
>>> pik = dill.dumps(d)

或者,如果您想将dict文件保存到文件中…

>>> with open('save.pik', 'w') as f:
...   dill.dump(globals(), f)
... 

后一个示例与此处发布的任何其他好的答案相同(除了忽略商品内容的可腌性之外dict)。

In general, pickling a dict will fail unless you have only simple objects in it, like strings and integers.

Python 2.7.9 (default, Dec 11 2014, 01:21:43) 
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from numpy import *
>>> type(globals())     
<type 'dict'>
>>> import pickle
>>> pik = pickle.dumps(globals())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1374, in dumps
    Pickler(file, protocol).dump(obj)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 224, in dump
    self.save(obj)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 663, in _batch_setitems
    save(v)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 306, in save
    rv = reduce(self.proto)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy_reg.py", line 70, in _reduce_ex
    raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle module objects
>>> 

Even a really simple dict will often fail. It just depends on the contents.

>>> d = {'x': lambda x:x}
>>> pik = pickle.dumps(d)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1374, in dumps
    Pickler(file, protocol).dump(obj)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 224, in dump
    self.save(obj)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 663, in _batch_setitems
    save(v)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 748, in save_global
    (obj, module, name))
pickle.PicklingError: Can't pickle <function <lambda> at 0x102178668>: it's not found as __main__.<lambda>

However, if you use a better serializer like dill or cloudpickle, then most dictionaries can be pickled:

>>> import dill
>>> pik = dill.dumps(d)

Or if you want to save your dict to a file…

>>> with open('save.pik', 'w') as f:
...   dill.dump(globals(), f)
... 

The latter example is identical to any of the other good answers posted here (which aside from neglecting the picklability of the contents of the dict are good).


回答 4

>>> import pickle
>>> with open("/tmp/picklefile", "wb") as f:
...     pickle.dump({}, f)
... 

通常,最好使用cPickle实现

>>> import cPickle as pickle
>>> help(pickle.dump)
Help on built-in function dump in module cPickle:

dump(...)
    dump(obj, file, protocol=0) -- Write an object in pickle format to the given file.

    See the Pickler docstring for the meaning of optional argument proto.
>>> import pickle
>>> with open("/tmp/picklefile", "wb") as f:
...     pickle.dump({}, f)
... 

normally it’s preferable to use the cPickle implementation

>>> import cPickle as pickle
>>> help(pickle.dump)
Help on built-in function dump in module cPickle:

dump(...)
    dump(obj, file, protocol=0) -- Write an object in pickle format to the given file.

    See the Pickler docstring for the meaning of optional argument proto.

回答 5

如果您只想将字典存储在单个文件中,请pickle像这样使用

import pickle

a = {'hello': 'world'}

with open('filename.pickle', 'wb') as handle:
    pickle.dump(a, handle)

with open('filename.pickle', 'rb') as handle:
    b = pickle.load(handle)

如果要在多个文件中保存和还原多个词典以进行缓存和存储更复杂的数据,请使用anycache。它可以完成您需要的所有其他工作pickle

from anycache import anycache

@anycache(cachedir='path/to/files')
def myfunc(hello):
    return {'hello', hello}

Anycache myfunc根据不同文件的参数存储不同的结果,cachedir然后重新加载它们。

有关更多详细信息,请参见文档

If you just want to store the dict in a single file, use pickle like that

import pickle

a = {'hello': 'world'}

with open('filename.pickle', 'wb') as handle:
    pickle.dump(a, handle)

with open('filename.pickle', 'rb') as handle:
    b = pickle.load(handle)

If you want to save and restore multiple dictionaries in multiple files for caching and store more complex data, use anycache. It does all the other stuff you need around pickle

from anycache import anycache

@anycache(cachedir='path/to/files')
def myfunc(hello):
    return {'hello', hello}

Anycache stores the different myfunc results depending on the arguments to different files in cachedir and reloads them.

See the documentation for any further details.


回答 6

将Python数据(例如字典)转储到pickle文件的简单方法。

import pickle

your_dictionary = {}

pickle.dump(your_dictionary, open('pickle_file_name.p', 'wb'))

Simple way to dump a Python data (e.g. dictionary) to a pickle file.

import pickle

your_dictionary = {}

pickle.dump(your_dictionary, open('pickle_file_name.p', 'wb'))

回答 7

import pickle

dictobj = {'Jack' : 123, 'John' : 456}

filename = "/foldername/filestore"

fileobj = open(filename, 'wb')

pickle.dump(dictobj, fileobj)

fileobj.close()
import pickle

dictobj = {'Jack' : 123, 'John' : 456}

filename = "/foldername/filestore"

fileobj = open(filename, 'wb')

pickle.dump(dictobj, fileobj)

fileobj.close()

回答 8

我发现酸洗令人困惑(可能是因为我很胖)。我发现这可行,但是:

myDictionaryString=str(myDictionary)

然后可以将其写入文本文件。我遇到错误并告诉我将整数写入.dat文件时,我放弃尝试使用pickle。很抱歉没有使用泡菜。

I’ve found pickling confusing (possibly because I’m thick). I found that this works, though:

myDictionaryString=str(myDictionary)

Which you can then write to a text file. I gave up trying to use pickle as I was getting errors telling me to write integers to a .dat file. I apologise for not using pickle.


time.sleep —休眠线程或进程?

问题:time.sleep —休眠线程或进程?

在Python for * nix中,是否time.sleep()阻塞线程或进程?

In Python for *nix, does time.sleep() block the thread or the process?


回答 0

它阻塞线程。如果在Python源代码中查看Modules / timemodule.c,您会看到在调用中floatsleep(),睡眠操作的实质部分包装在Py_BEGIN_ALLOW_THREADS和Py_END_ALLOW_THREADS块中,从而允许其他线程在当前线程继续执行一睡觉。您也可以使用简单的python程序进行测试:

import time
from threading import Thread

class worker(Thread):
    def run(self):
        for x in xrange(0,11):
            print x
            time.sleep(1)

class waiter(Thread):
    def run(self):
        for x in xrange(100,103):
            print x
            time.sleep(5)

def run():
    worker().start()
    waiter().start()

将打印:

>>> thread_test.run()
0
100
>>> 1
2
3
4
5
101
6
7
8
9
10
102

It blocks the thread. If you look in Modules/timemodule.c in the Python source, you’ll see that in the call to floatsleep(), the substantive part of the sleep operation is wrapped in a Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS block, allowing other threads to continue to execute while the current one sleeps. You can also test this with a simple python program:

import time
from threading import Thread

class worker(Thread):
    def run(self):
        for x in xrange(0,11):
            print x
            time.sleep(1)

class waiter(Thread):
    def run(self):
        for x in xrange(100,103):
            print x
            time.sleep(5)

def run():
    worker().start()
    waiter().start()

Which will print:

>>> thread_test.run()
0
100
>>> 1
2
3
4
5
101
6
7
8
9
10
102

回答 1

它只会休眠线程,除非您的应用程序只有一个线程,在这种情况下,它将休眠线程并有效地进程。

睡眠中的python文档未指定此内容,因此我当然可以理解混淆!

http://docs.python.org/2/library/time.html

It will just sleep the thread except in the case where your application has only a single thread, in which case it will sleep the thread and effectively the process as well.

The python documentation on sleep doesn’t specify this however, so I can certainly understand the confusion!

http://docs.python.org/2/library/time.html


回答 2

只是线程。

Just the thread.


回答 3

该线程将阻塞,但是该进程仍然有效。

在单线程应用程序中,这意味着您在睡眠时一切都被阻止了。在多线程应用程序中,只有您显式“睡眠”的线程将被阻塞,其他线程仍在进程中运行。

The thread will block, but the process is still alive.

In a single threaded application, this means everything is blocked while you sleep. In a multithreaded application, only the thread you explicitly ‘sleep’ will block and the other threads still run within the process.


回答 4

只有线程,除非您的进程具有单个线程。

Only the thread unless your process has a single thread.


回答 5

进程本身无法运行。关于执行,进程只是线程的容器。这意味着您根本无法暂停该过程。它根本不适用于过程。

Process is not runnable by itself. In regard to execution, process is just a container for threads. Meaning you can’t pause the process at all. It is simply not applicable to process.


回答 6

如果它在同一线程中执行,则它将阻塞一个线程,如果不是从主代码中执行,则它将阻塞该线程

it blocks a thread if it is executed in the same thread not if it is executed from the main code


如何在Windows中添加到PYTHONPATH,以便找到我的模块/软件包?

问题:如何在Windows中添加到PYTHONPATH,以便找到我的模块/软件包?

我有一个托管所有Django应用程序的目录(C:\My_Projects)。我想将此目录添加到我的目录中,PYTHONPATH以便直接调用应用程序。

我尝试从Windows GUI()添加C:\My_Projects\;到Windows Path变量中My Computer > Properties > Advanced System Settings > Environment Variables。但是它仍然不读取coltrane模块并生成此错误:

错误:没有名为coltrane的模块

I have a directory which hosts all of my Django apps (C:\My_Projects). I want to add this directory to my PYTHONPATH so I can call the apps directly.

I tried adding C:\My_Projects\; to my Windows Path variable from the Windows GUI (My Computer > Properties > Advanced System Settings > Environment Variables). But it still doesn’t read the coltrane module and generates this error:

Error: No module named coltrane


回答 0

您知道在Windows上对我非常有效的方法。

My Computer > Properties > Advanced System Settings > Environment Variables >

只需将路径添加为C:\ Python27(或安装python的任何位置)

要么

然后在系统变量下创建一个名为的新变量PythonPath。在这个变量中C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk;C:\other-folders-on-the-path

在此处输入图片说明

这是对我有用的最好方法,我在提供的任何文档中都没有找到它。

编辑:对于那些谁无法获得它,请添加

C:\ Python27;

随之而来。否则它将永远无法正常工作

You know what has worked for me really well on windows.

My Computer > Properties > Advanced System Settings > Environment Variables >

Just add the path as C:\Python27 (or wherever you installed python)

OR

Then under system variables I create a new Variable called PythonPath. In this variable I have C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk;C:\other-folders-on-the-path

enter image description here

This is the best way that has worked for me which I hadn’t found in any of the docs offered.

EDIT: For those who are not able to get it, Please add

C:\Python27;

along with it. Else it will never work.


回答 1

Windows 7 Professional I修改了@mongoose_za的答案,以便更轻松地更改python版本:

  1. [右键单击]计算机>属性>高级系统设置>环境变量
  2. 点击“系统变量”下的[新建]
  3. 变量名称:PY_HOME,变量值:C:\ path \ to \ python \ version 在此处输入图片说明
  4. 点击[确定]
  5. 找到“路径”系统变量,然后单击[编辑]
  6. 将以下内容添加到现有变量中:

    %PY_HOME%;%PY_HOME%\ Lib;%PY_HOME%\ DLLs;%PY_HOME%\ Lib \ lib-tk; 在此处输入图片说明

  7. 单击[确定]关闭所有窗口。

最后,检查命令提示符并输入python。你应该看到

>python [whatever version you are using]

如果需要在版本之间进行切换,则只需修改PY_HOME变量以指向正确的目录。如果您需要安装多个python版本,这将更易于管理。

Windows 7 Professional I Modified @mongoose_za’s answer to make it easier to change the python version:

  1. [Right Click]Computer > Properties >Advanced System Settings > Environment Variables
  2. Click [New] under “System Variable”
  3. Variable Name: PY_HOME, Variable Value:C:\path\to\python\version enter image description here
  4. Click [OK]
  5. Locate the “Path” System variable and click [Edit]
  6. Add the following to the existing variable:

    %PY_HOME%;%PY_HOME%\Lib;%PY_HOME%\DLLs;%PY_HOME%\Lib\lib-tk; enter image description here

  7. Click [OK] to close all of the windows.

As a final sanity check open a command prompt and enter python. You should see

>python [whatever version you are using]

If you need to switch between versions, you only need to modify the PY_HOME variable to point to the proper directory. This is bit easier to manage if you need multiple python versions installed.


回答 2

从Windows命令行:

set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib

要永久设置PYTHONPATH,请将该行添加到中autoexec.bat。或者,如果您通过“系统属性”编辑系统变量,则该变量也将被永久更改。

From Windows command line:

set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib

To set the PYTHONPATH permanently, add the line to your autoexec.bat. Alternatively, if you edit the system variable through the System Properties, it will also be changed permanently.


回答 3

您只需将您的安装路径(前C:\ Python27 \)到PATH变量系统变量。然后关闭并打开命令行,并输入’python’

Just append your installation path (ex. C:\Python27\) to the PATH variable in System variables. Then close and open your command line and type ‘python’.


回答 4

这些解决方案有效,但是它们仅在您的计算机上适用于您的代码。我会在代码中添加几行,如下所示:

import sys
if "C:\\My_Python_Lib" not in sys.path:
    sys.path.append("C:\\My_Python_Lib")

那应该照顾你的问题

These solutions work, but they work for your code ONLY on your machine. I would add a couple of lines to your code that look like this:

import sys
if "C:\\My_Python_Lib" not in sys.path:
    sys.path.append("C:\\My_Python_Lib")

That should take care of your problems


回答 5

PythonPythonPath添加到Windows环境:

  1. 打开资源管理器。
  2. 右键单击左侧导航树面板中的“计算机”
  3. 选择上下文菜单底部的“属性”
  4. 选择“高级系统设置”
  5. 点击“环境变量…”高级”选项卡中的
  6. “系统变量”下

      • PY_HOME

        C:\Python27
      • PYTHONPATH

        %PY_HOME%\Lib;%PY_HOME%\DLLs;%PY_HOME%\Lib\lib-tk;C:\another-library
    1. 附加

      • path

        %PY_HOME%;%PY_HOME%\Scripts\

Adding Python and PythonPath to the Windows environment:

  1. Open Explorer.
  2. Right-click ‘Computer’ in the Navigation Tree Panel on the left.
  3. Select ‘Properties’ at the bottom of the Context Menu.
  4. Select ‘Advanced system settings’
  5. Click ‘Environment Variables…’ in the Advanced Tab
  6. Under ‘System Variables’:

    1. Add

      • PY_HOME

        C:\Python27
        
      • PYTHONPATH

        %PY_HOME%\Lib;%PY_HOME%\DLLs;%PY_HOME%\Lib\lib-tk;C:\another-library
        
    2. Append

      • path

        %PY_HOME%;%PY_HOME%\Scripts\
        

回答 6

在python中设置路径的更简单方法是:单击开始>我的电脑>属性>高级系统设置>环境变量>第二个窗口>

在此处输入图片说明

选择“路径”>“编辑”>,然后添加“; C:\ Python27 \; C:\ Python27 \ Scripts \”

链接:http : //docs.python-guide.org/en/latest/starting/install/win/

The easier way to set the path in python is : click start> My Computer >Properties > Advanced System Settings > Environment Variables > second windows >

enter image description here

select Path > Edit > and then add “;C:\Python27\;C:\Python27\Scripts\”

link :http://docs.python-guide.org/en/latest/starting/install/win/


回答 7

您需要添加到您的PYTHONPATH变量而不是Windows PATH变量。

http://docs.python.org/using/windows.html

You need to add to your PYTHONPATH variable instead of Windows PATH variable.

http://docs.python.org/using/windows.html


回答 8

您还可以在.pth文件c:\PythonX.X夹或中添加一个包含所需目录的文件\site-packages folder,这在我开发Python软件包时通常是我的首选方法。

有关更多信息,请参见此处

You can also add a .pth file containing the desired directory in either your c:\PythonX.X folder, or your \site-packages folder, which tends to be my preferred method when I’m developing a Python package.

See here for more information.


回答 9

import sys
sys.path.append("path/to/Modules")
print sys.path

这不会在重新启动后持续存在,也不会转换为其他文件。但是,如果您不想对系统进行永久性修改,那就太好了。

import sys
sys.path.append("path/to/Modules")
print sys.path

This won’t persist over reboots or get translated to other files. It is however great if you don’t want to make a permanent modification to your system.


回答 10

成功执行此操作的最简单方法是再次运行python安装程序(首次安装后),然后:

  1. 选择修改。
  2. 检查所需的可选功能,然后单击下一步。
  3. 到这里,在“高级选项”步骤中,您必须看到一个选项“将Python添加到环境变量”。只需检查该选项,然后单击“安装”即可。 第三步 安装完成后,将添加python环境变量,您可以在任何地方轻松使用python。

The easiest way to do that successfully, is to run the python installer again (after the first installation) and then:

  1. choose Modify.
  2. check the optional features which you want and click Next.
  3. here we go, in “Advanced Options” step you must see an option saying “Add Python to environment variables”. Just check that option and click Install. 3rd step When the installation is completed, python environment variables are added and you can easily use python everywhere.

回答 11

在Windows上的Python 3.4中,当我将其添加到PATH环境变量而不是PYTHONPATH 时,它可以工作。就像您在D:\ Programming \ Python34中安装了Python 3.4一样,请将其添加到PATH环境变量的末尾

;D:\Programming\Python34

关闭并重新打开命令提示符,然后执行“ python”。它将打开python shell。这也解决了我的Sublime 3问题:“ python无法识别为内部或外部命令”

In Python 3.4 on windows it worked when I added it to PATH enviroment variable instead of PYTHONPATH. Like if you have installed Python 3.4 in D:\Programming\Python34 then add this at the end of your PATH environment variable

;D:\Programming\Python34

Close and reopen command prompt and execute ‘python’. It will open the python shell. This also fixed my Sublime 3 issue of ‘python is not recognized as an internal or external command’.


回答 12

可以从上面的一些说明中设置python 2.X路径。默认情况下,Python 3将安装在C:\ Users \\ AppData \ Local \ Programs \ Python \ Python35-32 \中,因此必须将此路径添加到Windows环境中的Path变量中。

The python 2.X paths can be set from few of the above instructions. Python 3 by default will be installed in C:\Users\\AppData\Local\Programs\Python\Python35-32\ So this path has to be added to Path variable in windows environment.


回答 13

要增强PYTHONPATH,请运行regedit并导航至KEY_LOCAL_MACHINE \ SOFTWARE \ Python \ PythonCore,然后选择要使用的python版本的文件夹。该文件夹内有一个标为PythonPath的文件夹,其中一个条目指定默认安装存储模块的路径。右键单击PythonPath并选择创建一个新密钥。您可能想在将要指定模块位置的项目后命名该密钥;这样,您可以轻松地划分和跟踪路径修改。

谢谢

To augment PYTHONPATH, run regedit and navigate to KEY_LOCAL_MACHINE \SOFTWARE\Python\PythonCore and then select the folder for the python version you wish to use. Inside this is a folder labelled PythonPath, with one entry that specifies the paths where the default install stores modules. Right-click on PythonPath and choose to create a new key. You may want to name the key after the project whose module locations it will specify; this way, you can easily compartmentalize and track your path modifications.

thanks


回答 14

Python使用PYTHONPATH环境变量来指定可在Windows上从中导入模块的目录列表。运行时,您可以检查sys.path变量以查看导入内容时将搜索哪些目录。

要从命令提示符设置此变量,请使用:set PYTHONPATH=list;of;paths

要从PowerShell设置此变量,请使用: $env:PYTHONPATH=’list;of;paths’在启动Python之前

建议通过“环境变量”设置全局设置此变量,因为任何版本的Python都可以使用它,而不是您打算使用的版本。在Windows FAQ文档的Python中阅读更多内容。

The PYTHONPATH environment variable is used by Python to specify a list of directories that modules can be imported from on Windows. When running, you can inspect the sys.path variable to see which directories will be searched when you import something.

To set this variable from the Command Prompt, use: set PYTHONPATH=list;of;paths.

To set this variable from PowerShell, use: $env:PYTHONPATH=’list;of;paths’ just before you launch Python.

Setting this variable globally through the Environment Variables settings is not recommended, as it may be used by any version of Python instead of the one that you intend to use. Read more in the Python on Windows FAQ docs.


回答 15

对于尝试使用Python 3.3+实现此功能的任何人,Windows安装程序现在都提供了一个将python.exe添加到系统搜索路径的选项。在文档中阅读更多内容。

For anyone trying to achieve this with Python 3.3+, the Windows installer now includes an option to add python.exe to the system search path. Read more in the docs.


回答 16

这个问题需要一个正确的答案:

只需使用site为此工作量身定制的标准包装即可!

这是怎么做的(在同一主题上回答我自己的问题的答案):


  1. 打开Python提示符并输入
>>> import site
>>> site.USER_SITE
'C:\\Users\\ojdo\\AppData\\Roaming\\Python\\Python37\\site-packages'
...
  1. 如果此文件夹尚不存在,请创建它:
...
>>> import os
>>> os.makedirs(site.USER_SITE)
...
  1. 手动或使用类似以下代码sitecustomize.py的内容在此文件夹中创建一个包含的内容的文件FIND_MY_PACKAGES。当然,您必须更改C:\My_Projects到自定义导入位置的正确路径。
...
>>> FIND_MY_PACKAGES = """
import site
site.addsitedir(r'C:\My_Projects')
"""
>>> filename = os.path.join(site.USER_SITE, 'sitecustomize.py')
>>> with open(filename, 'w') as outfile:
...     print(FIND_MY_PACKAGES, file=outfile)

下次启动Python时,C:\My_Projects它会出现在您的中sys.path,而无需触摸系统范围的设置。奖励:以上步骤也适用于Linux!

This question needs a proper answer:

Just use the standard package site, which was made for this job!

and here is how (plagiating my own answer to my own question on the very same topic):


  1. Open a Python prompt and type
>>> import site
>>> site.USER_SITE
'C:\\Users\\ojdo\\AppData\\Roaming\\Python\\Python37\\site-packages'
...
  1. Create this folder if it does not exist yet:
...
>>> import os
>>> os.makedirs(site.USER_SITE)
...
  1. Create a file sitecustomize.py in this folder containing the content of FIND_MY_PACKAGES, either manually or using something like the following code. Of course, you have to change C:\My_Projects to the correct path to your custom import location.
...
>>> FIND_MY_PACKAGES = """
import site
site.addsitedir(r'C:\My_Projects')
"""
>>> filename = os.path.join(site.USER_SITE, 'sitecustomize.py')
>>> with open(filename, 'w') as outfile:
...     print(FIND_MY_PACKAGES, file=outfile)

And the next time you start Python, C:\My_Projects is present in your sys.path, without having to touch system-wide settings. Bonus: the above steps work on Linux, too!


回答 17

安装ArcGIS Desktop时PYTHONPATH需要设置此变量ArcPY

PYTHONPATH=C:\arcgis\bin (您的ArcGIS Home Bin)

由于某种原因,当我在Windows 7 32位系统上使用安装程序时,从未设置过它。

This PYTHONPATH variable needs to be set for ArcPY when ArcGIS Desktop is installed.

PYTHONPATH=C:\arcgis\bin (your ArcGIS home bin)

For some reason it never was set when I used the installer on a Windows 7 32-bit system.


回答 18

我按照以下步骤在Windows 10中工作了。

在环境变量下,应仅将其添加到“ 系统变量 ”的PATH下,而不应将其添加到“ 用户变量 ”下。这是一个很大的混乱,如果我们错过了,那就会浪费时间。

另外,只需尝试导航到在计算机中安装Python的路径并将其添加到PATH。这只是工作而无需添加其他任何东西。

C:\ Users \ YourUserName \ AppData \ Local \ Programs \ Python \ Python37-32

最重要的是,关闭命令提示符,重新打开,然后再次尝试键入“ python”以查看版本详细信息。在环境变量中设置路径后,需要重新启动命令提示符以查看版本。

重新启动后,在命令提示符下键入python时,您应该能够看到python提示符和以下信息:

在命令提示符下键入python时

I got it worked in Windows 10 by following below steps.

Under environment variables, you should only add it under PATH of “System Variables” and not under “User Variables“. This is a great confusion and eats time if we miss it.

Also, just try to navigate to the path where you got Python installed in your machine and add it to PATH. This just works and no need to add any other thing in my case.I added just below path and it worked.

C:\Users\YourUserName\AppData\Local\Programs\Python\Python37-32

Most important, close command prompt, re-open and then re-try typing “python” to see the version details. You need to restart command prompt to see the version after setting up the path in environment variables.

After restarting, you should be able to see the python prompt and below info when typing python in command prompt:

On typing python in command prompt


回答 19

可能有些晚,但这是您将路径添加到Windows环境变量的方法。

  1. 转到环境变量选项卡,您可以通过按Windows键+ Pausa inter来执行此操作。

  2. 转到高级系统设置。

  3. 单击环境变量。

  4. 在下方的窗口中搜索“路径”值。

  5. 选择它

  6. 点击编辑

  7. 在该行的末尾,添加您的安装文件夹和到“脚本”文件夹的路由。

  8. 单击确定,接受器等。

完成后,输入cmd并从驱动器的任何位置编写python,它应该进入Python程序。

我的PC的示例(我有Python34

EXISTING_LINES;C:\Python34;C:\Python34\Scripts\

希望能帮助到你。

波哥大的问候

Maybe a little late, but this is how you add the path to the Windows Environment Variables.

  1. Go to the Environment Variables tab, you do this by pressing Windows key + Pausa inter.

  2. Go to Advanced System Settings.

  3. Click on Environment Variables.

  4. On the lower window search for the ‘Path’ value.

  5. Select it

  6. Click on Edit

  7. In the end of the line add your instalation folder and the route to ‘Scripts’ folder.

  8. Click ok, aceptar etc.

You’re done, enter cmd and write python from any location of your drive, it should enter the Python program.

Example with my pc (I have Python34)

EXISTING_LINES;C:\Python34;C:\Python34\Scripts\

Hope it helps.

Greetings from Bogotá


回答 20

您可以通过命令提示符轻松设置路径变量。

  1. 打开运行并编写cmd

  2. 在命令窗口中,输入以下内容:set path =%path%; C:\ python36

  3. 按回车。
  4. 检查写python并输入。您将看到python版本,如图所示。

在此处输入图片说明

You can set the path variable for easily by command prompt.

  1. Open run and write cmd

  2. In the command window write the following: set path=%path%;C:\python36

  3. press enter.
  4. to check write python and enter. You will see the python version as shown in the picture.

enter image description here


回答 21

虽然这个问题是关于“真正的” Python的,但确实是在网络搜索“ Iron Python PYTHONPATH”中出现的。对于像我一样困惑的Iron Python用户:事实证明Iron Python寻找一个名为的环境变量IRONPYTHONPATH

Linux / Mac / POSIX用户:不要忘记Windows不仅\用作路径分隔符,而且还;用作路径定界符,而不是:

While this question is about the ‘real’ Python, it did come up in a websearch for ‘Iron Python PYTHONPATH’. For Iron Python users as confused as I was: It turns out that Iron Python looks for an environment variable called IRONPYTHONPATH.

Linux/Mac/POSIX users: Don’t forget that not only does Windows use \ as path separators, but it also uses ; as path delimiters, not :.


在Python中将N秒添加到datetime.time的标准方法是什么?

问题:在Python中将N秒添加到datetime.time的标准方法是什么?

给定datetime.timePython中的值,是否有标准的方法向其添加整数秒,例如11:34:59+ 3 = 11:35:02

这些明显的想法行不通:

>>> datetime.time(11, 34, 59) + 3
TypeError: unsupported operand type(s) for +: 'datetime.time' and 'int'
>>> datetime.time(11, 34, 59) + datetime.timedelta(0, 3)
TypeError: unsupported operand type(s) for +: 'datetime.time' and 'datetime.timedelta'
>>> datetime.time(11, 34, 59) + datetime.time(0, 0, 3)
TypeError: unsupported operand type(s) for +: 'datetime.time' and 'datetime.time'

最后,我编写了这样的函数:

def add_secs_to_time(timeval, secs_to_add):
    secs = timeval.hour * 3600 + timeval.minute * 60 + timeval.second
    secs += secs_to_add
    return datetime.time(secs // 3600, (secs % 3600) // 60, secs % 60)

我不禁以为我缺少一种更简单的方法来做到这一点。

有关

Given a datetime.time value in Python, is there a standard way to add an integer number of seconds to it, so that 11:34:59 + 3 = 11:35:02, for example?

These obvious ideas don’t work:

>>> datetime.time(11, 34, 59) + 3
TypeError: unsupported operand type(s) for +: 'datetime.time' and 'int'
>>> datetime.time(11, 34, 59) + datetime.timedelta(0, 3)
TypeError: unsupported operand type(s) for +: 'datetime.time' and 'datetime.timedelta'
>>> datetime.time(11, 34, 59) + datetime.time(0, 0, 3)
TypeError: unsupported operand type(s) for +: 'datetime.time' and 'datetime.time'

In the end I have written functions like this:

def add_secs_to_time(timeval, secs_to_add):
    secs = timeval.hour * 3600 + timeval.minute * 60 + timeval.second
    secs += secs_to_add
    return datetime.time(secs // 3600, (secs % 3600) // 60, secs % 60)

I can’t help thinking that I’m missing an easier way to do this though.

Related


回答 0

您可以将完整datetime变量与一起使用timedelta,并通过提供一个虚拟日期,然后使用time来获取时间值。

例如:

import datetime
a = datetime.datetime(100,1,1,11,34,59)
b = a + datetime.timedelta(0,3) # days, seconds, then other fields.
print(a.time())
print(b.time())

得出两个值,相隔三秒:

11:34:59
11:35:02

您也可以选择更具可读性的

b = a + datetime.timedelta(seconds=3)

如果你这么倾向。


如果您追求的是可以执行此操作的函数,则可以使用addSecs以下方法进行研究:

import datetime

def addSecs(tm, secs):
    fulldate = datetime.datetime(100, 1, 1, tm.hour, tm.minute, tm.second)
    fulldate = fulldate + datetime.timedelta(seconds=secs)
    return fulldate.time()

a = datetime.datetime.now().time()
b = addSecs(a, 300)
print(a)
print(b)

输出:

 09:11:55.775695
 09:16:55

You can use full datetime variables with timedelta, and by providing a dummy date then using time to just get the time value.

For example:

import datetime
a = datetime.datetime(100,1,1,11,34,59)
b = a + datetime.timedelta(0,3) # days, seconds, then other fields.
print(a.time())
print(b.time())

results in the two values, three seconds apart:

11:34:59
11:35:02

You could also opt for the more readable

b = a + datetime.timedelta(seconds=3)

if you’re so inclined.


If you’re after a function that can do this, you can look into using addSecs below:

import datetime

def addSecs(tm, secs):
    fulldate = datetime.datetime(100, 1, 1, tm.hour, tm.minute, tm.second)
    fulldate = fulldate + datetime.timedelta(seconds=secs)
    return fulldate.time()

a = datetime.datetime.now().time()
b = addSecs(a, 300)
print(a)
print(b)

This outputs:

 09:11:55.775695
 09:16:55

回答 1

如此处其他人所述,您可以在整个过程中使用完整的datetime对象:

from datetime import datetime, date, time, timedelta
sometime = time(8,00) # 8am
later = (datetime.combine(date.today(), sometime) + timedelta(seconds=3)).time()

但是,我认为值得解释为什么需要完整的datetime对象。考虑如果我在下午11点增加2个小时会发生什么情况。正确的行为是什么?有一个exceptions,因为您的时间不能超过晚上11:59?它应该回绕吗?

不同的程序员会期望不同的东西,因此他们选择的任何结果都会使很多人感到惊讶。更糟糕的是,程序员最初编写的代码在最初测试时就可以正常工作,然后通过做一些意想不到的事情而使代码中断。这非常糟糕,这就是为什么不允许您向时间对象添加timedelta对象的原因。

As others here have stated, you can just use full datetime objects throughout:

from datetime import datetime, date, time, timedelta
sometime = time(8,00) # 8am
later = (datetime.combine(date.today(), sometime) + timedelta(seconds=3)).time()

However, I think it’s worth explaining why full datetime objects are required. Consider what would happen if I added 2 hours to 11pm. What’s the correct behavior? An exception, because you can’t have a time larger than 11:59pm? Should it wrap back around?

Different programmers will expect different things, so whichever result they picked would surprise a lot of people. Worse yet, programmers would write code that worked just fine when they tested it initially, and then have it break later by doing something unexpected. This is very bad, which is why you’re not allowed to add timedelta objects to time objects.


回答 2

一件事,可能会增加清晰度以覆盖默认值(秒)

>>> b = a + datetime.timedelta(seconds=3000)
>>> b
datetime.datetime(1, 1, 1, 12, 24, 59)

One little thing, might add clarity to override the default value for seconds

>>> b = a + datetime.timedelta(seconds=3000)
>>> b
datetime.datetime(1, 1, 1, 12, 24, 59)

回答 3

感谢@Pax Diablo,@ bvmou和@Arachnid建议在整个过程中使用完整的日期时间。如果我必须从外部来源接受datetime.time对象,那么这似乎是一种替代add_secs_to_time()功能:

def add_secs_to_time(timeval, secs_to_add):
    dummy_date = datetime.date(1, 1, 1)
    full_datetime = datetime.datetime.combine(dummy_date, timeval)
    added_datetime = full_datetime + datetime.timedelta(seconds=secs_to_add)
    return added_datetime.time()

此冗长的代码可以压缩为以下形式:

(datetime.datetime.combine(datetime.date(1, 1, 1), timeval) + datetime.timedelta(seconds=secs_to_add)).time()

但我想我还是要将其包装在一个函数中,以确保代码清晰。

Thanks to @Pax Diablo, @bvmou and @Arachnid for the suggestion of using full datetimes throughout. If I have to accept datetime.time objects from an external source, then this seems to be an alternative add_secs_to_time() function:

def add_secs_to_time(timeval, secs_to_add):
    dummy_date = datetime.date(1, 1, 1)
    full_datetime = datetime.datetime.combine(dummy_date, timeval)
    added_datetime = full_datetime + datetime.timedelta(seconds=secs_to_add)
    return added_datetime.time()

This verbose code can be compressed to this one-liner:

(datetime.datetime.combine(datetime.date(1, 1, 1), timeval) + datetime.timedelta(seconds=secs_to_add)).time()

but I think I’d want to wrap that up in a function for code clarity anyway.


回答 4

如果值得在您的项目中添加另一个文件/依赖项,那么我刚刚编写了一个很小的小类,它datetime.time具有算术能力。当您经过午夜时,它会绕零。现在,“从现在开始24小时将是几点钟”有很多特殊情况,包括夏时制,leap秒,历史时区更改等。但是有时候您确实确实需要简单的案例,这就是这样做的目的。

您的示例将写为:

>>> import datetime
>>> import nptime
>>> nptime.nptime(11, 34, 59) + datetime.timedelta(0, 3)
nptime(11, 35, 2)

nptime继承自datetime.time,因此任何这些方法也应该可用。

可以从PyPi以nptime(“非修整时间”)或在GitHub上获得:https : //github.com/tgs/nptime

If it’s worth adding another file / dependency to your project, I’ve just written a tiny little class that extends datetime.time with the ability to do arithmetic. When you go past midnight, it wraps around zero. Now, “What time will it be, 24 hours from now” has a lot of corner cases, including daylight savings time, leap seconds, historical timezone changes, and so on. But sometimes you really do need the simple case, and that’s what this will do.

Your example would be written:

>>> import datetime
>>> import nptime
>>> nptime.nptime(11, 34, 59) + datetime.timedelta(0, 3)
nptime(11, 35, 2)

nptime inherits from datetime.time, so any of those methods should be usable, too.

It’s available from PyPi as nptime (“non-pedantic time”), or on GitHub: https://github.com/tgs/nptime


回答 5

您不能简单地添加数字,datetime因为不清楚使用的单位是秒,小时,周…

timedelta用于日期和时间操作的类。datetime减去datetimeGives timedeltadatetimePlus timedeltaGives datetimedatetime虽然两个对象可以添加,但不能添加两个对象timedelta

创建timedelta要添加多少秒的datetime对象并将其添加到对象:

>>> from datetime import datetime, timedelta
>>> t = datetime.now() + timedelta(seconds=3000)
>>> print(t)
datetime.datetime(2018, 1, 17, 21, 47, 13, 90244)

C ++中有相同的概念:std::chrono::duration

You cannot simply add number to datetime because it’s unclear what unit is used: seconds, hours, weeks…

There is timedelta class for manipulations with date and time. datetime minus datetime gives timedelta, datetime plus timedelta gives datetime, two datetime objects cannot be added although two timedelta can.

Create timedelta object with how many seconds you want to add and add it to datetime object:

>>> from datetime import datetime, timedelta
>>> t = datetime.now() + timedelta(seconds=3000)
>>> print(t)
datetime.datetime(2018, 1, 17, 21, 47, 13, 90244)

There is same concept in C++: std::chrono::duration.


回答 6

为了完整起见,这是使用它的方式arrow(Python的更好的日期和时间):

sometime = arrow.now()
abitlater = sometime.shift(seconds=3)

For completeness’ sake, here’s the way to do it with arrow (better dates and times for Python):

sometime = arrow.now()
abitlater = sometime.shift(seconds=3)

回答 7

尝试添加datetime.datetimedatetime.timedelta。如果只需要时间部分,则可以time()在结果datetime.datetime对象上调用方法以获取它。

Try adding a datetime.datetime to a datetime.timedelta. If you only want the time portion, you can call the time() method on the resultant datetime.datetime object to get it.


回答 8

老问题了,但我想我会抛出一个处理时区的函数。关键部分是将datetime.time对象的tzinfo属性传递到Combine中,然后在结果虚拟日期时间上使用timetz()而不是time()。此答案部分受此处其他答案的启发。

def add_timedelta_to_time(t, td):
    """Add a timedelta object to a time object using a dummy datetime.

    :param t: datetime.time object.
    :param td: datetime.timedelta object.

    :returns: datetime.time object, representing the result of t + td.

    NOTE: Using a gigantic td may result in an overflow. You've been
    warned.
    """
    # Create a dummy date object.
    dummy_date = date(year=100, month=1, day=1)

    # Combine the dummy date with the given time.
    dummy_datetime = datetime.combine(date=dummy_date, time=t, tzinfo=t.tzinfo)

    # Add the timedelta to the dummy datetime.
    new_datetime = dummy_datetime + td

    # Return the resulting time, including timezone information.
    return new_datetime.timetz()

这是一个非常简单的测试用例类(使用内置unittest):

import unittest
from datetime import datetime, timezone, timedelta, time

class AddTimedeltaToTimeTestCase(unittest.TestCase):
    """Test add_timedelta_to_time."""

    def test_wraps(self):
        t = time(hour=23, minute=59)
        td = timedelta(minutes=2)
        t_expected = time(hour=0, minute=1)
        t_actual = add_timedelta_to_time(t=t, td=td)
        self.assertEqual(t_expected, t_actual)

    def test_tz(self):
        t = time(hour=4, minute=16, tzinfo=timezone.utc)
        td = timedelta(hours=10, minutes=4)
        t_expected = time(hour=14, minute=20, tzinfo=timezone.utc)
        t_actual = add_timedelta_to_time(t=t, td=td)
        self.assertEqual(t_expected, t_actual)


if __name__ == '__main__':
    unittest.main()

Old question, but I figured I’d throw in a function that handles timezones. The key parts are passing the datetime.time object’s tzinfo attribute into combine, and then using timetz() instead of time() on the resulting dummy datetime. This answer partly inspired by the other answers here.

def add_timedelta_to_time(t, td):
    """Add a timedelta object to a time object using a dummy datetime.

    :param t: datetime.time object.
    :param td: datetime.timedelta object.

    :returns: datetime.time object, representing the result of t + td.

    NOTE: Using a gigantic td may result in an overflow. You've been
    warned.
    """
    # Create a dummy date object.
    dummy_date = date(year=100, month=1, day=1)

    # Combine the dummy date with the given time.
    dummy_datetime = datetime.combine(date=dummy_date, time=t, tzinfo=t.tzinfo)

    # Add the timedelta to the dummy datetime.
    new_datetime = dummy_datetime + td

    # Return the resulting time, including timezone information.
    return new_datetime.timetz()

And here’s a really simple test case class (using built-in unittest):

import unittest
from datetime import datetime, timezone, timedelta, time

class AddTimedeltaToTimeTestCase(unittest.TestCase):
    """Test add_timedelta_to_time."""

    def test_wraps(self):
        t = time(hour=23, minute=59)
        td = timedelta(minutes=2)
        t_expected = time(hour=0, minute=1)
        t_actual = add_timedelta_to_time(t=t, td=td)
        self.assertEqual(t_expected, t_actual)

    def test_tz(self):
        t = time(hour=4, minute=16, tzinfo=timezone.utc)
        td = timedelta(hours=10, minutes=4)
        t_expected = time(hour=14, minute=20, tzinfo=timezone.utc)
        t_actual = add_timedelta_to_time(t=t, td=td)
        self.assertEqual(t_expected, t_actual)


if __name__ == '__main__':
    unittest.main()

在Matplotlib图中隐藏轴文本

问题:在Matplotlib图中隐藏轴文本

我正在尝试在两个轴上绘制一个没有刻度或数字的图形(我使用传统意义上的轴,而不是matplotlib命名法!)。我遇到的一个问题是matplotlib通过减去值N来调整x(y)ticklabel,然后在轴的末端添加N。

这可能含糊其词,但以下简化示例突出了该问题,其中“ 6.18”是N的有问题的值:

import matplotlib.pyplot as plt
import random
prefix = 6.18

rx = [prefix+(0.001*random.random()) for i in arange(100)]
ry = [prefix+(0.001*random.random()) for i in arange(100)]
plt.plot(rx,ry,'ko')

frame1 = plt.gca()
for xlabel_i in frame1.axes.get_xticklabels():
    xlabel_i.set_visible(False)
    xlabel_i.set_fontsize(0.0)
for xlabel_i in frame1.axes.get_yticklabels():
    xlabel_i.set_fontsize(0.0)
    xlabel_i.set_visible(False)
for tick in frame1.axes.get_xticklines():
    tick.set_visible(False)
for tick in frame1.axes.get_yticklines():
    tick.set_visible(False)

plt.show()

我想知道的三件事是:

  1. 如何关闭这一行为在首位(虽然在大多数情况下,它是有用的,它并不总是!)我已经通过看matplotlib.axis.XAxis,并不能找到任何合适

  2. 如何使N消失(即X.set_visible(False)

  3. 无论如何,还有更好的方法来做上述事情吗?如果可以的话,我的最终绘图将是图中的4×4子图。

I’m trying to plot a figure without tickmarks or numbers on either of the axes (I use axes in the traditional sense, not the matplotlib nomenclature!). An issue I have come across is where matplotlib adjusts the x(y)ticklabels by subtracting a value N, then adds N at the end of the axis.

This may be vague, but the following simplified example highlights the issue, with ‘6.18’ being the offending value of N:

import matplotlib.pyplot as plt
import random
prefix = 6.18

rx = [prefix+(0.001*random.random()) for i in arange(100)]
ry = [prefix+(0.001*random.random()) for i in arange(100)]
plt.plot(rx,ry,'ko')

frame1 = plt.gca()
for xlabel_i in frame1.axes.get_xticklabels():
    xlabel_i.set_visible(False)
    xlabel_i.set_fontsize(0.0)
for xlabel_i in frame1.axes.get_yticklabels():
    xlabel_i.set_fontsize(0.0)
    xlabel_i.set_visible(False)
for tick in frame1.axes.get_xticklines():
    tick.set_visible(False)
for tick in frame1.axes.get_yticklines():
    tick.set_visible(False)

plt.show()

The three things I would like to know are:

  1. How to turn off this behaviour in the first place (although in most cases it is useful, it is not always!) I have looked through matplotlib.axis.XAxis and cannot find anything appropriate

  2. How can I make N disappear (i.e. X.set_visible(False))

  3. Is there a better way to do the above anyway? My final plot would be 4×4 subplots in a figure, if that is relevant.


回答 0

除了隐藏每个元素,您还可以隐藏整个轴:

frame1.axes.get_xaxis().set_visible(False)
frame1.axes.get_yaxis().set_visible(False)

或者,您可以将刻度线设置为空列表:

frame1.axes.get_xaxis().set_ticks([])
frame1.axes.get_yaxis().set_ticks([])

在第二个选项中,您仍然可以使用plt.xlabel()plt.ylabel()在轴上添加标签。

Instead of hiding each element, you can hide the whole axis:

frame1.axes.get_xaxis().set_visible(False)
frame1.axes.get_yaxis().set_visible(False)

Or, you can set the ticks to an empty list:

frame1.axes.get_xaxis().set_ticks([])
frame1.axes.get_yaxis().set_ticks([])

In this second option, you can still use plt.xlabel() and plt.ylabel() to add labels to the axes.


回答 1

如果要仅隐藏保留网格线的轴文本:

frame1 = plt.gca()
frame1.axes.xaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticklabels([])

set_visible(False)set_ticks([])也将隐藏网格线。

If you want to hide just the axis text keeping the grid lines:

frame1 = plt.gca()
frame1.axes.xaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticklabels([])

Doing set_visible(False) or set_ticks([]) will also hide the grid lines.


回答 2

如果您像我一样,并且ax在绘制图形时并不总是检索轴,则一个简单的解决方案是

plt.xticks([])
plt.yticks([])

If you are like me and don’t always retrieve the axes, ax, when plotting the figure, then a simple solution would be to do

plt.xticks([])
plt.yticks([])

回答 3

有点旧的线程,但是,这似乎是使用最新版本的matplotlib的更快方法:

设置x轴的主要格式

ax.xaxis.set_major_formatter(plt.NullFormatter())

Somewhat of an old thread but, this seems to be a faster method using the latest version of matplotlib:

set the major formatter for the x-axis

ax.xaxis.set_major_formatter(plt.NullFormatter())

回答 4

我实际上无法根据此处的任何代码段(甚至答案中接受的代码段)绘制没有边界或轴数据的图像。在浏览了一些API文档之后,我使用了这段代码来渲染图像

plt.axis('off')
plt.tick_params(axis='both', left='off', top='off', right='off', bottom='off', labelleft='off', labeltop='off', labelright='off', labelbottom='off')
plt.savefig('foo.png', dpi=100, bbox_inches='tight', pad_inches=0.0)

我使用该tick_params调用基本上关闭了可能呈现的任何其他信息,并且在输出文件中有一个完美的图形。

I was not actually able to render an image without borders or axis data based on any of the code snippets here (even the one accepted at the answer). After digging through some API documentation, I landed on this code to render my image

plt.axis('off')
plt.tick_params(axis='both', left='off', top='off', right='off', bottom='off', labelleft='off', labeltop='off', labelright='off', labelbottom='off')
plt.savefig('foo.png', dpi=100, bbox_inches='tight', pad_inches=0.0)

I used the tick_params call to basically shut down any extra information that might be rendered and I have a perfect graph in my output file.


回答 5

我已经对该图进行了颜色编码以简化此过程。

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)

在此处输入图片说明

您可以使用以下命令完全控制图形,以完成答案,我还添加了对样条线的控制:

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

# X AXIS -BORDER
ax.spines['bottom'].set_visible(False)
# BLUE
ax.set_xticklabels([])
# RED
ax.set_xticks([])
# RED AND BLUE TOGETHER
ax.axes.get_xaxis().set_visible(False)

# Y AXIS -BORDER
ax.spines['left'].set_visible(False)
# YELLOW
ax.set_yticklabels([])
# GREEN
ax.set_yticks([])
# YELLOW AND GREEN TOGHETHER
ax.axes.get_yaxis().set_visible(False)

I’ve colour coded this figure to ease the process.

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)

enter image description here

You can have full control over the figure using these commands, to complete the answer I’ve add also the control over the splines:

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

# X AXIS -BORDER
ax.spines['bottom'].set_visible(False)
# BLUE
ax.set_xticklabels([])
# RED
ax.set_xticks([])
# RED AND BLUE TOGETHER
ax.axes.get_xaxis().set_visible(False)

# Y AXIS -BORDER
ax.spines['left'].set_visible(False)
# YELLOW
ax.set_yticklabels([])
# GREEN
ax.set_yticks([])
# YELLOW AND GREEN TOGHETHER
ax.axes.get_yaxis().set_visible(False)

回答 6

使用面向对象的API时,该Axes对象有两种用于删除轴文本的有用方法,set_xticklabels()set_xticks()

假设您使用

fig, ax = plt.subplots(1)
ax.plot(x, y)

如果您只想删除刻度线标签,则可以使用

ax.set_xticklabels([])

或完全删除刻度线,您可以使用

ax.set_xticks([])

这些方法对于准确指定刻度线的位置以及如何标记刻度线很有用。传递空列表将分别导致没有滴答声或标签。

When using the object oriented API, the Axes object has two useful methods for removing the axis text, set_xticklabels() and set_xticks().

Say you create a plot using

fig, ax = plt.subplots(1)
ax.plot(x, y)

If you simply want to remove the tick labels, you could use

ax.set_xticklabels([])

or to remove the ticks completely, you could use

ax.set_xticks([])

These methods are useful for specifying exactly where you want the ticks and how you want them labeled. Passing an empty list results in no ticks, or no labels, respectively.


回答 7

一种技巧可能是将刻度标签的颜色设置为白色以隐藏它!

plt.xticks(color='w')
plt.yticks(color='w')

One trick could be setting the color of tick labels as white to hide it!

plt.xticks(color='w')
plt.yticks(color='w')

在python中使用matplotlib绘制对数轴

问题:在python中使用matplotlib绘制对数轴

我想使用matplotlib绘制一个对数轴的图形。

我一直在阅读文档,但无法弄清楚语法。我知道这可能'scale=linear'与plot参数类似,但是我似乎无法正确理解

示例程序:

import pylab
import matplotlib.pyplot as plt
a = [pow(10, i) for i in range(10)]
fig = plt.figure()
ax = fig.add_subplot(2, 1, 1)

line, = ax.plot(a, color='blue', lw=2)
pylab.show()

I want to plot a graph with one logarithmic axis using matplotlib.

I’ve been reading the docs, but can’t figure out the syntax. I know that it’s probably something simple like 'scale=linear' in the plot arguments, but I can’t seem to get it right

Sample program:

import pylab
import matplotlib.pyplot as plt
a = [pow(10, i) for i in range(10)]
fig = plt.figure()
ax = fig.add_subplot(2, 1, 1)

line, = ax.plot(a, color='blue', lw=2)
pylab.show()

回答 0

您可以使用该Axes.set_yscale方法。这样,您可以在Axes创建对象后更改比例。这也将允许您构建一个控件,让用户根据需要选择比例。

要添加的相关行是:

ax.set_yscale('log')

您可以使用'linear'切换回线性刻度。您的代码如下所示:

import pylab
import matplotlib.pyplot as plt
a = [pow(10, i) for i in range(10)]
fig = plt.figure()
ax = fig.add_subplot(2, 1, 1)

line, = ax.plot(a, color='blue', lw=2)

ax.set_yscale('log')

pylab.show()

结果图

You can use the Axes.set_yscale method. That allows you to change the scale after the Axes object is created. That would also allow you to build a control to let the user pick the scale if you needed to.

The relevant line to add is:

ax.set_yscale('log')

You can use 'linear' to switch back to a linear scale. Here’s what your code would look like:

import pylab
import matplotlib.pyplot as plt
a = [pow(10, i) for i in range(10)]
fig = plt.figure()
ax = fig.add_subplot(2, 1, 1)

line, = ax.plot(a, color='blue', lw=2)

ax.set_yscale('log')

pylab.show()

result chart


回答 1

首先,混合pylabpyplot编码不是很整洁。而且,pyplot样式比使用pylab更为可取

这是一个仅使用pyplot函数的稍作清理的代码:

from matplotlib import pyplot

a = [ pow(10,i) for i in range(10) ]

pyplot.subplot(2,1,1)
pyplot.plot(a, color='blue', lw=2)
pyplot.yscale('log')
pyplot.show()

相关功能是pyplot.yscale()。如果使用面向对象的版本,请用方法替换它Axes.set_yscale()。请记住,您还可以使用pyplot.xscale()(或Axes.set_xscale())更改X轴的比例。

检查我的问题‘log’和’symlog’有什么区别?查看matplotlib提供的图形比例的一些示例。

First of all, it’s not very tidy to mix pylab and pyplot code. What’s more, pyplot style is preferred over using pylab.

Here is a slightly cleaned up code, using only pyplot functions:

from matplotlib import pyplot

a = [ pow(10,i) for i in range(10) ]

pyplot.subplot(2,1,1)
pyplot.plot(a, color='blue', lw=2)
pyplot.yscale('log')
pyplot.show()

The relevant function is pyplot.yscale(). If you use the object-oriented version, replace it by the method Axes.set_yscale(). Remember that you can also change the scale of X axis, using pyplot.xscale() (or Axes.set_xscale()).

Check my question What is the difference between ‘log’ and ‘symlog’? to see a few examples of the graph scales that matplotlib offers.


回答 2

您只需要使用符号学而不是情节:

from pylab import *
import matplotlib.pyplot  as pyplot
a = [ pow(10,i) for i in range(10) ]
fig = pyplot.figure()
ax = fig.add_subplot(2,1,1)

line, = ax.semilogy(a, color='blue', lw=2)
show()

You simply need to use semilogy instead of plot:

from pylab import *
import matplotlib.pyplot  as pyplot
a = [ pow(10,i) for i in range(10) ]
fig = pyplot.figure()
ax = fig.add_subplot(2,1,1)

line, = ax.semilogy(a, color='blue', lw=2)
show()

回答 3

如果要更改对数的底数,只需添加:

plt.yscale('log',basey=2) 
# where basex or basey are the bases of log

if you want to change the base of logarithm, just add:

plt.yscale('log',basey=2) 
# where basex or basey are the bases of log

回答 4

我知道这有点不合时宜,因为一些评论提到这ax.set_yscale('log')是“最好的”解决方案,我认为可能是反驳。我不建议将其ax.set_yscale('log')用于直方图和条形图。在我的版本(0.99.1.1)中,我遇到了一些渲染问题-不确定此问题的普遍性。但是,bar和hist都具有可选参数,可以将y比例设置为log,这很好用。

参考:http : //matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.bar

http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.hist

I know this is slightly off-topic, since some comments mentioned the ax.set_yscale('log') to be “nicest” solution I thought a rebuttal could be due. I would not recommend using ax.set_yscale('log') for histograms and bar plots. In my version (0.99.1.1) i run into some rendering problems – not sure how general this issue is. However both bar and hist has optional arguments to set the y-scale to log, which work fine.

references: http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.bar

http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.hist


回答 5

因此,如果您只是像我经常那样使用简单的API(我在ipython中经常使用它),那么这很简单

yscale('log')
plot(...)

希望这可以帮助寻找简单答案的人!:)。

So if you are simply using the unsophisticated API, like I often am (I use it in ipython a lot), then this is simply

yscale('log')
plot(...)

Hope this helps someone looking for a simple answer! :).


回答 6

您可以使用以下代码:

np.log(df['col_whose_log_you_need']).iplot(kind='histogram', bins=100,
                                   xTitle = 'log of col',yTitle ='Count corresponding to column',
                                   title='Distribution of log(col_whose_log_you_need)')

You can use below code:

np.log(df['col_whose_log_you_need']).iplot(kind='histogram', bins=100,
                                   xTitle = 'log of col',yTitle ='Count corresponding to column',
                                   title='Distribution of log(col_whose_log_you_need)')

如何在不执行Python脚本的情况下检查其语法?

问题:如何在不执行Python脚本的情况下检查其语法?

我曾经用来perl -c programfile检查Perl程序的语法,然后退出而不执行它。有没有等效的方法可以对Python脚本执行此操作?

I used to use perl -c programfile to check the syntax of a Perl program and then exit without executing it. Is there an equivalent way to do this for a Python script?


回答 0

您可以通过编译来检查语法:

python -m py_compile script.py

You can check the syntax by compiling it:

python -m py_compile script.py

回答 1

您可以使用以下工具:


回答 2

import sys
filename = sys.argv[1]
source = open(filename, 'r').read() + '\n'
compile(source, filename, 'exec')

将其另存为checker.py并运行python checker.py yourpyfile.py

import sys
filename = sys.argv[1]
source = open(filename, 'r').read() + '\n'
compile(source, filename, 'exec')

Save this as checker.py and run python checker.py yourpyfile.py.


回答 3

这是使用ast模块的另一种解决方案:

python -c "import ast; ast.parse(open('programfile').read())"

要在Python脚本中完全做到这一点:

import ast, traceback

filename = 'programfile'
with open(filename) as f:
    source = f.read()
valid = True
try:
    ast.parse(source)
except SyntaxError:
    valid = False
    traceback.print_exc()  # Remove to silence any errros
print(valid)

Here’s another solution, using the ast module:

python -c "import ast; ast.parse(open('programfile').read())"

To do it cleanly from within a Python script:

import ast, traceback

filename = 'programfile'
with open(filename) as f:
    source = f.read()
valid = True
try:
    ast.parse(source)
except SyntaxError:
    valid = False
    traceback.print_exc()  # Remove to silence any errros
print(valid)

回答 4

也许有用的在线检查器PEP8:http ://pep8online.com/

Perhaps useful online checker PEP8 : http://pep8online.com/


回答 5

Pyflakes会执行您所要求的操作,它只会检查语法。从文档:

Pyflakes做出了一个简单的承诺:它永远不会抱怨样式,并且会非常非常努力地避免产生误报。

Pyflakes也比Pylint或Pychecker更快。这主要是因为Pyflakes仅检查每个文件的语法树。

要安装和使用:

$ pip install pyflakes
$ pyflakes yourPyFile.py

Pyflakes does what you ask, it just checks the syntax. From the docs:

Pyflakes makes a simple promise: it will never complain about style, and it will try very, very hard to never emit false positives.

Pyflakes is also faster than Pylint or Pychecker. This is largely because Pyflakes only examines the syntax tree of each file individually.

To install and use:

$ pip install pyflakes
$ pyflakes yourPyFile.py

回答 6

由于某些原因(我是py新手…),-m调用不起作用…

所以这是一个bash包装函数…

# ---------------------------------------------------------
# check the python synax for all the *.py files under the
# <<product_version_dir/sfw/python
# ---------------------------------------------------------
doCheckPythonSyntax(){

    doLog "DEBUG START doCheckPythonSyntax"

    test -z "$sleep_interval" || sleep "$sleep_interval"
    cd $product_version_dir/sfw/python
    # python3 -m compileall "$product_version_dir/sfw/python"

    # foreach *.py file ...
    while read -r f ; do \

        py_name_ext=$(basename $f)
        py_name=${py_name_ext%.*}

        doLog "python3 -c \"import $py_name\""
        # doLog "python3 -m py_compile $f"

        python3 -c "import $py_name"
        # python3 -m py_compile "$f"
        test $! -ne 0 && sleep 5

    done < <(find "$product_version_dir/sfw/python" -type f -name "*.py")

    doLog "DEBUG STOP  doCheckPythonSyntax"
}
# eof func doCheckPythonSyntax

for some reason ( I am a py newbie … ) the -m call did not work …

so here is a bash wrapper func …

# ---------------------------------------------------------
# check the python synax for all the *.py files under the
# <<product_version_dir/sfw/python
# ---------------------------------------------------------
doCheckPythonSyntax(){

    doLog "DEBUG START doCheckPythonSyntax"

    test -z "$sleep_interval" || sleep "$sleep_interval"
    cd $product_version_dir/sfw/python
    # python3 -m compileall "$product_version_dir/sfw/python"

    # foreach *.py file ...
    while read -r f ; do \

        py_name_ext=$(basename $f)
        py_name=${py_name_ext%.*}

        doLog "python3 -c \"import $py_name\""
        # doLog "python3 -m py_compile $f"

        python3 -c "import $py_name"
        # python3 -m py_compile "$f"
        test $! -ne 0 && sleep 5

    done < <(find "$product_version_dir/sfw/python" -type f -name "*.py")

    doLog "DEBUG STOP  doCheckPythonSyntax"
}
# eof func doCheckPythonSyntax

根据另一个列表中的值对列表进行排序?

问题:根据另一个列表中的值对列表进行排序?

我有一个这样的字符串列表:

X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
Y = [ 0,   1,   1,   0,   1,   2,   2,   0,   1 ]

使用Y中的值对X进行排序以获取以下输出的最短方法是什么?

["a", "d", "h", "b", "c", "e", "i", "f", "g"]

具有相同“键”的元素的顺序无关紧要。我可以求助于for结构的使用,但我好奇是否有更短的方法。有什么建议么?

I have a list of strings like this:

X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
Y = [ 0,   1,   1,   0,   1,   2,   2,   0,   1 ]

What is the shortest way of sorting X using values from Y to get the following output?

["a", "d", "h", "b", "c", "e", "i", "f", "g"]

The order of the elements having the same “key” does not matter. I can resort to the use of for constructs but I am curious if there is a shorter way. Any suggestions?


回答 0

最短代码

[x for _,x in sorted(zip(Y,X))]

例:

X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
Y = [ 0,   1,   1,    0,   1,   2,   2,   0,   1]

Z = [x for _,x in sorted(zip(Y,X))]
print(Z)  # ["a", "d", "h", "b", "c", "e", "i", "f", "g"]

一般来说

[x for _, x in sorted(zip(Y,X), key=lambda pair: pair[0])]

解释:

  1. zip两个list
  2. 创建一个新的,list基于zip使用排序sorted()
  3. 使用列表推导从排序的,压缩的中提取每对的第一个元素list

有关如何设置\使用key参数以及sorted一般功能的更多信息,请查看this


Shortest Code

[x for _,x in sorted(zip(Y,X))]

Example:

X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
Y = [ 0,   1,   1,    0,   1,   2,   2,   0,   1]

Z = [x for _,x in sorted(zip(Y,X))]
print(Z)  # ["a", "d", "h", "b", "c", "e", "i", "f", "g"]

Generally Speaking

[x for _, x in sorted(zip(Y,X), key=lambda pair: pair[0])]

Explained:

  1. zip the two lists.
  2. create a new, sorted list based on the zip using sorted().
  3. using a list comprehension extract the first elements of each pair from the sorted, zipped list.

For more information on how to set\use the key parameter as well as the sorted function in general, take a look at this.



回答 1

将两个列表压缩在一起,对其进行排序,然后选择所需的部分:

>>> yx = zip(Y, X)
>>> yx
[(0, 'a'), (1, 'b'), (1, 'c'), (0, 'd'), (1, 'e'), (2, 'f'), (2, 'g'), (0, 'h'), (1, 'i')]
>>> yx.sort()
>>> yx
[(0, 'a'), (0, 'd'), (0, 'h'), (1, 'b'), (1, 'c'), (1, 'e'), (1, 'i'), (2, 'f'), (2, 'g')]
>>> x_sorted = [x for y, x in yx]
>>> x_sorted
['a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g']

将它们结合在一起可获得:

[x for y, x in sorted(zip(Y, X))]

Zip the two lists together, sort it, then take the parts you want:

>>> yx = zip(Y, X)
>>> yx
[(0, 'a'), (1, 'b'), (1, 'c'), (0, 'd'), (1, 'e'), (2, 'f'), (2, 'g'), (0, 'h'), (1, 'i')]
>>> yx.sort()
>>> yx
[(0, 'a'), (0, 'd'), (0, 'h'), (1, 'b'), (1, 'c'), (1, 'e'), (1, 'i'), (2, 'f'), (2, 'g')]
>>> x_sorted = [x for y, x in yx]
>>> x_sorted
['a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g']

Combine these together to get:

[x for y, x in sorted(zip(Y, X))]

回答 2

另外,如果您不介意使用numpy数组(或者实际上已经在处理numpy数组…),这是另一个不错的解决方案:

people = ['Jim', 'Pam', 'Micheal', 'Dwight']
ages = [27, 25, 4, 9]

import numpy
people = numpy.array(people)
ages = numpy.array(ages)
inds = ages.argsort()
sortedPeople = people[inds]

我在这里找到它:http : //scienceoss.com/sort-one-list-by-another-list/

Also, if you don’t mind using numpy arrays (or in fact already are dealing with numpy arrays…), here is another nice solution:

people = ['Jim', 'Pam', 'Micheal', 'Dwight']
ages = [27, 25, 4, 9]

import numpy
people = numpy.array(people)
ages = numpy.array(ages)
inds = ages.argsort()
sortedPeople = people[inds]

I found it here: http://scienceoss.com/sort-one-list-by-another-list/


回答 3

对我来说,最明显的解决方案是使用key关键字arg。

>>> X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
>>> Y = [ 0,   1,   1,    0,   1,   2,   2,   0,   1]
>>> keydict = dict(zip(X, Y))
>>> X.sort(key=keydict.get)
>>> X
['a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g']

请注意,如果您愿意,可以将其缩短为单线:

>>> X.sort(key=dict(zip(X, Y)).get)

The most obvious solution to me is to use the key keyword arg.

>>> X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
>>> Y = [ 0,   1,   1,    0,   1,   2,   2,   0,   1]
>>> keydict = dict(zip(X, Y))
>>> X.sort(key=keydict.get)
>>> X
['a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g']

Note that you can shorten this to a one-liner if you care to:

>>> X.sort(key=dict(zip(X, Y)).get)

回答 4

我实际上是来这里寻找按值匹配的列表对列表进行排序的。

list_a = ['foo', 'bar', 'baz']
list_b = ['baz', 'bar', 'foo']
sorted(list_b, key=lambda x: list_a.index(x))
# ['foo', 'bar', 'baz']

I actually came here looking to sort a list by a list where the values matched.

list_a = ['foo', 'bar', 'baz']
list_b = ['baz', 'bar', 'foo']
sorted(list_b, key=lambda x: list_a.index(x))
# ['foo', 'bar', 'baz']

回答 5

more_itertools 有一个用于并行迭代可迭代对象的工具:

给定

from more_itertools import sort_together


X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
Y = [ 0,   1,   1,    0,   1,   2,   2,   0,   1]

演示版

sort_together([Y, X])[1]
# ('a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g')

more_itertools has a tool for sorting iterables in parallel:

Given

from more_itertools import sort_together


X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
Y = [ 0,   1,   1,    0,   1,   2,   2,   0,   1]

Demo

sort_together([Y, X])[1]
# ('a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g')

回答 6

我喜欢列出排序索引。这样,我可以按照与源列表相同的顺序对任何列表进行排序。一旦有了排序索引的列表,简单的列表理解就可以解决问题:

X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
Y = [ 0,   1,   1,    0,   1,   2,   2,   0,   1]

sorted_y_idx_list = sorted(range(len(Y)),key=lambda x:Y[x])
Xs = [X[i] for i in sorted_y_idx_list ]

print( "Xs:", Xs )
# prints: Xs: ["a", "d", "h", "b", "c", "e", "i", "f", "g"]

请注意,也可以使用来获得排序后的索引列表numpy.argsort()

I like having a list of sorted indices. That way, I can sort any list in the same order as the source list. Once you have a list of sorted indices, a simple list comprehension will do the trick:

X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
Y = [ 0,   1,   1,    0,   1,   2,   2,   0,   1]

sorted_y_idx_list = sorted(range(len(Y)),key=lambda x:Y[x])
Xs = [X[i] for i in sorted_y_idx_list ]

print( "Xs:", Xs )
# prints: Xs: ["a", "d", "h", "b", "c", "e", "i", "f", "g"]

Note that the sorted index list can also be gotten using numpy.argsort().


回答 7

另一个选择,结合几个答案。

zip(*sorted(zip(Y,X)))[1]

为了使用python3:

list(zip(*sorted(zip(B,A))))[1]

Another alternative, combining several of the answers.

zip(*sorted(zip(Y,X)))[1]

In order to work for python3:

list(zip(*sorted(zip(B,A))))[1]

回答 8

zip,按第二列排序,返回第一列。

zip(*sorted(zip(X,Y), key=operator.itemgetter(1)))[0]

zip, sort by the second column, return the first column.

zip(*sorted(zip(X,Y), key=operator.itemgetter(1)))[0]

回答 9

快速的单线。

list_a = [5,4,3,2,1]
list_b = [1,1.5,1.75,2,3,3.5,3.75,4,5]

假设您希望列表a与列表b匹配。

orderedList =  sorted(list_a, key=lambda x: list_b.index(x))

当需要将较小的列表排序为较大的值时,这将很有帮助。假设较大的列表包含较小列表中的所有值,则可以完成此操作。

A quick one-liner.

list_a = [5,4,3,2,1]
list_b = [1,1.5,1.75,2,3,3.5,3.75,4,5]

Say you want list a to match list b.

orderedList =  sorted(list_a, key=lambda x: list_b.index(x))

This is helpful when needing to order a smaller list to values in larger. Assuming that the larger list contains all values in the smaller list, it can be done.


回答 10

您可以创建一个pandas Series,使用主列表作为data,其他列表作为index,然后按索引排序:

import pandas as pd
pd.Series(data=X,index=Y).sort_index().tolist()

输出:

['a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g']

You can create a pandas Series, using the primary list as data and the other list as index, and then just sort by the index:

import pandas as pd
pd.Series(data=X,index=Y).sort_index().tolist()

output:

['a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g']

回答 11

如果您想同时获得两个排序列表(python3),这是Whatangs的答案。

X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
Y = [ 0,   1,   1,    0,   1,   2,   2,   0,   1]

Zx, Zy = zip(*[(x, y) for x, y in sorted(zip(Y, X))])

print(list(Zx))  # [0, 0, 0, 1, 1, 1, 1, 2, 2]
print(list(Zy))  # ['a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g']

只要记住Zx和Zy是元组即可。如果有更好的方法,我也在徘徊。

警告:如果使用空列表运行它,则会崩溃。

Here is Whatangs answer if you want to get both sorted lists (python3).

X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
Y = [ 0,   1,   1,    0,   1,   2,   2,   0,   1]

Zx, Zy = zip(*[(x, y) for x, y in sorted(zip(Y, X))])

print(list(Zx))  # [0, 0, 0, 1, 1, 1, 1, 2, 2]
print(list(Zy))  # ['a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g']

Just remember Zx and Zy are tuples. I am also wandering if there is a better way to do that.

Warning: If you run it with empty lists it crashes.


回答 12

我创建了一个更通用的函数,该函数根据@Whatang的答案启发,根据另一个列表对两个以上的列表进行排序。

def parallel_sort(*lists):
    """
    Sorts the given lists, based on the first one.
    :param lists: lists to be sorted

    :return: a tuple containing the sorted lists
    """

    # Create the initially empty lists to later store the sorted items
    sorted_lists = tuple([] for _ in range(len(lists)))

    # Unpack the lists, sort them, zip them and iterate over them
    for t in sorted(zip(*lists)):
        # list items are now sorted based on the first list
        for i, item in enumerate(t):    # for each item...
            sorted_lists[i].append(item)  # ...store it in the appropriate list

    return sorted_lists

I have created a more general function, that sorts more than two lists based on another one, inspired by @Whatang’s answer.

def parallel_sort(*lists):
    """
    Sorts the given lists, based on the first one.
    :param lists: lists to be sorted

    :return: a tuple containing the sorted lists
    """

    # Create the initially empty lists to later store the sorted items
    sorted_lists = tuple([] for _ in range(len(lists)))

    # Unpack the lists, sort them, zip them and iterate over them
    for t in sorted(zip(*lists)):
        # list items are now sorted based on the first list
        for i, item in enumerate(t):    # for each item...
            sorted_lists[i].append(item)  # ...store it in the appropriate list

    return sorted_lists

回答 13

list1 = ['a','b','c','d','e','f','g','h','i']
list2 = [0,1,1,0,1,2,2,0,1]

output=[]
cur_loclist = []

要获取存在的唯一值 list2

list_set = set(list2)

在以下位置查找索引的位置 list2

list_str = ''.join(str(s) for s in list2)

list2使用跟踪索引的位置cur_loclist

[0、3、7、1、2、4、8、5、6]

for i in list_set:
cur_loc = list_str.find(str(i))

while cur_loc >= 0:
    cur_loclist.append(cur_loc)
    cur_loc = list_str.find(str(i),cur_loc+1)

print(cur_loclist)

for i in range(0,len(cur_loclist)):
output.append(list1[cur_loclist[i]])
print(output)
list1 = ['a','b','c','d','e','f','g','h','i']
list2 = [0,1,1,0,1,2,2,0,1]

output=[]
cur_loclist = []

To get unique values present in list2

list_set = set(list2)

To find the loc of the index in list2

list_str = ''.join(str(s) for s in list2)

Location of index in list2 is tracked using cur_loclist

[0, 3, 7, 1, 2, 4, 8, 5, 6]

for i in list_set:
cur_loc = list_str.find(str(i))

while cur_loc >= 0:
    cur_loclist.append(cur_loc)
    cur_loc = list_str.find(str(i),cur_loc+1)

print(cur_loclist)

for i in range(0,len(cur_loclist)):
output.append(list1[cur_loclist[i]])
print(output)

回答 14

这是一个古老的问题,但是我看到的一些答案由于zip无法编写脚本而无法实际使用。其他答案没有困扰import operator并在此处提供有关此模块及其好处的更多信息。

这个问题至少有两个好的习惯用法。从您提供的示例输入开始:

X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
Y = [ 0,   1,   1,   0,   1,   2,   2,   0,   1 ]

使用“ 装饰-排序-未装饰 ”习惯用法

R. Schwartz在90年代在Perl中推广了这种模式之后,这也称为Schwartzian_transform

# Zip (decorate), sort and unzip (undecorate).
# Converting to list to script the output and extract X
list(zip(*(sorted(zip(Y,X)))))[1]                                                                                                                       
# Results in: ('a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g')

请注意,在这种情况下YX会按字典顺序进行排序和比较。也就是说,比较第一个项目(来自Y);如果它们相同,则比较第二个项目(来自X),依此类推。这会造成不稳定除非您为字典顺序包括原始列表索引,以使重复项保持原始顺序,否则输出。

使用operator模块

这使您可以更直接地控制对输入进行排序的方式,因此您可以通过简单地说明要作为排序依据的特定键来获得排序稳定性在这里查看更多示例。

import operator    

# Sort by Y (1) and extract X [0]
list(zip(*sorted(zip(X,Y), key=operator.itemgetter(1))))[0]                                                                                                 
# Results in: ('a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g')

This is an old question but some of the answers I see posted don’t actually work because zip is not scriptable. Other answers didn’t bother to import operator and provide more info about this module and its benefits here.

There are at least two good idioms for this problem. Starting with the example input you provided:

X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
Y = [ 0,   1,   1,   0,   1,   2,   2,   0,   1 ]

Using the “Decorate-Sort-Undecorate” idiom

This is also known as the Schwartzian_transform after R. Schwartz who popularized this pattern in Perl in the 90s:

# Zip (decorate), sort and unzip (undecorate).
# Converting to list to script the output and extract X
list(zip(*(sorted(zip(Y,X)))))[1]                                                                                                                       
# Results in: ('a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g')

Note that in this case Y and X are sorted and compared lexicographically. That is, the first items (from Y) are compared; and if they are the same then the second items (from X) are compared, and so on. This can create unstable outputs unless you include the original list indices for the lexicographic ordering to keep duplicates in their original order.

Using the operator module

This gives you more direct control over how to sort the input, so you can get sorting stability by simply stating the specific key to sort by. See more examples here.

import operator    

# Sort by Y (1) and extract X [0]
list(zip(*sorted(zip(X,Y), key=operator.itemgetter(1))))[0]                                                                                                 
# Results in: ('a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g')

从变量中的值构造pandas DataFrame会得到“ ValueError:如果使用所有标量值,则必须传递索引”

问题:从变量中的值构造pandas DataFrame会得到“ ValueError:如果使用所有标量值,则必须传递索引”

这可能是一个简单的问题,但是我不知道该怎么做。可以说我有两个变量,如下所示。

a = 2
b = 3

我想从中构造一个DataFrame:

df2 = pd.DataFrame({'A':a,'B':b})

这会产生一个错误:

ValueError:如果使用所有标量值,则必须传递索引

我也尝试过这个:

df2 = (pd.DataFrame({'a':a,'b':b})).reset_index()

这给出了相同的错误消息。

This may be a simple question, but I can not figure out how to do this. Lets say that I have two variables as follows.

a = 2
b = 3

I want to construct a DataFrame from this:

df2 = pd.DataFrame({'A':a,'B':b})

This generates an error:

ValueError: If using all scalar values, you must pass an index

I tried this also:

df2 = (pd.DataFrame({'a':a,'b':b})).reset_index()

This gives the same error message.


回答 0

错误消息指出,如果要传递标量值,则必须传递索引。因此,您不能对列使用标量值-例如,使用列表:

>>> df = pd.DataFrame({'A': [a], 'B': [b]})
>>> df
   A  B
0  2  3

或使用标量值并传递索引:

>>> df = pd.DataFrame({'A': a, 'B': b}, index=[0])
>>> df
   A  B
0  2  3

The error message says that if you’re passing scalar values, you have to pass an index. So you can either not use scalar values for the columns — e.g. use a list:

>>> df = pd.DataFrame({'A': [a], 'B': [b]})
>>> df
   A  B
0  2  3

or use scalar values and pass an index:

>>> df = pd.DataFrame({'A': a, 'B': b}, index=[0])
>>> df
   A  B
0  2  3

回答 1

pd.DataFrame.from_records当您已经有了字典时,也可以使用以下方法更方便:

df = pd.DataFrame.from_records([{ 'A':a,'B':b }])

您还可以根据需要通过以下方式设置索引:

df = pd.DataFrame.from_records([{ 'A':a,'B':b }], index='A')

You can also use pd.DataFrame.from_records which is more convenient when you already have the dictionary in hand:

df = pd.DataFrame.from_records([{ 'A':a,'B':b }])

You can also set index, if you want, by:

df = pd.DataFrame.from_records([{ 'A':a,'B':b }], index='A')

回答 2

您需要首先创建一个熊猫系列。第二步是将熊猫系列转换为熊猫数据框。

import pandas as pd
data = {'a': 1, 'b': 2}
pd.Series(data).to_frame()

您甚至可以提供列名。

pd.Series(data).to_frame('ColumnName')

You need to create a pandas series first. The second step is to convert the pandas series to pandas dataframe.

import pandas as pd
data = {'a': 1, 'b': 2}
pd.Series(data).to_frame()

You can even provide a column name.

pd.Series(data).to_frame('ColumnName')

回答 3

您可以尝试将字典包装到列表中

my_dict = {'A':1,'B':2}

pd.DataFrame([my_dict])

   A  B
0  1  2

You may try wrapping your dictionary in to list

my_dict = {'A':1,'B':2}

pd.DataFrame([my_dict])

   A  B
0  1  2

回答 4

也许Series将提供您需要的所有功能:

pd.Series({'A':a,'B':b})

可以将DataFrame视为Series的集合,因此您可以:

  • 连接多个系列到一个数据帧(如所描述的在这里

  • 将Series变量添加到现有数据框中(此处示例

Maybe Series would provide all the functions you need:

pd.Series({'A':a,'B':b})

DataFrame can be thought of as a collection of Series hence you can :

  • Concatenate multiple Series into one data frame (as described here )

  • Add a Series variable into existing data frame ( example here )


回答 5

您需要提供可迭代项作为Pandas DataFrame列的值:

df2 = pd.DataFrame({'A':[a],'B':[b]})

You need to provide iterables as the values for the Pandas DataFrame columns:

df2 = pd.DataFrame({'A':[a],'B':[b]})

回答 6

我对numpy数组有同样的问题,解决方案是将它们展平:

data = {
    'b': array1.flatten(),
    'a': array2.flatten(),
}

df = pd.DataFrame(data)

I had the same problem with numpy arrays and the solution is to flatten them:

data = {
    'b': array1.flatten(),
    'a': array2.flatten(),
}

df = pd.DataFrame(data)

回答 7

如果要转换标量字典,则必须包含一个索引:

import pandas as pd

alphabets = {'A': 'a', 'B': 'b'}
index = [0]
alphabets_df = pd.DataFrame(alphabets, index=index)
print(alphabets_df)

尽管列表字典不需要索引,但是可以将相同的概念扩展为列表字典:

planets = {'planet': ['earth', 'mars', 'jupiter'], 'length_of_day': ['1', '1.03', '0.414']}
index = [0, 1, 2]
planets_df = pd.DataFrame(planets, index=index)
print(planets_df)

当然,对于列表字典,您可以构建不带索引的数据框:

planets_df = pd.DataFrame(planets)
print(planets_df)

If you intend to convert a dictionary of scalars, you have to include an index:

import pandas as pd

alphabets = {'A': 'a', 'B': 'b'}
index = [0]
alphabets_df = pd.DataFrame(alphabets, index=index)
print(alphabets_df)

Although index is not required for a dictionary of lists, the same idea can be expanded to a dictionary of lists:

planets = {'planet': ['earth', 'mars', 'jupiter'], 'length_of_day': ['1', '1.03', '0.414']}
index = [0, 1, 2]
planets_df = pd.DataFrame(planets, index=index)
print(planets_df)

Of course, for the dictionary of lists, you can build the dataframe without an index:

planets_df = pd.DataFrame(planets)
print(planets_df)

回答 8

您可以尝试:

df2 = pd.DataFrame.from_dict({'a':a,'b':b}, orient = 'index')

从’orient’参数的文档中:如果传递的dict的键应该是结果DataFrame的列,请传递’columns’(默认值)。否则,如果键应该是行,则传递“ index”。

You could try:

df2 = pd.DataFrame.from_dict({'a':a,'b':b}, orient = 'index')

From the documentation on the ‘orient’ argument: If the keys of the passed dict should be the columns of the resulting DataFrame, pass ‘columns’ (default). Otherwise if the keys should be rows, pass ‘index’.


回答 9

熊猫魔术在工作。一切逻辑都搞定了。

错误消息"ValueError: If using all scalar values, you must pass an index"说您必须传递索引。

这并不一定意味着传递索引会使熊猫按照自己的意愿去做

传递索引时,pandas会将字典键视为列名,并将值视为列中索引中每个值应包含的值。

a = 2
b = 3
df2 = pd.DataFrame({'A':a,'B':b}, index=[1])

    A   B
1   2   3

传递更大的索引:

df2 = pd.DataFrame({'A':a,'B':b}, index=[1, 2, 3, 4])

    A   B
1   2   3
2   2   3
3   2   3
4   2   3

如果没有给出索引,则通常由数据框自动生成索引。然而,大熊猫不知道多少行23你想要的。但是,您可以对此更加明确

df2 = pd.DataFrame({'A':[a]*4,'B':[b]*4})
df2

    A   B
0   2   3
1   2   3
2   2   3
3   2   3

但是默认索引是基于0的。

我建议在创建数据框时始终将列表字典传递给数据框构造函数。对于其他开发人员来说更容易阅读。Pandas有很多警告,不要让其他开发人员必须要拥有所有这些方面的专家才能阅读您的代码。

Pandas magic at work. All logic is out.

The error message "ValueError: If using all scalar values, you must pass an index" Says you must pass an index.

This does not necessarily mean passing an index makes pandas do what you want it to do

When you pass an index, pandas will treat your dictionary keys as column names and the values as what the column should contain for each of the values in the index.

a = 2
b = 3
df2 = pd.DataFrame({'A':a,'B':b}, index=[1])

    A   B
1   2   3

Passing a larger index:

df2 = pd.DataFrame({'A':a,'B':b}, index=[1, 2, 3, 4])

    A   B
1   2   3
2   2   3
3   2   3
4   2   3

An index is usually automatically generated by a dataframe when none is given. However, pandas does not know how many rows of 2 and 3 you want. You can however be more explicit about it

df2 = pd.DataFrame({'A':[a]*4,'B':[b]*4})
df2

    A   B
0   2   3
1   2   3
2   2   3
3   2   3

The default index is 0 based though.

I would recommend always passing a dictionary of lists to the dataframe constructor when creating dataframes. It’s easier to read for other developers. Pandas has a lot of caveats, don’t make other developers have to experts in all of them in order to read your code.


回答 10

输入不必是记录列表,也可以是单个字典:

pd.DataFrame.from_records({'a':1,'b':2}, index=[0])
   a  b
0  1  2

这似乎等效于:

pd.DataFrame({'a':1,'b':2}, index=[0])
   a  b
0  1  2

the input does not have to be a list of records – it can be a single dictionary as well:

pd.DataFrame.from_records({'a':1,'b':2}, index=[0])
   a  b
0  1  2

Which seems to be equivalent to:

pd.DataFrame({'a':1,'b':2}, index=[0])
   a  b
0  1  2

回答 11

这是因为DataFrame具有两个直观的维度-列行。

您仅使用字典键指定列。

如果只想指定一维数据,请使用系列!

This is because a DataFrame has two intuitive dimensions – the columns and the rows.

You are only specifying the columns using the dictionary keys.

If you only want to specify one dimensional data, use a Series!


回答 12

将字典转换为数据框

col_dict_df = pd.Series(col_dict).to_frame('new_col').reset_index()

为列命名

col_dict_df.columns = ['col1', 'col2']

Convert Dictionary to Data Frame

col_dict_df = pd.Series(col_dict).to_frame('new_col').reset_index()

Give new name to Column

col_dict_df.columns = ['col1', 'col2']

回答 13

如果您有字典,则可以使用以下代码将其转换为熊猫数据框:

pd.DataFrame({"key": d.keys(), "value": d.values()})

If you have a dictionary you can turn it into a pandas data frame with the following line of code:

pd.DataFrame({"key": d.keys(), "value": d.values()})

回答 14

只需将字典传递给列表即可:

a = 2
b = 3
df2 = pd.DataFrame([{'A':a,'B':b}])

Just pass the dict on a list:

a = 2
b = 3
df2 = pd.DataFrame([{'A':a,'B':b}])