split list to 3 parts [duplicate] - python

This question already has answers here:
Equally distribute a list in python
(2 answers)
How do I split a list into equally-sized chunks?
(66 answers)
Closed 8 years ago.
I have objects list:
l = [1, 2, 3, 4, 5, 6]
i find some snippet.. http://herself.movielady.net/2008/07/16/split-list-to-columns-django-template-tag/
but they split like this:
[1, 2] [3, 4] [5, 6]
i need split list like this:
l1 = [1, 4]
l2 = [2, 5]
l3 = [3, 6]
Please, help build right templatetag.

You could gather your lists l1, l2 l3 in another list using a list comprehension, and afterwards do something to them. For example:
l = [1, 2, 3, 4, 5, 6]
x = [[l[i]] + [l[i+3]] for i in range(len(l) - 3)]
for a in x:
print(a)
will get you
[1, 4]
[2, 5]
[3, 6]
If you know x contains three lists, you can assign l1, l2, l3 with
l1, l2, l3 = x
Of course, you could just manually assign l1, l2, l3 too.
l1 = [l[0]] + [l[3]]
...

h = int(len(l)/2)
l1, l2, l3 = zip( l[:h], l[h:] )
l[:h] is the first half and l[h:] the second half. See list slices.
>>> l[:h], l[h:]
([1, 2, 3], [4, 5, 6])
Then the zip function, see zip.
>>> zip([1, 2, 3], [4, 5, 6])
[(1, 4), (2, 5), (3, 6)]

Related

How can I calculate numbers in a list?

For example, we have these lists:
l1 = [1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 4]
l2 = [11, 11, 2]
l3 = [5, 6]
l4 = [1, 2, 3, 3, 3]
Here, l2 and l3 have 2 numbers (11 and 2 for l2, and 5 and 6 for l3)
l1 has 4 numbers (1, 2, 3, and 4)
l4 has 3 numbers (1, 2, 3)
How can I calculate that? (assuming the list is unknown)
Thank you!
Convert the lists to sets and use len() to see how many unique numbers are in each of them. Something like this:
>>> l1 = [1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 4]
>>> l2 = [11, 11, 2]
>>> l3 = [5, 6]
>>> l4 = [1, 2, 3, 3, 3]
>>> len(set(l1))
4
>>> len(set(l2))
2
>>> len(set(l3))
2
>>> len(set(l4))
3
>>>
And to see the unique elements, the following will show you them:
>>> set(l1)
{1, 2, 3, 4}
>>>
print(len(list(dict.fromkeys(l2)))
explanation:
len - method that return length of list
list - converts dict to list
dict.fromkeys - create dictionary from given list
(values that repeat will be appended to dictionary just once because its dictionary :) it couldnt have two definitions of one variable )
You can achieve this by using list of list:
def numbersList(l1):
return len(set(l1))
nList = [[1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 4],[11, 11, 2], [5, 6], [1, 2, 3, 3, 3]]
for l in nList:
print(numbersList(l))
# if you want to add more list to list
print("After Append")
nList.append([3,3,3,5,5,5,6,6,6,7,7,7,8,8,9,10])
for l in nList:
print(numbersList(l))

Python: Generator that yields lists, problems with list() and list comprehension [duplicate]

This question already has an answer here:
Python: How to append generator iteration values to a list
(1 answer)
Closed 4 years ago.
Suppose I want to write a generator, that returns lists, for example to iterate over list permutations. Take the following simple example:
def list_gen():
foo = [1,2,3,4]
for i in range(5, 9):
foo[1] = i
yield foo
bar = list(list_gen())
print(bar)
bar = [l for l in list_gen()]
print(bar)
for l in list_gen():
print(l, end=' ')
The output is:
[[1, 8, 3, 4], [1, 8, 3, 4], [1, 8, 3, 4], [1, 8, 3, 4]]
[[1, 8, 3, 4], [1, 8, 3, 4], [1, 8, 3, 4], [1, 8, 3, 4]]
[1, 5, 3, 4] [1, 6, 3, 4] [1, 7, 3, 4] [1, 8, 3, 4]
So with the for loop everything works as expected, but with list() or list comprehension all values are equal to the last one. As I understand, this is because lists are mutable and all elements of bar are pointing to the same object.
One possible workaround could be bar = [list(l) for l in list_gen()], but it seems rather ugly. Is there a better way to deal with this problem?
A simple solution for this specific problem is to return a new list, rather than the foo object.
def list_gen():
foo = [1,2,3,4]
for i in range(5, 9):
foo[1] = i
yield list(foo)
Disclaimer: I don't use generators a lot, so this could be against best practice. But it solves the problem.

