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]]
Related
This question already has answers here:
Copying nested lists in Python
(3 answers)
What is the difference between shallow copy, deepcopy and normal assignment operation?
(12 answers)
Closed 2 years ago.
a = [[1], [2, 3], [4, 5, 6]]
b = a[:]
a[0] = "x"
a[1][0] = 10
print(a)
print(b)
Why the result is (--why 10 changed the original object?)
['x', [10, 3], [4, 5, 6]]
[[1], [10, 3], [4, 5, 6]]
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:
How to add an integer to each element in a list?
(12 answers)
Closed 5 years ago.
A = [[1,2,3],[4,5,6]]
def addOne(a):
return a+1
addOne(A) doesn't work, obviously because I don't want A+1,
I want to get [[[2,3,4]],[5,6,7]]
[addone(x) for x in y for y in A] doesn't work either, I think list comprehensions doesn't work that way.
note, addOne is just a placeholder for a more complicated function
>>> nums = [[1, 2, 3], [4, 5, 6]]
>>> def add_one(a):
... return a + 1
...
>>> [list(map(add_one, sublist)) for sublist in nums]
[[2, 3, 4], [5, 6, 7]]
>>> [[add_one(num) for num in sublist] for sublist in nums]
[[2, 3, 4], [5, 6, 7]]
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