Understanding *x ,= lst

I'm going through some old code trying to understand what it does, and I came across this odd statement: *x ,= p p is a list in this context. I've been trying to figure out what this statement does. As far as I can tell, it just sets x to the value of p . For example: p = [1,2] *x ,= p print(x) Just gives [1, 2] So is this any different than x = p ? Any idea what this syntax

了解* x,= lst

我正在浏览一些旧代码,试图理解它的作用,并且我遇到了这个奇怪的声明: *x ,= p p是这个上下文中的一个列表。 我一直在试图弄清楚这个陈述的作用。 据我所知,它只是将x设置为p的值。 例如: p = [1,2] *x ,= p print(x) 只是给了 [1, 2] 那么这与x = p什么不同呢? 任何想法这个语法在做什么? *x ,= p基本上是使用扩展迭代解包的x = list(p)的混淆版本。 x之后的逗号必须使赋值目标成为一个元组(它也可以

Iterating vs List Concatenation

So there are two ways to take a list and add the members of a second list to the first. You can use list concatenation or your can iterate over it. You can: for obj in list2: list1.append(obj) or you can: list1 = list1 + list2 or list1 += list2 My question is: which is faster, and why? I tested this using two extremely large lists (upwards of 10000 objects) and it seemed the iteratin

迭代vs List连接

因此,有两种方法可以创建列表并将第二个列表的成员添加到第一个列表中。 你可以使用列表连接,或者你可以迭代它。 您可以: for obj in list2: list1.append(obj) 或者您可以: list1 = list1 + list2 要么 list1 += list2 我的问题是:哪个更快,为什么? 我使用两个非常大的列表(10000个以上的对象)测试了这种情况,似乎迭代方法比列表级联快得多(如在l1 = l1 + l2中)。 为什么是这样? 有人可以解释吗?

Inverse Wavelet Transform [/xpost signalprocessing]

Main Problem: How can the scipy.signal.cwt() function be inversed. I have seen where Matlab has an inverse continuous wavelet transform function which will return the original form of the data by inputting the wavelet transform, although you can filter out the slices you don't want. MATALAB inverse cwt funciton Since scipy doesn't appear to have the same function, I have been trying

逆小波变换[/ xpost信号处理]

主要问题:如何反转scipy.signal.cwt()函数。 我已经看到Matlab有一个反向连续小波变换函数,它将通过输入小波变换返回数据的原始形式,尽管您可以过滤掉不需要的切片。 MATALAB逆cwt funciton 由于scipy似乎没有相同的功能,我一直在试图弄清楚如何以相同的形式获取数据,同时消除噪音和背景。 我该怎么做呢? 我试着将它平方去除负值,但是这给了我值得的方式来大而不正确。 这是我一直在尝试的: # Compute the wav

How do you remove duplicates from a list whilst preserving order?

Is there a built-in that removes duplicates from list in Python, whilst preserving order? I know that I can use a set to remove duplicates, but that destroys the original order. I also know that I can roll my own like this: def uniq(input): output = [] for x in input: if x not in output: output.append(x) return output (Thanks to unwind for that code sample.) But I'd like

如何在保持秩序的同时从列表中删除重复项?

有没有一种内置的方法可以从Python中的列表中删除重复项,同时保持顺序? 我知道我可以使用一套删除重复项,但破坏了原来的顺序。 我也知道我可以像这样推出自己的产品: def uniq(input): output = [] for x in input: if x not in output: output.append(x) return output (感谢解放这个代码示例。) 但是,如果可能的话,我想利用内置的或更加Pythonic的成语。 相关问题:在Python中,从列表中删除

How to make an unaware datetime timezone aware in python

What I need to do I have a timezone-unaware datetime object, to which I need to add a time zone in order to be able to compare it with other timezone-aware datetime objects. I do not want to convert my entire application to timezone unaware for this one legacy case. What I've Tried First, to demonstrate the problem: Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) [GCC 4.2.1 (Apple I

如何在python中感知不知道的日期时间时区

我需要做什么 我有一个时区不知道的日期时间对象,我需要添加一个时区,以便能够将其与其他可识别时区的日期时间对象进行比较。 我不想将我的整个应用程序转换为不知道这个传统案例的时区。 我试过的 首先,展示问题: Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) [GCC 4.2.1 (Apple Inc. build 5646)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import d

How do I convert local time to UTC in Python?

How do I convert a datetime string in local time to a string in UTC time? I'm sure I've done this before, but can't find it and SO will hopefully help me (and others) do that in future. Clarification : For example, if I have 2008-09-17 14:02:00 in my local timezone ( +10 ), I'd like to generate a string with the equivalent UTC time: 2008-09-17 04:02:00 . Also, from http://lu

如何在Python中将本地时间转换为UTC?

如何将本地时间的日期时间字符串转换为UTC时间的字符串? 我确信我以前做过这些,但是找不到它,所以希望将来可以帮助我(和其他人)做到这一点。 澄清 :例如,如果我有我的本地时区( +10 ) 2008-09-17 14:02:00 ,我想用等效的UTC时间生成一个字符串: 2008-09-17 04:02:00 。 此外,从http://lucumr.pocoo.org/2011/7/15/eppur-si-muove/,请注意,一般来说,这是不可能的,因为与DST和其他问题不存在从本地时间到UTC

Convert python datetime to epoch with strftime

I have a time in UTC from which I want the number of seconds since epoch. I am using strftime to convert it to the number of seconds. Taking 1st April 2012 as an example. >>>datetime.datetime(2012,04,01,0,0).strftime('%s') '1333234800' 1st of April 2012 UTC from epoch is 1333238400 but this above returns 1333234800 which is different by 1 hour. So it looks like that strftime is ta

使用strftime将python datetime转换为纪元

我有一段UTC时间,我想从这个时间开始计算时代以来的秒数。 我正在使用strftime将其转换为秒数。 以2012年4月1日为例。 >>>datetime.datetime(2012,04,01,0,0).strftime('%s') '1333234800' 2012年4月1日世界标准时间从世纪开始是1333238400,但是这个返回1333234800,这相差1个小时。 所以看起来strftime正在考虑我的系统时间,并在某处应用时区转换。 我以为日期时间纯粹是天真的? 我该如何解决这个问题?

Initialize a datetime object with seconds since epoch

The time module can be initialized using seconds since epoch: >>> import time >>> t1=time.gmtime(1284286794) >>> t1 time.struct_time(tm_year=2010, tm_mon=9, tm_mday=12, tm_hour=10, tm_min=19, tm_sec=54, tm_wday=6, tm_yday=255, tm_isdst=0) Is there an elegant way to initialize a datetime.datetime object in the same way? datetime.datetime.fromtimesta

使用epoch自动初始化一个日期时间对象

time模块可以使用自时代以来的秒数进行初始化: >>> import time >>> t1=time.gmtime(1284286794) >>> t1 time.struct_time(tm_year=2010, tm_mon=9, tm_mday=12, tm_hour=10, tm_min=19, tm_sec=54, tm_wday=6, tm_yday=255, tm_isdst=0) 有没有一种优雅的方式来以相同的方式初始化一个datetime.datetime对象? datetime.datetime.fromtimestamp会做,如果你知道时区,你可以

How to subtract a day from a date?

I have a Python datetime.datetime object. What is the best way to subtract one day? 您可以使用timedelta对象: from datetime import datetime, timedelta d = datetime.today() - timedelta(days=days_to_subtract) 减去datetime.timedelta(days=1) If your Python datetime object is timezone-aware than you should be careful to avoid errors around DST transitions (or changes in UTC offset for other reason

如何从日期中减去一天?

我有一个Python datetime.datetime对象。 减去一天的最佳方式是什么? 您可以使用timedelta对象: from datetime import datetime, timedelta d = datetime.today() - timedelta(days=days_to_subtract) 减去datetime.timedelta(days=1) 如果您的Python日期时间对象具有时区感知功能,那么您应该小心避免DST转换周围出现错误(或因其他原因导致的UTC偏移量更改): from datetime import datetime, timedelta from tzlocal i

Get Last Day of the Month in Python

Is there a way using Python's standard library to easily determine (ie one function call) the last day of a given month? If the standard library doesn't support that, does the dateutil package support this? I didn't notice this earlier when I was looking at the documentation for the calendar module, but a method called monthrange provides this information: monthrange(year, month

用Python获取本月的最后一天

有没有使用Python的标准库来轻松确定(即一个函数调用)给定月份的最后一天的方法? 如果标准库不支持,dateutil包是否支持这个? 之前我在查看日历模块的文档时没有注意到这一点,但名为monthrange的方法提供了以下信息: 月份(年份,月份) 返回指定年份和月份的月份第一天的工作日和月份中的天数。 >>> import calendar >>> calendar.monthrange(2002,1) (1, 31) >>> calendar.monthrang