Change the data structure [duplicate]

This question already has answers here:
Combine two lists into one multidimensional list
(4 answers)
Closed 5 years ago.
I have 2 lists with the same size. I want to make a new list which the combination of both of them (list of lists) in a way that elements with the same indexes would be in a list and this list would have the same index.
input example:
a = [1, 2, 3]
b = [4, 5, 6]
combined = [[1, 4], [2, 5], [3, 6]]
do you know how to do that?
Using the built-in zip:
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> zip(a, b)
[(1, 4), (2, 5), (3, 6)]
This gives you the list.
combined = [[i,k] for i,k in zip(a,b)]
this will give you list of tuples
combined = list(zip(a, b))
if you really need your elements to be lists then we can write
combined = list(map(list, zip(a, b)))
Use zip
>>> list(zip(a,b))
[(1, 4), (2, 5), (3, 6)]
Or you want list instead of tuples :
>>> [[x,y] for x,y in zip(a,b)]
[[1, 4], [2, 5], [3, 6]]
You can zip them:
list(zip(a, b))
a = [1, 2, 3]
b = [4, 5, 6]
combined = list(zip(a,b))
for i in combined:
print(i)
Use zip command to combine both the list.

Python, match by index some lists into a list of lists [duplicate]

This question already has answers here:
How do I iterate through two lists in parallel?
(8 answers)
Combining lists into one [duplicate]
(8 answers)
Closed 6 years ago.
So I have something like :
l1=[1,2,3]
l2=[4,5,6]
l3=[7,8,9]
Expected output is : ls=[[1,4,7],[2,5,8],[3,6,9]]
What it will be the most corect way to do that?
Use zip and then list comprehension to turn the tuples into lists
[list(x) for x in zip(l1, l2, l3)]
Result:
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
>>> l1=[1,2,3]
>>> l2=[4,5,6]
>>> l3=[7,8,9]
>>> zip(l1, l2, l3)
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
The built-in function zip will help you with what you want.
zip the three lists:
>>> l1 = [1,2,3]
>>> l2 = [4,5,6]
>>> l3 = [7,8,9]
>>> zip(l1,l2,l3)
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
Use a list comprehension to cast the tuples into lists to have a list of lists:
>>> [list(i) for i in zip(l1,l2,l3)]
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

sum two lists element-by-element in python recursively [duplicate]

This question already has answers here:
Element-wise addition of 2 lists?
(17 answers)
Closed 8 years ago.
Is it possible to recursively sum two lists element by element and then return the new list?
def sumListElements(listOne, listTwo):
list = []
i = 0
while i < len(listOne):
list.append(listOne[i] + listTwo[i])
i += 1
return list
So,
a = [1, 2, 3]
b = [3, 4, 5]
Results
R = [4, 6, 8]
Here is a recursive implementation
def recursive_sum(l1, l2, idx = 0):
if idx < min(len(l1), len(l2)):
return [l1[idx] + l2[idx]] + recursive_sum(l1, l2, idx + 1)
else:
return []
print recursive_sum([1, 2, 3], [4, 5, 6])
# [5, 7, 9]
Or
def recursive_sum(l1, l2, result = None, idx = 0):
if result is None:
result = []
if idx < min(len(l1), len(l2)):
result.append(l1[idx] + l2[idx])
return recursive_sum(l1, l2, result, idx + 1)
else:
return result
Use zip() and map() here:
R = map(sum, zip(a, b))
Demo:
>>> a = [1, 2, 3]
>>> b = [3, 4, 5]
>>> map(sum, zip(a, b))
[4, 6, 8]
For Python 3 compatibility, replace map() with a list comprehension:
[sum(z) for z in zip(a, b)]
Function which takes n lists, and adds each element in an i-th index together with the others:
from itertools import izip_longest
def sum_elements(*lists):
return map(sum, izip_longest(*lists, fillvalue=0))
Showed with your data:
>>> sum_elements([1, 2, 3], [3, 4, 5])
[4, 6, 8]
Lists with uneven lengths still have a nice result:
>>> sum_elements([1, 2, 3], [3, 4, 5, 6])
[4, 6, 8, 6]
And it can take any number of lists:
>>> sum_elements([1, 2, 3], [3, 4, 5, 6], [8,9])
[12, 15, 8, 6]

Categories