Extract first item of each sublist in Python
Using list comprehension: >>> lst = [[‘a’,’b’,’c’], [1,2,3], [‘x’,’y’,’z’]] >>> lst2 = [item[0] for item in lst] >>> lst2 [‘a’, 1, ‘x’]
Using list comprehension: >>> lst = [[‘a’,’b’,’c’], [1,2,3], [‘x’,’y’,’z’]] >>> lst2 = [item[0] for item in lst] >>> lst2 [‘a’, 1, ‘x’]
Use the power of the zip function and list comprehensions: list1 = [(‘a’, ), (‘b’, ‘c’), (‘d’, ‘e’), (‘f’, ‘g’, ‘h’) ] list2 = [(‘p’, ‘q’), (‘r’, ‘s’), (‘t’, ), (‘u’, ‘v’, ‘w’) ] print [a + b for a, b in zip(list1, list2)]
What you need is to nest another <ui:repeat> tag in your outer iteration: <ui:repeat value=”#{bean.listOfA}” var=”a”> … <ui:repeat value=”#{a.listOfB}” var=”b”> … </ui:repeat> </ui:repeat> The only thing left that is worth noting is that nested <ui:repeat> tags used to have problems with state management until Mojarra 2.1.15 version (details in jsf listener not called inside nested … Read more
Here’s a brute-force approach (it might be easier to understand): from itertools import chain def condense(*lists): # remember original positions positions = {} for pos, item in enumerate(chain(*lists)): if item not in positions: positions[item] = pos # condense disregarding order sets = condense_sets(map(set, lists)) # restore order result = [sorted(s, key=positions.get) for s in sets] … Read more
Just use a list comprehension: nested_lst_of_tuples = [tuple(l) for l in nested_lst] Demo: >>> nested_lst = [[‘tom’, ‘cat’], [‘jerry’, ‘mouse’], [‘spark’, ‘dog’]] >>> [tuple(l) for l in nested_lst] [(‘tom’, ‘cat’), (‘jerry’, ‘mouse’), (‘spark’, ‘dog’)]
Iterate through every sub-list in your original list and unpack it in the print call with *: a = [[1, 3, 4], [2, 5, 7]] for s in a: print(*s) The separation is by default set to ‘ ‘ so there’s no need to explicitly provide it. This prints: 1 3 4 2 5 7 … Read more
Assuming you have something like the following: myList <- list(`0` = c(`1` = 10, `2` = 20, `3` = 30, `4` = 72), `1` = c(`1` = 15, `2` = 9, `3` = 7)) myList # $`0` # 1 2 3 4 # 10 20 30 72 # # $`1` # 1 2 3 # … Read more
Using generator functions can make your example easier to read and improve performance. Python 2 Using the Iterable ABC added in 2.6: from collections import Iterable def flatten(xs): for x in xs: if isinstance(x, Iterable) and not isinstance(x, basestring): for item in flatten(x): yield item else: yield x Python 3 In Python 3, basestring is … Read more
You can also use (at least v1.9.3) of rbindlist in the data.table package: library(data.table) rbindlist(mylist, fill=TRUE) ## Hit Project Year Rating Launch ID Dept Error ## 1: True Blue 2011 4 26 Jan 2012 19 1, 2, 4 NA ## 2: False NA NA NA NA NA NA Record not found ## 3: True Green … Read more
A strange behaviour indeed, but that’s only because * operator makes shallow copies, in your case – shallow copies of [0, 0, 0] list. You can use the id() function to make sure that these internal lists are actually the same: out=[[0]*3]*3 id(out[0]) >>> 140503648365240 id(out[1]) >>> 140503648365240 id(out[2]) >>> 140503648365240 Comprehensions can be used … Read more