sublist

Find starting and ending indices of sublist in list

和自甴很熟 提交于 2019-11-27 23:07:05
I have a list: greeting = ['hello','my','name','is','bob','how','are','you'] I want to define a function that will find the first and last index of a sublist in this list. Thus: find_sub_list(['my','name','is'], greeting) should return: 1, 3 Suggestions? If you want multiple matches, this works: greeting = ['hello','my','name','is','bob','how','are','you','my','name','is'] def find_sub_list(sl,l): results=[] sll=len(sl) for ind in (i for i,e in enumerate(l) if e==sl[0]): if l[ind:ind+sll]==sl: results.append((ind,ind+sll-1)) return results print find_sub_list(['my','name','is'], greeting) # [

Splitting a list into uneven groups?

痴心易碎 提交于 2019-11-27 16:05:15
问题 I know how to split a list into even groups, but I'm having trouble splitting it into uneven groups. Essentially here is what I have: some list, let's call it mylist , that contains x elements. I also have another file, lets call it second_list, that looks something like this: {2, 4, 5, 9, etc.} Now what I want to do is divide mylist into uneven groups by the spacing in second_list. So, I want my first group to be the first 2 elements of mylist , the second group to be the next 4 elements of

Why changes in sublist are reflected in the original list?

允我心安 提交于 2019-11-27 07:52:00
问题 I know that Collections in Java are mutable when you pass them through references. I want to know exactly what happens in memory addresses of original-list and sublist/s of it. Do the sublist and original list refer to the same object? Following is code sample reflecting changes made in sublist to main original list. List<String> list = new ArrayList<String>(); list.add("1"); list.add("2"); list.add(1, "3"); List<String> list2 = new LinkedList<String>(list); list.addAll(list2); list2 = list

Make Python Sublists from a list using a Separator

喜欢而已 提交于 2019-11-27 02:15:14
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 >>> [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', u'DAD'], [u'GRAND'], [u'MOM', u'MAX', u'JULES']] itertools.groupby() does this very nicely... >>> import

Find starting and ending indices of sublist in list

喜欢而已 提交于 2019-11-26 21:17:24
问题 I have a list: greeting = ['hello','my','name','is','bob','how','are','you'] I want to define a function that will find the first and last index of a sublist in this list. Thus: find_sub_list(['my','name','is'], greeting) should return: 1, 3 Suggestions? 回答1: If you want multiple matches, this works: greeting = ['hello','my','name','is','bob','how','are','you','my','name','is'] def find_sub_list(sl,l): results=[] sll=len(sl) for ind in (i for i,e in enumerate(l) if e==sl[0]): if l[ind:ind+sll