This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 6 months ago.
Suppose I have a list of lists, like [[1, 2], [3, 4], [5, 6], [7, 8]]. What is the most elegant way in python to get [1, 2, 3, 4, 5, 6, 7, 8]?
myCombinedList = []
[myCombinedList.extend(inner) for inner in mylistOfLists]
Or:
import itertools
myCombinedIterable = itertools.chain.from_iterable(mylistOfLists)
myCombinedList = list(myCombinedIterable)
res=[]
for item in mylistOfList:
res+=item
Related
This question already has answers here:
Flatten an irregular (arbitrarily nested) list of lists
(51 answers)
Closed 3 years ago.
Let's say I have a list of lists.
>>> my_list = [[1, 2, 3], [4, 5, 6], [7, 8, [9, 0]]]
Is there a way to take all the items from that list and make it a single, un-nested list?
Something like this:
>>> break_up(my_list)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
This uses recursion
def appnd(new_list = [], list_to_append=[]):
for i in list_to_append:
if type(i) == list:
appnd(new_list=new_list, list_to_append=i)
else:
new_list.append(i)
def main():
my_list = [[1, 2, 3], [4, 5, 6], [7, 8, [9, 0]]]
new_list = []
appnd(new_list=new_list, list_to_append=my_list)
print(new_list)
if __name__ == '__main__':
main()
This question already has answers here:
Python -Intersection of multiple lists?
(6 answers)
Closed 4 years ago.
def query_RR(postings, qtext):
words = tokenize(qtext)
allpostings = [postings[w] for w in words]
for a in allpostings:
print a.keys()
And this was the result of the query [0, 2, 3, 4, 6] [1, 4, 5] [0, 2, 4] [4, 5]
The query is taking a user inputted term ('qtext'), tokenizing and generating a postings list for each token.
The posting list is a list of nested dictionaries (e.g. [{0 : 0.68426, 1: 0.26423}, {2: 0.6842332, 0: 0.9823}]. I am attempting to find the intersection for these nested dictionaries using the keys
Assuming the order does not matter, you could use set.intersection():
>>> lst = [[0, 2, 3, 4, 6], [1, 4, 5], [0, 2, 4], [4, 5]]
>>> set.intersection(*map(set,lst))
{4}
>>> set(lst[0]).intersection(*lst[1:])
{4}
This question already has answers here:
Rolling or sliding window iterator?
(29 answers)
Closed 6 years ago.
I would like to reorder a python list:
a = [1,2,3,4,5,6,7,...]
to the following form:
[[1,2,3],[2,3,4],[3,4,5],...]
What is the fastest way to do that?
you can try:
>>> a = [1,2,3,4,5,6,7]
>>> new_list = []
>>> for index in range(len(a)-2):
new_list.append(a[index:index+3])
>>> new_list
[[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7]]
This question already has answers here:
How do I reverse a list or loop over it backwards?
(37 answers)
Closed 7 years ago.
I was wondering how to reverse a list of lists in python. For example,
Original: list = [[1,2,3],[4,5,6],[7,8,9]]
Output: new_list = [[7,8,9],[4,5,6],[1,2,3]]
Right now, I am trying this:
new_list = reversed(list)
However, this doesn't seem to work.
In [24]: L = [[1,2,3],[4,5,6],[7,8,9]]
In [25]: L[::-1]
Out[25]: [[7, 8, 9], [4, 5, 6], [1, 2, 3]]
This question already has answers here:
How do I split a list into equally-sized chunks?
(66 answers)
Closed 8 years ago.
Is there an easy way to convert a 1D list into a 2D list with a given row length?
Suppose I have a list like this:
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
I want to convert the above list into a 3 x 3 table like below:
myList = [[1,2,3],[4,5,6],[7,8,9]]
I know I can accomplish this by creating a new 2D list called myList2 and inserting the element into MyList2 using 2 nested for loops. Like MyList2[x][y] = myList[i] i++
I think there should be a better way to do it (without using external libraries or modules)
Thanks!
Using list comprehension with slice:
>>> myList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> n = 3
>>> [myList[i:i+n] for i in range(0, len(myList), n)]
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]