reverse

Python reverse dictionary items order

和自甴很熟 提交于 2021-01-13 08:02:10
问题 Assume I have a dictionary: d = {3: 'three', 2: 'two', 1: 'one'} I want to rearrange the order of this dictionary so that the dictionary is: d = {1: 'one', 2: 'two', 3: 'three'} I was thinking something like the reverse() function for lists, but that did not work. Thanks in advance for your answers! 回答1: Since Python 3.8 and above, the items view is iterable in reverse, so you can just do: d = dict(reversed(d.items())) On 3.7 and 3.6, they hadn't gotten around to implementing __reversed__ on

Python reverse dictionary items order

混江龙づ霸主 提交于 2021-01-13 08:00:34
问题 Assume I have a dictionary: d = {3: 'three', 2: 'two', 1: 'one'} I want to rearrange the order of this dictionary so that the dictionary is: d = {1: 'one', 2: 'two', 3: 'three'} I was thinking something like the reverse() function for lists, but that did not work. Thanks in advance for your answers! 回答1: Since Python 3.8 and above, the items view is iterable in reverse, so you can just do: d = dict(reversed(d.items())) On 3.7 and 3.6, they hadn't gotten around to implementing __reversed__ on

Swapping first array element with last, second with second last and so on

十年热恋 提交于 2021-01-04 05:56:12
问题 I have an array, where I have to swap the first value with last, second with second last, and so on. I tried doing it like in the code below, but it only works halfway, replacing the other half with zeros. Could somebody explain what is wrong here, please? for (i = 0; i < arr.length; i++) { temp = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = temp; } 回答1: You have the right idea, but you're iterating over the whole array, so you switch the first and the last elements,

Calculate cumsum from the end towards the beginning

谁说我不能喝 提交于 2021-01-02 08:21:38
问题 I'm trying to calculate the cumsum starting from the last row towards the first for each group. Sample data: t1 <- data.frame(var = "a", val = c(0,0,0,0,1,0,0,0,0,1,0,0,0,0,0)) t2 <- data.frame(var = "b", val = c(0,0,0,0,1,0,0,1,0,0,0,0,0,0,0)) ts <- rbind(t1, t2) Desired format (grouped by var ): ts <- data.frame(var = c("a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b"), val = c(2,2,2,2,2,1,1

How to read a file in reverse order?

别来无恙 提交于 2020-11-29 04:06:51
问题 How to read a file in reverse order using python? I want to read a file from last line to first line. 回答1: for line in reversed(open("filename").readlines()): print line.rstrip() And in Python 3: for line in reversed(list(open("filename"))): print(line.rstrip()) 回答2: A correct, efficient answer written as a generator. import os def reverse_readline(filename, buf_size=8192): """A generator that returns the lines of a file in reverse order""" with open(filename) as fh: segment = None offset = 0

Sorting an array of directory filenames in descending natural order

 ̄綄美尐妖づ 提交于 2020-07-03 12:24:00
问题 I have a content directory to be returned in descending natural order. I'm using scandir() and natsort() , but the addition of array_reverse() yields no results. I've been researching using a combination of opendir() and readdir() as well what ever else to affect this outcome. The items to be sorted are numbered image files. They are to be returned as: 10 9 8 7 and so on, but like from like 1000 999 998 997 ... until 0 Here's my current code: $dir = 'dead_dir/dead_content/'; $launcher =

Sorting an array of directory filenames in descending natural order

十年热恋 提交于 2020-07-03 12:23:51
问题 I have a content directory to be returned in descending natural order. I'm using scandir() and natsort() , but the addition of array_reverse() yields no results. I've been researching using a combination of opendir() and readdir() as well what ever else to affect this outcome. The items to be sorted are numbered image files. They are to be returned as: 10 9 8 7 and so on, but like from like 1000 999 998 997 ... until 0 Here's my current code: $dir = 'dead_dir/dead_content/'; $launcher =

Sorting an array of directory filenames in descending natural order

♀尐吖头ヾ 提交于 2020-07-03 12:23:14
问题 I have a content directory to be returned in descending natural order. I'm using scandir() and natsort() , but the addition of array_reverse() yields no results. I've been researching using a combination of opendir() and readdir() as well what ever else to affect this outcome. The items to be sorted are numbered image files. They are to be returned as: 10 9 8 7 and so on, but like from like 1000 999 998 997 ... until 0 Here's my current code: $dir = 'dead_dir/dead_content/'; $launcher =