How to code this condition in Python? I am new to python [closed] - python

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 1 year ago.
Improve this question
I am new to python. so any help will be appreciated.
I have two arrays A = [1,2,4,2,3,5,3] and B = [0,4,4,4,1,1,1]
for the function if I give A, B as input then I should get output as = [1,(2+4+2),(3+5+3)] = [1,8,11](if numbers are repeating in B then corresponding values in A should be added together).

This should do the trick:
def bla(list1:list, list2:list):
prev = list2[0] - 1
final_list = []
for ele, pos in zip(list1, list2):
if prev != pos:
final_list.append(ele)
else:
final_list[-1] += ele
prev = pos
return final_list

Related

alternative of enumerate() in python [closed]

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
I want to access the index without using enumerate in this code
def check(text):
stack=[]
index=[]
for i, ch in enumerate(text):
if ch in ['{','(']:
stack.append(ch)
index.append(i)
here is my approch
def check(text):
stack=[]
index=0
x=range(len(text))
for i in x:
if i in ['{','(']:
stack.append(i)
index+=1
is it correct?
for i in x will return 0, 1, 2, 3, 4..., not the text. Also, your index is completely useless in your current code.
Also, your index is in the wrong place.
The easiest way to fix the code is just to do this:
def check(text):
stack = []
index = 0
for char in text:
if char in ["{", "("]:
stack.append(char)
index+=1
Note that x is never used.
Thanks to #G. Anderson for the fix (index in the wrong place)!
Also side note: is this for an ide? :P

I'm returning a defaultdict(list), but randomly choosing between the two, why does it return nothing sometimes? [closed]

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 2 years ago.
Improve this question
output = ""
numberList = [0, 1]
print(random.choice(numberList))
if(random.choice(numberList) == 0):
if len(slots) > 0:
output = templates[state][0].replace("<num_classes>", str(slots[0][1]))
else:
output = templates[state][0]
elif(random.choice(numberList) == 1):
if len(slots) > 0:
output = templates2[state][0].replace("<num_classes>", str(slots[0][1]))
else:
output = templates2[state][0]
return output
expected answer
sometimes get this
Sometimes it returns nothing or no dictionary... Why?
With
elif(random.choice(numberList) == 1)
you will again choose a brand new random number. And if that isn't 1 then there's no else that will set output.
Instead of elif you should have a plain else.

Find matching numbers in an array and print result to new array [closed]

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 2 years ago.
Improve this question
Trying to figure out why the code I have produced is not the answer to the following question:
Given two arrays, one with user inputs and another of correct inputs, return an array that denotes when the user input matches the correct input with the number 1, and incorrect inputs with -1.
My code below
newArr = []
def correct_stream(user, correct):
for i in user:
for j in correct:
if i == j:
newArr.append(1)
else:
newArr.append(-1)
return newArr
Your return statement is inside of your second for loop and will only execute once.
newArr = []
def correct_stream(user, correct):
for i in user:
for j in correct:
if i == j:
newArr.append(1)
else:
newArr.append(-1)
return newArr #this executes after the if statement runs only once, leading to an array with size 1
Python is built upon proper syntax, and while some aspects are flexible, indentation is strict. Brackets don't split apart scopes, indentation does.
newArr = []
def correct_stream(user, correct):
for i in user:
for j in correct:
if i == j:
newArr.append(1)
else:
newArr.append(-1)
return newArr #this now executes after the whole for loop above is finished

How to fix the recursive function? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
It's a simple question of recursive function, which extracting every digits from a positive number. As the comment mentioned, I have known the mistake is because of global variable, but I still have no idea to fix it to get expected result. Thanks.
def getdigits(n):
if n == 0:
return list_1[::-1]
list_1.append(n % 10)
return getdigits(int(n / 10))
list_1 = []
print(getdigits(120)) # output = [1,2,0]
print(getdigits(5)) # output = [5,1,2,0]
But the expected output should be
print(getdigits(120)) # expected output = [1,2,0]
print(getdigits(5)) # expected output = [5]
You are using the existing LIST that already have values from the previous function call.
First clear the list and then try to call for another value.\
Try it
def getdigits(n):
if n == 0:
return list_1[::-1]
list_1.append(n % 10)
return getdigits(int(n / 10))
list_1 = []
print(getdigits(120))
list_1.clear() #clear list values
print(getdigits(5))
def getdigits(n):
global list_1
if n == 0:
listTemp = list_1
list_1 = []
return listTemp
list_1.append(n % 10)
return getdigits(int(n / 10))
list_1 = []
print(getdigits(120)) # output = [1,2,0]
print(getdigits(5)) # output = [5]
you need to declare list 1 as a global varible inside your function so you can clear list one within th function
i have tested it and got that output

How to form a given list of elements in python to create another set of lists from it? [closed]

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 5 years ago.
Improve this question
List1 =['000095', '000094', '000092', '000101', '000099', '000096', '000095']
def makecycle(list, startElement):
A loop which forms the bottom list which is made from the upper one's elements!
if i pass that function the start element and the list it shoul print like this:
makecycle(list1, 000094) it should print:
['000094', '000092', '000101', '000099', '000096', '000095', '000094']
and if pass
makecycle(list1, 000101) it should print:
['000101', '000099', '000096', '000095', '000094', '000092', '000101']
and if pass
makecycle(list1, 000092) it should print:
['000092', '000101', '000099', '000096', '000095', '000094', '000092']
i know its kinda not clear enough but thats all i can point!
def makecycle(list1,startElement):
ind = list1.index(startElement)
l = len(list1)
i = ind
list2 = []
while ind < (l-1):
list2.append(list1[ind])
ind = ind + 1
for x in range(i):
if list1[x] not in list2:
list2.append(list1[x])
print(list2)

Categories