How to change values in a list - python? [closed] - python

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']

Related

How to repeat certain lines conditionally in Python? [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 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]

How to map one list and dictionary 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 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}

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']

Categories

Resources