Related
I'm trying to solve this problem here: https://codingbat.com/prob/p252079?parent=/home/peter#norvig.com
In math, a "combination" of a set of things is a subset of the things. We define the function combinations(things, k) to be a list of all the subsets of exactly k elements of things. Conceptually, that's all there is, but there are some questions to settle: (A) how do we represent a subset? (B) What order are the elements within each subset? (C) What order to we list the subsets? Here's what we will agree to: (A) a subset will be a list. (B) The order of elements within a list will be the same as the order within 'things'. So, for example, for combinations([1, 2, 3], 2) one of the subsets will be [1, 2]; whereas [2, 1] is not a subset. (C) The order of subsets will be lexicographical or sorted order -- that is, combinations([1, 2, 3], 2) returns [ [1, 2], [1, 3], 2, 3] ] because [1, 2] < [1, 3] < [2, 3]. You might want to use the function 'sorted' to make sure the results you return are properly ordered.
combinations([1, 2, 3, 4, 5], 2) → [[1, 2], [1, 3], [1, 4], [1, 5], [2, 3], [2, 4], [2, 5], [3, 4], [3, 5], [4, 5]]
combinations([1, 2, 3], 2) → [[1, 2], [1, 3], [2, 3]]
combinations([1, 2, 3, 4, 5, 6], 5) → [[1, 2, 3, 4, 5], [1, 2, 3, 4, 6], [1, 2, 3, 5, 6], [1, 2, 4, 5, 6], [1, 3, 4, 5, 6], [2, 3, 4, 5, 6]]
Here's my code:
def combinations(things, k):
if k == 0 or k == len(things):
return [things]
elif len(things) < k:
return
else:
finalcomb = []
subcomb1 = combinations(things[1:], k - 1)
subcomb2 = combinations(things[1:], k)
for i in range(len(combinations(things[1:], k - 1))):
firstelement = [things[0]]
firstelement += combinations(things[1:], k - 1)[i]
finalcomb.append(firstelement)
for j in range(len(combinations(things[1:], k))):
finalcomb.append(combinations(things[1:], k)[j])
return finalcomb
However, this is the output:
Haven't hit 10 reputation yet so it's a link to the error. I'm not sure what I did wrong, can anybody help me out? Thank you so much.
The problem is this. When k == 0 it shouldn't return [things]. It should return an empty array. Similar to when len(things) < k:. This is because, when k == 0, it means we that we have already found all the numbers for that specific combination.
But there's one more problem. We're returning an empty array. However, in the for loops, we're iterating over the returned array. So if the array is empty, nothing happens. So what we should really return is an empty 2D array. I won't go into too much detail about what the problem is since it's better for you to try and understand why it's not working. Try adding print statements inside and outside the for loops.
Anyway, the working code looks like this:
def combinations(things, k):
if k == len(things):
return [things[:]]
if len(things) < k or k == 0:
return [[]]
finalcomb = []
subcomb1 = combinations(things[1:], k - 1)
subcomb2 = combinations(things[1:], k)
for comb in subcomb1:
firstelement = [things[0]]
firstelement += comb
finalcomb.append(firstelement)
finalcomb += subcomb2
return finalcomb
Note a few things:
Use the variables you've already assigned (I'm assuming you forgot about them)
Lists can be concatenated using +, similar to strings. If you return within an if statement, you don't need an else for the next line since if the if statement is satisfied, it would definitely not go to the else.
You simply can try using itertools:
import itertools
output = []
for nums in itertools.combinations([1, 2, 3, 4, 5], 2):
output.append(list(nums))
print(output)
output:
[[1, 2], [1, 3], [1, 4], [1, 5], [2, 3], [2, 4], [2, 5], [3, 4], [3, 5], [4, 5]]
For 3 nums:
import itertools
output = []
for nums in itertools.combinations([1, 2, 3, 4, 5], 3):
output.append(list(nums))
print(output)
Output:
[[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3, 5], [1, 4, 5], [2, 3, 4], [2, 3, 5], [2, 4, 5], [3, 4, 5]]
I have the following code to remove from the data list all sublists for which nums is a subset.and I dont understand why its not working:
data=[[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]
nums=[1,2]
for each in data:
if set(nums).issubset(each):
data.remove(each)
print(data)
>>[[1, 2, 4], [1, 3, 4], [2, 3, 4]]
Why isn't [1,2,4] being removed when nums is a subset of it, as seen below?
set(nums).issubset([1,2,4])
>>True
You're modifing the list you're iterating from.
This is a nicer solution:
data=[[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]
nums=[1,2]
data = [each for each in data if not set(nums).issubset(each)]
print(data)
For learning purposes, see this code which also works. The difference with your code is that here we're not modifying data list in the for loop.
data=[[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]
nums=[1,2]
new_data = []
for each in data:
if not set(nums).issubset(each):
new_data.append(each)
data = new_data
print(data)
Because the iterator is unaware that you removed an element. When it passes to the second element, it finds [1, 3, 4] meaning that you skipped [1, 2, 4].
For your information, there is also a filterfalse function in the very useful itertools module.
from itertools import filterfalse
data = list(filterfalse(set(nums).issubset, data))
Let's say
>>> a = [1,2,3,4,5]
And I want an output like
>>> b
[[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5]]
Here is my code:
class Child(object):
def get_lines(self):
a = [1,2,3,4,5]
b=[]
c=[]
j=0
for i in a:
print i
b.append(i)
print b
c.insert(j,b)
j=j+1
print c
son= Child()
son.get_lines()
When I print list b in loop, it gives:
1
[1]
2
[1, 2]
3
[1, 2, 3]
4
[1, 2, 3, 4]
5
[1, 2, 3, 4, 5]
and the output is:
[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]
Where do I make wrong in the code?
b is always the same list object (note I've changed print c to return c):
>>> map(id, Child().get_lines())
...
[49021616, 49021616, 49021616, 49021616, 49021616]
c contains five references to the same list. I think what you want is:
class Child(object):
def get_lines(self):
a = [1, 2, 3, 4, 5]
return [a[:x+1] for x in range(len(a))]
This makes a shallow copy of part (or all) of a for each step:
>>> Child().get_lines()
[[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5]]
Replace:
c.insert(j,b)
with:
c.append(b[:])
and try again.
You need to make a copy of b. Otherwise you add the same b again and again resulting in the full list at all indices. 'b[:]' copies the list.
This solution does the same but is a bit shorter:
a = [1, 2, 3, 4, 5]
c = []
for i in range(1, len(a) + 1):
c.append(a[:i])
now c is:
[[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5]]
a[:i] slices the list a from the beginning to excluding the index i.
In this case it does a[:1], a[:2] and so on. a[:1] makes a new list [1],
a[:2] a new list [1, 2,] and so on. Using append() is simpler and insert().
Another alternative is a list comprehension:
a = [1, 2, 3, 4, 5]
[a[:i] for i in range(1, len(a) + 1)]
also results in:
[[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5]]
Here in this case you are not supposed to append value to the list object (b).
since list is mutable, it will refer to the exact same object in memory until a reassign occurs.
>>> b=[]
>>> b.append(1)
>>> id(b)
4337935424
>>> b.append(2)
>>> id(b)
4337935424
>>> b.append(3)
>>> id(b)
4337935424
>>> b = [1, 2, 3, 4]
>>> id(b)
4337942608
so that in your code c will make five references to the same list.
It will instruct a new object and then (to the extent possible) inserts references into it to the objects found in the original.
>>> class Child(object):
...
... def get_lines(self):
... a = [1, 2, 3, 4, 5]
... return map(lambda x: a[:x+1], range(len(a)))
...
>>> Child().get_lines()
[[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5]]
You insert same b five times in list c. As a list actually contains references to objects and not copies, you have 5 times a reference to a same b list in which you have added successively 1, 2, 3, 4, 5. So the result.
You must instead add copies of list b :
def get_lines(self):
a = [1,2,3,4,5]
b=[]
c=[]
j=0
for i in a:
print i
b.append(i)
print b
c.insert(j,b[:]) # forces insertion of a copy
j=j+1
print c
In the loop, value of b persists since it's a mutable object. Hence when you print c, the last value of b is shown.
Instead of using b as temporary variable, you can directly use a as follows:
class Child(object):
def get_lines(self):
a = [1,2,3,4,5]
b = []
for index, element in enumerate(a, 1):
b.append(x[:index])
return b
son= Child()
son.get_lines()
This question already has answers here:
How to group a list of tuples/objects by similar index/attribute in python?
(3 answers)
Closed 8 years ago.
I see splitting-a-list-of-arbitrary-size-into-only-roughly-n-equal-parts. How about not-equal splitting? I have list having items with some attribute (value which can be retrieved for running same function against every item), how to split items having same attribute to be new list e.g. new sublist? Something lambda-related could work here?
Simple example could be:
list = [1, 1, 1, 2, 3, 3, 3, 3, 4, 4]
After fancy operation we could have:
list = [[1, 1, 1], [2], [3, 3, 3, 3], [4, 4]]
>>> L = [1, 1, 1, 2, 3, 3, 3, 3, 4, 4]
>>> [list(g) for i, g in itertools.groupby(L)]
[[1, 1, 1], [2], [3, 3, 3, 3], [4, 4]]
>>> L2 = ['apple', 'aardvark', 'banana', 'coconut', 'crow']
>>> [list(g) for i, g in itertools.groupby(L2, operator.itemgetter(0))]
[['apple', 'aardvark'], ['banana'], ['coconut', 'crow']]
You should use the itertools.groupby function from the standard library.
This function groups the elements in the iterable it receives (by default using the identity function, i.e., checking consequent elements for equality), and for each streak of grouped elements, it reutrns a 2-tuple consisting of the streak representative (the element itself), and an iterator of the elements within the streak.
Indeed:
l = [1, 1, 1, 2, 3, 3, 3, 3, 4, 4]
list(list(k[1]) for k in groupby(l))
>>> [[1, 1, 1], [2], [3, 3, 3, 3], [4, 4]]
P.S. you should avoid using list as a variable name, as it would conflict with the built-in type/function.
Here's a pretty simple roll your own solution. If the 'attribute' in question is simply the value of the item, there are more straightforward approaches.
def split_into_sublists(data_list, sizes_list):
if sum(sizes_list) != len(data_list):
raise ValueError
count = 0
output = []
for size in sizes_list:
output.append(data_list[count:count+size])
count += size
return output
if __name__ == '__main__':
data_list = [1, 1, 1, 2, 3, 3, 3, 3, 4, 4]
sizes_list = [3,1,4,2]
list2 = [[1, 1, 1], [2], [3, 3, 3, 3], [4, 4]]
print(split_into_sublists(data_list, sizes_list) == list2) # True
Say we create a list like so in python:
[[1, 2, 3], [1, 3, 4], [2, 4, 5]]
And then I want to take 1+1+2 and divide by 3, giving me the average for that element and store in a new list. I want to do that again for the second elements and lastly for the third. How would one do it succinctly? (I cannot think of a way other than multiple loops.)
The output should be a new list [(1+1+2), (2+3+4), (3+4+5)]
Thanks so much!
Averages:
>>> data = [[1, 2, 3], [1, 3, 4], [2, 4, 5]]
>>> from __future__ import division
>>> [sum(e)/len(e) for e in zip(*data)]
[1.3333333333333333, 3.0, 4.0]
Sums:
>>> data = [[1, 2, 3], [1, 3, 4], [2, 4, 5]]
>>> [sum(e) for e in zip(*data)]
[4, 9, 12]
zip
returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables.
Unpacking argument lists
when the arguments are already in a list or tuple but need to be unpacked for a function call requiring separate positional arguments ... write the function call with the *-operator to unpack the arguments out of a list or tuple.
>>> data
[[1, 2, 3], [1, 3, 4], [2, 4, 5]]
>>> zip(*data)
[(1, 1, 2), (2, 3, 4), (3, 4, 5)]
>>> l = [[1, 2, 3], [1, 3, 4], [2, 4, 5]]
>>> zip(*l)
[(1, 1, 2), (2, 3, 4), (3, 4, 5)]
>>> def average(nums, default=float('nan')):
... return sum(nums) / float(len(nums)) if nums else default
...
>>> [average(n) for n in zip(*l)]
[2.0, 2.6666666666666665, 3.6666666666666665]