Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I want the following output: [[1,2,1,2],[1,2,3,4],[3,4,1,2],[3,4,3,4]] but my code does not work out properly.
flag_2=[[1,2],[3,4]]
for i in flag_2:
icopy = copy.copy(i)
print "icopy",icopy
for j in flag_2:
temp4=[]
jcopy = copy.copy(j)
print "jcopy",jcopy
temp4=copy.copy(list_extend(icopy,jcopy))
print temp4
temp4=[]
print temp4
Use itertools' product to get the combinations you need and chain to flatten the result:
from itertools import product,chain
[list(chain.from_iterable(c)) for c in product(flag_2,repeat=2)]
Out[7]: [[1, 2, 1, 2], [1, 2, 3, 4], [3, 4, 1, 2], [3, 4, 3, 4]]
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 days ago.
Improve this question
Consider,
tp = (2, 3, 5, 7) # tuple
ls = [2, 3, 5, 7] # list
st = {2, 3, 5, 7} # set
Python has twin delimiters for different types of ensembles. As shown above, we have () for tuples, [] for lists and {} for sets. Is it possible to define new ones for something one wrote themselves or pre-written? E. g., say we wanted to define a numpy array np.array([2, 3, 5, 7]) as <2, 3, 5, 7>?
Edit: Not quite looking to define new infix relations. That's why I used the term twin delimiters.
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
Can someone help me writing a code in order to increasingly iterate on multiple iterators.
As an example imagine that I have several lists (for example : [1, 9] and [2, 3, 4]) and I want to get an iterable from those lists yielding 1, 2, 3, 4 and 9. Of course the idea is to use more big and complex iterators (otherwise it is easy to just merge and sort).
Thank you !
heapq is exactly designed for what you want : It creates an iterator, so you don't have to handle huge lists. Here is the code for a simple example :
import heapq
a = [1, 4, 7, 10]
b = [2, 5, 6, 11]
for c in heapq.merge(a, b):
print(c)
Of course, it works only if your lists are sorted, you have to sort them before if they are not sorted.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have 5 arrays with 6 questions in each one.
I need the script to pick 2 questions from each array and create an input() function. The part I can't seem to think of is how to make an output for a correct answer for the questions. I understand how a specified input would work but what about randomized.
I think you're looking for something like this:
randomNumber1=***some generated number (0 thru 6)
randomNumber2=***some generated number (0 thru 6)
array1=['what is the meaning of life', 'how far away is the sun',...]
array2=['what did is your favorite color', 'how many pennies are in 1 dollar'...]
q1=array1[randomNumber1]
q2=array2[randomNumber2]
input1=input(q1)
input2=input(q2)
#stores answers in a dictionary
answers={q1:input1, q2:input2}
I do not think the random module has the function that you want.
But it is easy to build one if you like. Python is easy.
Does this work?
import random
from typing import Iterable
def get_sub_random_list(sub_length: int, iterable: Iterable) -> list:
iterable_copy = list(iterable)
result = []
for __ in range(sub_length):
length = len(iterable_copy)
if length == 0:
raise ValueError(f"the iterable should longer than {sub_length}")
index = random.choice(range(length))
result.append(iterable_copy[index])
del iterable_copy[index]
return result
example:
>>> get_sub_random_list(1, [1, 2, 3, 4, 5, 6])
[5]
>>> get_sub_random_list(6, [1, 2, 3, 4, 5, 6])
[4, 1, 5, 2, 6, 3]
The complexity is O(n+m): n is the length of iterable, and the m is the the times of the loop.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
what does np.c means in this code. Learning it from Udemy
df_cancer = pd.DataFrame(np.c_[cancer['data'], cancer['target'], columns=np.append(cancer ['feature_names'],['target;]))
According to official NumPy documentation,
numpy.c_ translates slice objects to concatenation along the second axis.
Example 1:
>>> np.c_[np.array([1,2,3]), np.array([4,5,6])]
array([[1, 4],
[2, 5],
[3, 6]])
Example 2:
>>> np.c_[np.array([[1,2,3]]), 0, 0, np.array([[4,5,6]])]
array([[1, 2, 3, 0, 0, 4, 5, 6]])
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]