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

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?

Multiplying two elements in 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 4 months ago.
Improve this question
How can I multiply two elements in a list?
I am reading a text file, and printing the following:
for i in range(len(listeisotoper)):
print("Isotop type:"+listeisotoper[i][0])
print("Isotopisk masse u: "+listeisotoper[i][1])
print("Naturlig forekomst: "+listeisotoper[i][2])
print("xxx"+"g/mol")
print("\n")
However i cannot fathom how i can multiply the listeisotoper[i][1] * listeisotoper[i][2]
and then have it print the number with decimal points.
Any suggestions?
It isn't clear question for me, but may be this code will help you
for i in range(len(listeisotoper)):
print("Isotop type:"+listeisotoper[i][0])
print("Isotopisk masse u: "+listeisotoper[i][1])
print("Naturlig forekomst: "+listeisotoper[i][2])
mult_res = float(listeisotoper[i][1]) * float(listeisotoper[i][2])
print(f"Mult = {mult_res:.1f}")
print("xxx"+"g/mol")
print("\n")
I am assuming your problem is multiplying strings. If so, try this:
for i in range(len(listeisotoper)):
print("Isotop type:"+listeisotoper[i][0])
print("Isotopisk masse u: "+listeisotoper[i][1])
print("Naturlig forekomst: "+listeisotoper[i][2])
print(str(float(listeisotoper[i][1])*float(listeisotoper[i][2]))+"g/mol")
print("\n")

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

subsets question from leetcode( question 78) [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 months ago.
Improve this question
def solution(arr):
sol=[[]]
temp=[[]]
for num in arr:
for i in range(len(temp)):
temp[i].append(num)
sol.append(temp[i])
temp=sol.copy()
return sol
This is my code. I was trying to add the new subsets from temp array to the solution array. but the command sol.append(temp[i]) isnt working as it should.instead of appending temp[i], the original values in sol array are getting changed. for array [1], the output is correct, but for array>len 1, it is showing weird values..
can someone help me pleasee..!
The indentation needs some fixing and clean some unnecessary code.
def solution(arr):
sol = [[]]
for num in arr:
for i in range(len(sol)):
temp = list(sol[i])
temp.append(num)
sol.append(temp)
return sol

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.

Categories

Resources