Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Say I've a list with a fixed size and elements.
I wish to create set of all possible combinations of the elements inside the array. How do I achieve this?
In python I've tried to use itertools like this:
import itertools
a = [4, 7]
for L in range(0, len(a)+1):
for subset in itertools.combinations(a, L):
print(subset)
Howsoever, this gives me an output(),(4),(7),(4,7) as in tuples. I want 4,7,47,74 as my desired output in a list or array(C++) and not to print them after each loop iteration.
I don't know if that's permutation or combination. So kindly bear with me and help.
From your sample output (which by the way is not in agreement with your input), it is clear that you don't need combinations but permutations from itertools. You can use join to merge your two numbers for printing purpose.
Most importantly, you should start your loop over L from 1 because you need at least length of 1 to generate the permutations other wise you will get empty lists (tuples) as you mentioned in your question.
import itertools
a = [4, 7]
lst = []
for L in range(1, len(a)+1):
for subset in itertools.permutations(a, L):
lst.append(''.join(str(x) for x in subset))
for l in lst:
print (l)
Output
4
7
47
74
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
list = [[2,4], [4,3], [7,6], [8,2], [9,11], [10,11], [13,18], [10,23]]
I have a 2D array. If there is any common element in these nested arrays, I want to include the common element and the element next to it in a new array. I give an example array above.
I want to add common elements and neighbors to an array like below.
list_dup_out = [[2,3,4,8], [9,10,11,23]]
Can you share a sample python code about this?
I'd keep a dict of all the associations and then dump the items with multiple associations into a set to dedupe them:
>>> arr = [[2,4], [4,3], [7,6], [8,2], [9,11]]
>>> from collections import defaultdict
>>> d = defaultdict(list)
>>> for x in arr:
... for y in x:
... for z in x:
... if y != z:
... d[y].append(z)
...
>>> sorted({y for x in d.values() for y in x if len(x) > 1})
[2, 3, 4, 8]
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
This is my first post so I will most likely do something wrong. I am wondering if there is a faster way to remove items from a list based on their position. I am relatively new to python so I most likely have overlooked a faster way.
Stack3 = (Cards[0],Cards[1],Cards[2])
while Count < 3:
Cards.pop(0)
Count += 1
Count = 0
This is what my current code is. As you can see, I am just removing the first item from the list many times over. This seems very messy and unorganized to me. I would really appreciate any help, thanks!
Removing elements from a list while looping is not a good practice. A better way to do it is to use a list comprehension:
lst = [1,2,3,4,5,6,7]
indices_to_remove = [3,4,1]
lst = [elem for index,elem in enumerate(lst) if index not in indices_to_remove]
Output:
>>> lst
[1, 3, 6, 7]
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a list like this:
my_lst = [[1,2,3,4]]
I was wondering how can I remove [] from the list to get the following list:
my_lst = [1,2,3,4]
The double [[]] is because you have a list containing a list. To get out the inner list, just index it:
my_lst = my_lst[0]
or unpack it:
[my_lst] = my_lst
Unpacking has the mild advantage that it will give you an error if the outer list doesn't not contain exactly one value (while my_lst[0] will silently discard any extra values).
There are many other options.
my_lst = my_lst[0]
You have a list with one element, which is itself a list. You want the variable to just be that first element, the list 1,2,3,4.
you have a list of lists.
my_lst = my_lst[0] #get the first list of the list
You can use itertools to achieve that without writing your explicit loop if the length of the outer list could be more than one.
In [47]: import itertools
In [49]: list(itertools.chain.from_iterable([[1, 2, 3], [1, 2]]))
Out[49]: [1, 2, 3, 1, 2]
You can reduce 1 dimension using sum.
my_lst=sum(my_lst,[])
# [1,2,3,4]
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a list L and want to return a list of n items from it, at random.
Right now I am relying on itertools.combinations and then picking one at random, but I have to wait a while until the list looks like something that isn't stuck with a lot of items near the start of the list, so it's not really "random."
use random.sample to sample K items from a list of population n.
>>> import random
>>> random.sample(range(100), 5)
[56, 1, 0, 60, 61]
from random import randint
L=[1,2,3,4,5,6]
a=len(L)
n=5
K=[ L[randint(0,a-1)%a] for x in xrange(0,n)]
print K
Sample output
[6, 4, 3, 2, 5]
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi guys i have a dictionary that looks like this:
[[['Test', 0,1],['Test2', 0,4],['Test3', 0,5],['Test4', 0,2],['Test5', 0,6],...]]
How can I get the top two (for example)?
Expected result:
[['Test3', 0,5],['Test5',0,6]]
Sort by the third value:
>>> sorted(l[0], key=lambda x: x[2])[-2:]
[['Test3', 0, 5], ['Test5', 0, 6]]
test = [[['Test', 0,1],['Test2', 0,4],['Test3', 0,5],['Test4', 0,2],['Test5', 0,6]]]
result = sorted(test[0], key=lambda x: x[2])[-2:]
print(result)
Output:
[['Test3', 0, 5], ['Test5', 0, 6]]
Something like this?
(I am assuming that you actually mean ['Test', 0, 1] to be a list with 3 elements which is to be sorted by the third element rather than a two element list with the second element being a float, in which case it should be ['Test', 0.1] and you can look at #jonrsharpe's answer. I also assumed that the output order (lowest to highest) is relevant.)
That is not a dictionary, it is a list ([]) containing another list which contains lists. Each sub-sub-list contains three elements:
['Test2', 0, 4]
#^0 ^1 ^2
Note that Python doesn't allow the user of commas as decimal points; I think you want 0.4.
Assuming that, sorting the sub-list and extracting the two highest sub-sub-lists is easy:
from operator import itemgetter
output = sorted(lst[0], key=itemgetter(1), reverse=True)[:2]
If I am wrong about the comma, make the argument to itemgetter 2.