sublist

Removing a sublist from a list

。_饼干妹妹 提交于 2020-02-02 16:29:33
问题 I have a list e.g. l1 = [1,2,3,4] and another list: l2 = [1,2,3,4,5,6,7,1,2,3,4] . I would like to check if l1 is a subset in l2 and if it is, then I want to delete these elements from l2 such that l2 would become [5,6,7,1,2,3,4] , where indexes 0-3 have been removed. Is there a pythonic way of doing this? I tried this: l1 = [1,2,3,4] l2 = [1,2,3,4,5,6,7,1,2,3,4] l3 = [] for i in l2: if i in l1: l3.append(i) -> prints [5,6,7] However I would like the output to be [5,6,7,1,2,3,4] . 回答1: Well,

Find (and keep) duplicates of sublist in python

Deadly 提交于 2020-01-10 19:50:08
问题 I have a list of lists (sublist) that contains numbers and I only want to keep those exists in all (sub)lists. Example: x = [ [1, 2, 3, 4], [3, 4, 6, 7], [2, 3, 4, 6, 7]] output => [3, 4] How can I do this? 回答1: common = set(x[0]) for l in x[1:]: common &= set(l) print list(common) or: import operator print reduce(operator.iand, map(set, x)) 回答2: In one liner: >>> reduce(set.intersection, x[1:], set(x[0])) set([3, 4]) 回答3: def f(a, b): return list(set(a).intersection(set(b))) reduce(f, x) 回答4

Find (and keep) duplicates of sublist in python

蓝咒 提交于 2020-01-10 19:50:06
问题 I have a list of lists (sublist) that contains numbers and I only want to keep those exists in all (sub)lists. Example: x = [ [1, 2, 3, 4], [3, 4, 6, 7], [2, 3, 4, 6, 7]] output => [3, 4] How can I do this? 回答1: common = set(x[0]) for l in x[1:]: common &= set(l) print list(common) or: import operator print reduce(operator.iand, map(set, x)) 回答2: In one liner: >>> reduce(set.intersection, x[1:], set(x[0])) set([3, 4]) 回答3: def f(a, b): return list(set(a).intersection(set(b))) reduce(f, x) 回答4

Checking for sublist in list

时光怂恿深爱的人放手 提交于 2020-01-10 04:31:06
问题 The Question is: You are to write a function, called isSublist() , which takes two arguments ( list, sublist ) and returns 1 if sublist is a sub-list of list, and 0 otherwise. So i have my code however I get True when the sublist is not in list. Any suggestions on fixing this please? def isSublist(list, sublist): for i in range(len(list)-(len(sublist))+1): return True if sublist==list[i:i+(len(sublist))]: return False sample input: list= (0,1,2,3,4,5,6,7,8,9) isSublist(list, [1,2,3]) output:

Iterative in place sub-list Heap Sort python implementation

本秂侑毒 提交于 2020-01-06 06:53:10
问题 I've found different versions of heap sort for python, but I can't seem to find the one that matches my needs. Iterative Heap Sort is the closest I found, but I can't quite figure out how to change it to work with a sub-list (index start, index end) and remain in place. If I get it right then I'll post my answer here. If anyone has an implementation in even C or Java that will be great. 回答1: I managed to do what I want. This code works on objects and sorts by a specific attribute. def

Split An Erlang List, X, into a List of All X's Sublists

大城市里の小女人 提交于 2020-01-05 05:37:27
问题 lists:sublist/2 and lists:sublist/3 make it simple to extract a single sublist from a list, but is there a BiF or module that returns a list of all of a list's sublists? i.e. lists:awesome_sublist_function([1,2,3,4]) -> [[1], [2], [3], [4], [1,2], [1,3], [1,4], [2,3], [2,4], [3,4], [1,2,3], [1,2,4], [1,3,4], [2,3,4], [1,2,3,4]] Can build my own, but wondered if the problem has been solved before anywhere? 回答1: I assume your test case is forgetting [1,3,4], but it could look something like

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

六月ゝ 毕业季﹏ 提交于 2019-12-30 09:42:17
问题 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 回答1: We can create a function using sapply fun1 <- function(lst, n){ sapply(lst, `[`, n) } fun1(l, 1)

Extract elements of multiple sublists in a list and transform to multiple dataframes

孤人 提交于 2019-12-25 16:44:21
问题 Hello Dear Community, Following this post, I am searching how to create multiple data frames from a list that contains multiple sublists with different names So, from a list called tier.col I would like to create a function tier2col that returns to multiple data.frames (as much as there is sublist in my list) containing specific elements of the sublist. I get how to do this manually as so : phones <- data.frame(tier.col$phones$xmin[-1,3], tier.col$phones$xmax[-1,3], tier.col$phones$text[3])

Arraylist Sublist IndexOutOfBounds Exception

試著忘記壹切 提交于 2019-12-25 05:13:49
问题 Working on below code to Modify HL7 messages and add some Hex Characters depending on condition i have written. Copying the String from the textbox and converting it to Arraylist so that i can traverse through it to find the names of the HL7 segments. To do so i am getting a sublist out of Arraylist. But when working with below code i am getting IndexOutOf Bounds exception. I know that when searching for Sublist my search is exceeding the size of Arraylist. Just need a tip to check if the

Haskell Filter Multiples of 3 from a List to a Sublist

独自空忆成欢 提交于 2019-12-25 04:36:17
问题 I am still trying to grasp the way Haskell and Functional Programming works, and I need help understanding why my function is not working. I am trying to create a function that takes a list of integers as a parameter and filters out/returns a sublist which contains any multiples of 3 from the first list. Here is my code: module Main where sublist = [] myFunc :: [Int] -> [Int] myFunc [] = [] myFunc [t] = do if t `mod` 3 == 0 then t : sublist else myFunc [] myFunc (h:t) = do if h `mod` 3 /= 0