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
List output of program
print ('list', L)
['A','B']
dictionary output of program after the manipulation of program is below
print ('dictionary', d)
dictionary {'a':1,'b':2}
dictionary {'a':1,'b':2}
Expected Out
{'A':{'a':1,'b':2}, 'B': {'a':1,'b':2}}
list_out = ['A', 'B']
dict_out1 = {'a':1,,'b':2}
dict_out2 = {'a':1,,'b':2}
exp_out = {list_out[0]: dict_out1,
list_out[1]: dict_out2}
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 4 years ago.
Improve this question
I have a list a[i,j,k]. Out of this, I need to iterate through list 'a' and declare each of the list items as a list separately.
i[]
j[]
k[]
Is there a way to do this in python?
loop through and create
for item in a:
item = []
or you can use list comprehension
a = [[] for item in a]
if your intended output is [[i], [j], [k]] then use
a = [[item] for item in a]
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 5 years ago.
Improve this question
I have a list in python of the following form:
myList = ['r0x94', 'r0x21', 'r0x51']
I want to sort it based on the last number in each string entry of the list such that:
sorted_myList = ['r0x21', 'r0x51', 'r0x94']
The last number is not hex, rather it is decimal. How to do it?
>>> my_list = ['r0x94', 'r0x21', 'r0x51']
>>> sorted(my_list, key=lambda x: int(x.rpartition('x')[-1]))
['r0x21', 'r0x51', 'r0x94']