Multiplying two elements in a list [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
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")

Related

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)).

input to choose how many reuslts to generate [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 this code for i in range(1111111111111111, 9999999999999999): print(i)
I want to give an input which user will select how much value will the program generate.
like if he want only to generate 5k numbers it will only generate 5k.
what to do!
You can do it like this:
a = 1111
b = 9999
num = 500 # how many values you want
for i in range(a, b):
if i >= a + num:
break
print(i)

Converting HEX string into int [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 4 years ago.
Improve this question
I need help on converting the varbinary data which I acquired from sql, into int by taking 4 bits at a time.
From the code above, I acquired the results as ('0x640761075D075A0.....'). My plan is to take every 4 bits & swap it (ex: 0764 0761 07D5 and so on) and then turn every 4 bits into integers. what should I do?
thank you very much!
Try it:
from struct import pack, unpack
def convert_hex_to_int(n:int, interval:int):
splitted = [hex(n)[2:][i:i+interval] for i in range(0, len(hex(n)[2:]), interval)]
return [unpack('<H', pack('>H', int(i, 16)))[0] for i in splitted]
print(convert_hex_to_int(0x640761075D07, 4))
It will return list of int: [1892, 1889, 1885]

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

new line in for multiplcatoin (python3) [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
So I am new to python and I am trying to essentially "draw a board game", however I need it to print new lines with multiplication. This is my code so far:
x=int(input("How many columns? "))
y=int(input("How many rows? "))
z="|"
q="---"
f=((z + q)*x)+z
print(f*y)
What I want is to have newlines between rows, like this:
|---|---|---|
|---|---|---|
|---|---|---|
I tried:
print('\n', f,'\n', f, '\n', f)
How to put newlines between rows ?
If you just want spaces between rows, you can do:
print("\n\n".join([f]*y))
Upon decomposing:
[f] * y yields an array of rows
string.join(list) puts a string between each element in an list
So, you get two new lines between each row.

Categories

Resources