0

How can I write this in one line? Help me šŸ„¹

for element in B: if element: C.append(A[i]) i +=1

28th Aug 2022, 5:28 AM
The Psycho
The Psycho - avatar
3 Answers
0
C = [ element for element in B if element ] read about list comprehension.
29th Aug 2022, 9:34 AM
Jayakrishna šŸ‡®šŸ‡³
0
i, C = len(B), [A[_] for _ in range(len(B)) if B[_]]
29th Aug 2022, 10:48 AM
Brian
Brian - avatar
0
import numpy def elements_vector_true(): A, B = numpy.random.randint(10, size = 10), numpy.random.randint(10, size = 10)%2==0 print(f"Vector A: {A}") print(f"Vector B: {B}\n") i, C = 0, [] print("The vector A elements wich position of the vector B elements are true are:") for element in B: if element: C.append(A[i]) i +=1 print(C) elements_vector_true() #This's the code complete. I hope you help me, and if it cannot, also thanks šŸ‘ My code: https://code.sololearn.com/cOcRd5u4gEMO/?ref=app
30th Aug 2022, 1:38 AM
The Psycho
The Psycho - avatar