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

Related

sum integer part of the alphanumeric list 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 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.

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?

How to edit the orginal value? [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
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)).

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.

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