How to repeat certain lines conditionally in Python? [closed] - python

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]

Related

How to match a string to pattern "Foo-Bar", where Bar can be any element of a list? [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 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.

Is there a way to declare all the values in the list as a list individually 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 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]

Sorting a list in python based on the last number in each string entry [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 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']

How to change values in a list - 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 6 years ago.
Improve this question
If I have a list like:
['15', 'A:B', 'B:C', 'D:C', 'F:A']
I want to change the values like:
['1515', 'A:B15', 'B:C15', 'D:C15', 'F:A15']
(add first one to other values in a list)
Is it possible?
Assuming ["15", "A:B", "B:C", "D:C", "F:A"] is your input list, a simple list comprehension would do the job:
>>> l = ["15", "A:B", "B:C", "D:C", "F:A"]
>>> value_to_add = l[0]
>>> [item + value_to_add for item in l]
['1515', 'A:B15', 'B:C15', 'D:C15', 'F:A15']

Concatenate a list of lists into a string printed on separate lines [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 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)
###>>?###>>?###>>?###>>?
########################
*&*...*&*...*&*...*&*...
##&#&###################

Categories

Resources