reverse

Python Reverse Generator

假如想象 提交于 2019-11-30 16:36:58
问题 I'm looking for a way to reverse a generator object. I know how to reverse sequences: foo = imap(seq.__getitem__, xrange(len(seq)-1, -1, -1)) But is something similar possible with a generator as the input and a reversed generator as the output (len(seq) stays the same, so the value from the original sequence can be used)? 回答1: You cannot reverse a generator in any generic way except by casting it to a sequence and creating an iterator from that. Later terms of a generator cannot necessarily

C memcpy in reverse

淺唱寂寞╮ 提交于 2019-11-30 14:55:48
问题 I am working with audio data. I'd like to play the sample file in reverse. The data is stored as unsigned ints and packed nice and tight. Is there a way to call memcpy that will copy in reverse order. i.e. if I had 1,2,3,4 stored in an array, could I call memcpy and magically reverse them so I get 4,3,2,1. 回答1: This works for copying int s in reverse: void reverse_intcpy(int *restrict dst, const int *restrict src, size_t n) { size_t i; for (i=0; i < n; ++i) dst[n-1-i] = src[i]; } Just like

C memcpy in reverse

邮差的信 提交于 2019-11-30 12:21:17
I am working with audio data. I'd like to play the sample file in reverse. The data is stored as unsigned ints and packed nice and tight. Is there a way to call memcpy that will copy in reverse order. i.e. if I had 1,2,3,4 stored in an array, could I call memcpy and magically reverse them so I get 4,3,2,1. This works for copying int s in reverse: void reverse_intcpy(int *restrict dst, const int *restrict src, size_t n) { size_t i; for (i=0; i < n; ++i) dst[n-1-i] = src[i]; } Just like memcpy() , the regions pointed-to by dst and src must not overlap. If you want to reverse in-place: void

What's “better” the reverse method or the reversed built-in function?

我怕爱的太早我们不能终老 提交于 2019-11-30 12:19:37
What is typically regarded as more Pythonic/better/faster to use, the reverse method or the reversed built-in function? Both in action: _list = list(xrange(4)) print _list rlist = list(reversed(_list)) print rlist _list.reverse() print _list Depends on whether you want to reverse the list in-place (i.e. change the list) or not. No other real difference. Often using reversed leads to nicer code. foo.reverse() actually reverses the elements in the container. reversed() doesn't actually reverse anything, it merely returns an object that can be used to iterate over the container's elements in

How to reverse lines of a text file?

流过昼夜 提交于 2019-11-30 11:09:05
I'm writing a small shell script that needs to reverse the lines of a text file. Is there a standard filter command to do this sort of thing? My specific application is that I'm getting a list of Git commit identifiers, and I want to process them in reverse order: git log --pretty=oneline work...master | grep -v DEBUG: | cut -d' ' -f1 | reverse The best I've come up with is to implement reverse like this: ... | cat -b | sort -rn | cut -f2- This uses cat to number every line, then sort to sort them in descending numeric order (which ends up reversing the whole file), then cut to remove the

Python: How exactly can you take a string, split it, reverse it and join it back together again?

送分小仙女□ 提交于 2019-11-30 10:57:16
问题 How exactly can you take a string, split it, reverse it and join it back together again without the brackets, commas, etc. using python? 回答1: >>> tmp = "a,b,cde" >>> tmp2 = tmp.split(',') >>> tmp2.reverse() >>> "".join(tmp2) 'cdeba' or simpler: >>> tmp = "a,b,cde" >>> ''.join(tmp.split(',')[::-1]) 'cdeba' The important parts here are the split function and the join function. To reverse the list you can use reverse() , which reverses the list in place or the slicing syntax [::-1] which returns

Reversing single linked list in C#

爷,独闯天下 提交于 2019-11-30 10:24:04
问题 I am trying to reverse a linked list. This is the code I have come up with: public static void Reverse(ref Node root) { Node tmp = root; Node nroot = null; Node prev = null; while (tmp != null) { //Make a new node and copy tmp nroot = new Node(); nroot.data = tmp.data; nroot.next = prev; prev = nroot; tmp = tmp.next; } root = nroot; } It is working well. Was wondering if it possible to avoid creating new node. Would like to have suggestions on this. 回答1: Node p = root, n = null; while (p !=

Reverse DataFrame column order

三世轮回 提交于 2019-11-30 08:26:38
I want to simply reverse the column order of a given DataFrame. My DataFrame: data = {'year': [2010, 2011, 2012, 2011, 2012, 2010, 2011, 2012], 'team': ['Bears', 'Bears', 'Bears', 'Packers', 'Packers', 'Lions', 'Lions', 'Lions'], 'wins': [11, 8, 10, 15, 11, 6, 10, 4], 'losses': [5, 8, 6, 1, 5, 10, 6, 12]} football = pd.DataFrame(data, columns=['year', 'team', 'wins', 'losses']) Actual output: year team wins losses 0 2010 Bears 11 5 1 2011 Bears 8 8 2 2012 Bears 10 6 3 2011 Packers 15 1 4 2012 Packers 11 5 5 2010 Lions 6 10 6 2011 Lions 10 6 7 2012 Lions 4 12 I thought this would work but it

Concat a video with itself, but in reverse, using ffmpeg

Deadly 提交于 2019-11-30 07:20:10
I was able to reverse with: ffmpeg -i input.mp4 -vf reverse output_reversed.mp4 And I can concat with: ffmpeg -i input.mp4 -i input.mp4 -filter_complex "[0:0] [0:1] [1:0] [1:1] concat=n=2:v=1:a=1 [v] [a]" -map "[v]" -map "[a]" output.mp4 But can I concat with a reverse version of the video, with a single command? What I am trying to achieve is a ping pong effect, where the video plays once, then plays backwards right after. Thanks! Gyan Technically, you can do it using ffmpeg -i input.mp4 -filter_complex "[0:v]reverse,fifo[r];[0:v][0:a][r] [0:a]concat=n=2:v=1:a=1 [v] [a]" -map "[v]" -map "[a]"

django-cms: urls used by apphooks don't work with reverse() or {% url %}

落爺英雄遲暮 提交于 2019-11-30 06:54:44
I'm using django-cms with apphooks to display book detail information. I need the page with the app hook to accept a slug that specifies which book to display. I created a page called 'books' and added the apphook 'BookDetailApp'. Here's what my books.cms_app file looks like: class BooksApp (CMSApp): name = _('Book Detail Page Application') urls = ['books.urls'] apphook_pool.register(BooksApp) Here's what my books.urls looks like: urlpatterns = patterns('', url(r'^(?P<slug>[\w\-]+)?', BookDetailView.as_view(), name='book_detail'), ) And here's what my books.views file looks like: class