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 months ago.
Improve this question
def solution(arr):
sol=[[]]
temp=[[]]
for num in arr:
for i in range(len(temp)):
temp[i].append(num)
sol.append(temp[i])
temp=sol.copy()
return sol
This is my code. I was trying to add the new subsets from temp array to the solution array. but the command sol.append(temp[i]) isnt working as it should.instead of appending temp[i], the original values in sol array are getting changed. for array [1], the output is correct, but for array>len 1, it is showing weird values..
can someone help me pleasee..!
The indentation needs some fixing and clean some unnecessary code.
def solution(arr):
sol = [[]]
for num in arr:
for i in range(len(sol)):
temp = list(sol[i])
temp.append(num)
sol.append(temp)
return sol
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 4 months ago.
Improve this question
Essentially, I was wondering if you could merge these 2 lines:
items = ["cat","apple","taco"]
def change(x):
return(items[x-1])
temp = change(2) # THIS ONE AND
temp = "orange" # THIS ONE
It's difficult for me to explain, my apologies. I essentially just want to be able to get rid of that temp variable or a least only have to use it once.
Why not just something like:
items = ["cat","apple","taco"]
def change(ind, tx):
items[ind-1] = tx
change(2, "orange")
print(items)
# >>> ['cat', 'orange', 'taco']
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 1 year ago.
Improve this question
I have an array like:
arr=[[1,2,3,4,'a,b,c,d'],[5,6,7,8,'e,f,g,h']]
I want to be:
arr=[[1,2,3,4,'a'],[1,2,3,4,'b'],[1,2,3,4,'c'],[1,2,3,4,'d'],[5,6,7,8,'e'],[5,6,7,8,'f'],[5,6,7,8,'g'],[5,6,7,8,'h']]
can somebody helps to gives me a suggestion? Many thanks.
you can iterate through the string (last element in list) and append the answer onto everything but the last element
new_arr = [[li[:-1]+[letter] for letter in li[-1].split(',')] for li in arr]
# Flatten
out = [item for sublist in new_arr for item in sublist]
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
x=0
for x in range(0,k):
if(x>list_length):
x=0
#adding value of list in position 'x' to concat list
concat_list.append(l[x])
x+=1
I want to edit the real value(defined outside the for loop) of x in "if statement". How can I do that?
Well, if you want to redefine the original value x, then redefine your for loop variable (i.e. for x in range(0, k) to for i in range(0, k)).
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 of strings L.
I need to check, whether a string is either directly an element of L or is in this format: "foo-element_of_L"
Is there a better way to do this in python than adding "foo-X" to L for all X in L?
I would do two lookups:
if x in L or f'foo-{x}' in L:
which may be significantly faster than
if any(x == y or f'foo-{x}' == y for x in L):
which is essentially what you were proposing.
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
I have a list of lists of strings such that:
decipher = [['###>>?', '######', '*&*...', '##&#&#'], ['###>>?', '######', '*&*...', '######'], ['###>>?', '######', '*&*...', '######'], ['###>>?', '######', '*&*...', '######']]
I need to concatenate items with the same indeces from each list to print on the same line, each subsequent set of identically indexed items have to print on a new line:
###>>?###>>?###>>?###>>?
########################
*&*...*&*...*&*...*&*...
##&#&###&#&###&#&###&#&#
How can I accomplish that? Thanks!
A simple one line solution would be:
>>> print '\n'.join(''.join(i) for i in zip(*decipher))
###>>?###>>?###>>?###>>?
########################
*&*...*&*...*&*...*&*...
##&#&###################
Here is another solution:
for col in range(len(decipher[0])):
out = ''
for row in range(len(decipher)):
out += decipher[row][col]
print(out)
###>>?###>>?###>>?###>>?
########################
*&*...*&*...*&*...*&*...
##&#&###################