sum integer part of the alphanumeric list 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 6 days ago.
Improve this question
I have a list like below
l1 = ['a10','b2','a2','c1','b4','c5']
and I want to sum of numeric and output like below
l2 = ['a12','b6','c7'].
Note: do not use in-built function

from collections import Counter
l1 = ['a10','b2','a2','c1','b4','c5']
l2 = [f'{k}{v}' for (k, v) in Counter(''.join(i[0]*int(i[1:]) for i in l1)).items()]
# ['a12', 'b6', 'c6']
Figuring this out is left as an exercise to the reader.
It also assumes the alpha part is always just the first character.

Related

Assigning values in a list to variables in an ordered 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 5 days ago.
Improve this question
I want to give the values in all_X_H in there normal indices order a to variables in ring[corrector_indexes[m]].KickAngle which doesn't have the same order as a.
This is a simple example:
all_X_H = [
0,-0.000009
1,-0.000018
2,-0.000010
3,-0.000007]
corrector_indexes = [1,2,3,4]
final_corr_order_H = [3,2,1,0]
for m in final_corr_order_H:
for a in range(len(all_X_H)):
ring[corrector_indexes[m]].KickAngle = [all_X_H[a],0]
The result i want to see is
ring[corrector_indexes[3]].KickAngle = [all_X_H[0],0]
ring[corrector_indexes[2]].KickAngle = [all_X_H[1],0]
ring[corrector_indexes[1]].KickAngle = [all_X_H[2],0]
ring[corrector_indexes[0]].KickAngle = [all_X_H[3],0]
How can i define two different indices in one for loop to implement this?

Call function and modify called variable in one operation [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 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']

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.

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

Categories

Resources