sublist

How do I reverse a sublist in a list in place?

二次信任 提交于 2019-12-04 03:08:12
I'm supposed to create a function, which input is a list and two numbers, the function reverses the sublist which its place is indicated by the two numbers. for example this is what it's supposed to do: >>> lst = [1, 2, 3, 4, 5] >>> reverse_sublist (lst,0,4) >>> lst [4, 3, 2, 1, 5] I created a function and it works, but I'm not sure is it's in place. This is my code: def reverse_sublist(lst,start,end): sublist=lst[start:end] sublist.reverse() lst[start:end]=sublist print(lst) def reverse_sublist(lst,start,end): lst[start:end] = lst[start:end][::-1] return lst Partial reverse with no temporary

Convert Hierarchical DataTable to Json

瘦欲@ 提交于 2019-12-03 09:01:16
I have a hierarchial data table as follows which generates menu and its sub menus. main menu has parentId as 0. Submenu has parent Ids referring to parentId. ResourceId DisplayName ParentId Url ----------------------------------------------- 1 Home 0 Some Url 2 Student 0 Some Url 3 Staff 0 Some Url 4 Library 0 Some Url 6 StudentAtt 1 Some Url 7 TimeTable 1 Some Url 8 Staff Att 2 Some Url 9 Book Issue 3 Some Url 10 Book Return 3 Some Url 11 Fee Payment 4 Some Url 12 Book fine 10 Some Url need to convert it to Json. Below is the code i tried out. I am trying to check if ParentId of SubMenu

How to Get a Sublist in C#

耗尽温柔 提交于 2019-12-03 06:26:16
问题 I have a List<String> and i need to take a sublist out of this list. Is there any methods of List available for this in .NET 3.5? 回答1: You want List::GetRange(firstIndex, count). See http://msdn.microsoft.com/en-us/library/21k0e39c.aspx // I have a List called list List sublist = list.GetRange(5, 5); // (gets elements 5,6,7,8,9) List anotherSublist = list.GetRange(0, 4); // gets elements 0,1,2,3) Is that what you're after? If you're looking to delete the sublist items from the original list,

Converting a subList of an ArrayList to an ArrayList

浪子不回头ぞ 提交于 2019-12-03 06:20:50
问题 Im using an ArrayList and im trying to copy a part of it to another ArrayList therefore im using: sibling.keys = (ArrayList<Integer>) keys.subList(mid, this.num); Where "sibling.keys" is the new ArrayList and "keys or this.keys" is the older ArrayList. I used the casting because eclipse told me to do that but then it throws a ClassCastException : java.util.ArrayList$SubList cannot be cast to java.util.ArrayList Any advice? 回答1: subList returns a view on an existing list. It's not an ArrayList

How to Get a Sublist in C#

99封情书 提交于 2019-12-02 21:00:13
I have a List<String> and i need to take a sublist out of this list. Is there any methods of List available for this in .NET 3.5? You want List::GetRange(firstIndex, count). See http://msdn.microsoft.com/en-us/library/21k0e39c.aspx // I have a List called list List sublist = list.GetRange(5, 5); // (gets elements 5,6,7,8,9) List anotherSublist = list.GetRange(0, 4); // gets elements 0,1,2,3) Is that what you're after? If you're looking to delete the sublist items from the original list, you can then do: // list is our original list // sublist is our (newly created) sublist built from GetRange(

Converting a subList of an ArrayList to an ArrayList

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 18:49:28
Im using an ArrayList and im trying to copy a part of it to another ArrayList therefore im using: sibling.keys = (ArrayList<Integer>) keys.subList(mid, this.num); Where "sibling.keys" is the new ArrayList and "keys or this.keys" is the older ArrayList. I used the casting because eclipse told me to do that but then it throws a ClassCastException : java.util.ArrayList$SubList cannot be cast to java.util.ArrayList Any advice? subList returns a view on an existing list. It's not an ArrayList . You can create a copy of it: sibling.keys = new ArrayList<Integer>(keys.subList(mid, this.num)); Or if

How to get a sublist of a list between two words of list in Python

若如初见. 提交于 2019-12-02 09:31:15
sorry for my english. I'm italian. Starting from a list like this: words = ['tree', 'water', 'dog', 'soap', 'bike', 'cat', 'bird'] i want to get (in Python) the sublist between two words that i input by keyboard or in the code. For example, if i put the words 'water' and 'bike' i want to get the sublist: words = ['water', 'dog', 'soap', 'bike'] or if the list is words = ['tree', 'water', 'dog', 'soap', 'tree', 'cat', 'bird'] and i put the words 'tree' and 'tree' i want to get this sublist: words = ['tree', 'water', 'dog', 'soap', 'tree'] I have also wrote a program like this in C, but i'm not

Python - Convert partial sublist's elements into int

眉间皱痕 提交于 2019-12-01 13:02:09
Suppose you have a list like: [["a", "1", "2", "3"], ["b", "4", "5", "6"], ["c", "7", "8", "9"]] And I want to convert the elements from index 1 to 2 of every sublist into integers as you can see they are themselves strings. Is it possible? If it is, then what is the shortest way to do it? What have I done uptil now is this: lists = [["a", "1", "2", "3"], ["b", "4", "5", "6"], ["c", "7", "8", "9"]] for l in lists: l[1:4] = [int(x) for x in l[1:4]] print(lists) If you want to convert the lists inplace, your code is good enough. BTW, the list comprehension can be replaced with map : l[1:4] = map

Python - Convert partial sublist's elements into int

ぐ巨炮叔叔 提交于 2019-12-01 11:40:21
问题 Suppose you have a list like: [["a", "1", "2", "3"], ["b", "4", "5", "6"], ["c", "7", "8", "9"]] And I want to convert the elements from index 1 to 2 of every sublist into integers as you can see they are themselves strings. Is it possible? If it is, then what is the shortest way to do it? What have I done uptil now is this: lists = [["a", "1", "2", "3"], ["b", "4", "5", "6"], ["c", "7", "8", "9"]] for l in lists: l[1:4] = [int(x) for x in l[1:4]] print(lists) 回答1: If you want to convert the

How to get the nth element of each item of a list, which is itself a vector of unknown length

杀马特。学长 韩版系。学妹 提交于 2019-12-01 05:52:35
If we have a list, and each item can have different length. For example: l <- list(c(1, 2), c(3, 4,5), c(5), c(6,7)) (In order to be clear, we will call objects in a list "items", and objects in the objects of list "elements".) How can we extract, for example the first element of each item? Here, I want to extract: 1, 3, 5, 6 Then same question for the second element of each item: 2, 4, NA, 7 We can create a function using sapply fun1 <- function(lst, n){ sapply(lst, `[`, n) } fun1(l, 1) #[1] 1 3 5 6 fun1(l, 2) #[1] 2 4 NA 7 data.table::transpose(l) will give you a list with vectors of all 1st