sublist

Most efficient way to get indexposition of a sublist in a nested list

好久不见. 提交于 2019-12-25 03:16:40
问题 I want to get the indexposition of a sublist in a nested list, e.g 0 for [1,2] in nested_ls = [[1,2],[3,4]]. What's the most elegant way of getting the index of the sublist, is there something similar to index() ? 回答1: Yes. It's called index . >>> [[1, 2], [3, 4]].index([1, 2]) 0 >>> [[1, 2], [3, 4]].index([3, 4]) 1 >>> [[1, 2], [3, 4]].index([1, 5]) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: [1, 5] is not in list 来源: https://stackoverflow.com/questions

Issue Saving and Loading to/from File in Java

核能气质少年 提交于 2019-12-25 01:28:40
问题 I have a similar question posted, it was getting messy and this will be much easier to follow. The part that is in the Runner() that is / commented out /. You un-comment this, run the program once, check you console print out. Then / comment it out / again, and run the program a second time, you will notice that the subCodebook = {} is returning a blank/null; This is my question, why is this returning null. import java.io.FileInputStream; import java.io.FileOutputStream; import java.io

How do you find common sublists between two lists? [duplicate]

女生的网名这么多〃 提交于 2019-12-23 18:10:25
问题 This question already has answers here : Finding intersection/difference between python lists (6 answers) Closed 5 years ago . How do you find or keep only the sublists of a list if it the sublist is also present within another list? lsta = [['a','b','c'],['c','d','e'],['e','f','g']] lstb = [['a','b','c'],['d','d','e'],['e','f','g']] I'd like to do something like set(lsta) & set(lstb) Desired_List = [['a','b','c'],['e','f','g']] The reason I'd like to do something like set is for it's speed

Breaking up a list into sublists with recursion

和自甴很熟 提交于 2019-12-23 03:24:07
问题 I'm trying to write a function with the type declaration [(Int, Bool)] -> [[Int]] . I want the function to only add Int s to the same nested sublist if the Boolean is True . However if the Boolean is False , I want the Int associated with the next True bool to be added to a new sublist. For example: An input of [(1,True),(2,True),(3,False),(4,True),(5,False),(6,False),(7,True)] should return [[1,2],[4],[7]]. My code so far: test:: [(Int, Bool)] -> [[Int]] test xs = case xs of []->[] x:xs |

Python, comparison sublists and making a list

家住魔仙堡 提交于 2019-12-20 03:24:06
问题 I have a list that contains a lot of sublists. i.e. mylst = [[1, 343, 407, 433, 27], [1, 344, 413, 744, 302], [1, 344, 500, 600, 100], [1, 344, 752, 1114, 363], [1, 345, 755, 922, 168], [2, 345, 188, 1093, 906], [2, 346, 4, 950, 947], [2, 346, 953, 995, 43], [3, 346, 967, 1084, 118], [3, 347, 4, 951, 948], [3, 347, 1053, 1086, 34], [3, 349, 1049, 1125, 77], [3, 349, 1004, 1124, 120], [3, 350, 185, 986, 802], [3, 352, 1018, 1055, 38]] I want to start categorizing this list firstly and making

.NET equivalent of Java's List.subList()?

别说谁变了你拦得住时间么 提交于 2019-12-19 05:13:59
问题 Is there a .NET equivalent of Java's List.subList() that works on IList<T> ? 回答1: using LINQ list.Skip(fromRange).Take(toRange - fromRange) 回答2: For the generic List<T> , it is the GetRange(int, int) method. Edit: note that this is a shallow copy, not a 'view' on the original. I don't think C# offers that exact functionality. Edit2: as Kamarey points out, you can have a read-only view: List<int> integers = new List<int>() { 5, 6, 7, 8, 9, 10, 11, 12 }; IEnumerable<int> view = integers.Skip(2)

Split a list into two sublists in all possible ways

大兔子大兔子 提交于 2019-12-17 23:35:48
问题 I have a list of variable size, for example [1, 2, 3, 4] and I want to get every possible way to split this list into two: ([], [1, 2, 3, 4]) ([1], [2, 3, 4]) ([2], [1, 3, 4]) ([3], [1, 2, 4]) ([4], [1, 2, 3]) ([1, 2], [3, 4]) ([1, 3], [2, 4]) ([1, 4], [2, 3]) ([2, 3], [1, 4]) ([2, 4], [1, 3]) ([3, 4], [1, 2]) ([1, 2, 3], [4]) ([1, 2, 4], [3]) ([1, 3, 4], [2]) ([2, 3, 4], [1]) ([1, 2, 3, 4], []) I'm pretty sure this is not an unknown problem and there is probably an algorithm for this,

Make Python Sublists from a list using a Separator

百般思念 提交于 2019-12-17 06:54:05
问题 I have for example the following list: ['|', u'MOM', u'DAD', '|', u'GRAND', '|', u'MOM', u'MAX', u'JULES', '|'] and want it to be split by the "|" so the result would look like: [[u'MOM', u'DAD'],[ u'GRAND'], [u'MOM', u'MAX', u'JULES']] How can I do this? I only find examples of sublists on the net which need a length of the elements 回答1: >>> [list(x[1]) for x in itertools.groupby(['|', u'MOM', u'DAD', '|', u'GRAND', '|', u'MOM', u'MAX', u'JULES', '|'], lambda x: x=='|') if not x[0]] [[u'MOM'

ArrayList : Find nth occurrence of an Integer

冷暖自知 提交于 2019-12-14 03:52:53
问题 What is the best way to find nth occurrence of a number in ArrayList? What I know already? To find lastIndexOf the number there is method in List interface, which is implemented in ArrayList class. To find first occurence there is indexOf method. What I was solving? In a problem there was a list with different numbers and I have to return index of two numbers whose sum is equal to target number. Ex: List = (1,2,1) & target = 2; Now 1 + 1 =2 and answer will be index of first 1 and second 1.

How to order a list in sublists in Lisp?

谁说胖子不能爱 提交于 2019-12-13 22:24:31
问题 I have a list like this: (4 5 6 3 12 22 4 4 55 43 1 4 0) and want an output like this: ((4 5 6) (3) (12 22) (4) (4 55) (43) (1 4) (0)) I think you can guess the order, it has an ascending order, I'm totally new with Lisp and need some help 回答1: Here is one possible solution in TXR Lisp: (defun ascending-partitions (list) (let ((indices (mappend (do if (>= @1 @2) (list @3)) list (cdr list) (range 1)))) (split list indices))) We obtain the numeric index positions of the elements in the list