val = 345.09874
now i want output like 345.10, 345.09 and 345.099 in python 2.7
Please help me.
Just to encourage you I am providing the answer, but next time you should google first.
Use this:
print round(val,2)
I could get 2 out of 3 values using:
val = 345.09874
print("%.2f" % round(val,2)) #345.10
print("%.3f" % round(val,3)) #345.099
I dont know how to get the output with 345.09
Related
I have tried different ways to try print odd numbers only but its only causing an infinite loop to occur. Could you please assist me?
import sys
i = 1
while i < len(sys.argv):
print sys.argv[i]
i = i + 1
Your question is little bit confusing.
You should be processing all arguments and then decide which one you should print.
import sys
i = 1
while i < len(sys.argv):
number = int(sys.argv[i])
if number % 2 == 1:
print number
i = i + 1
In python, sys.argv contains only one item 'main.py' in index number 0.
So, if you run the program from index 1, you will get nothing as output. If you set it from index 0, you will get 'main.py' printed as output.
If this answer is not satisfactory, please clarify your issue with this code.
I have a column in data frame which ex df:
A
0 Good to 1. Good communication EI : tathagata.kar#ae.com
1 SAP ECC Project System EI: ram.vaddadi#ae.com
2 EI : ravikumar.swarna Role:SSE Minimum Skill
I have a list of of strings
ls=['tathagata.kar#ae.com','a.kar#ae.com']
Now if i want to filter out
for i in range(len(ls)):
df1=df[df['A'].str.contains(ls[i])
if len(df1.columns!=0):
print ls[i]
I get the output
tathagata.kar#ae.com
a.kar#ae.com
But I need only tathagata.kar#ae.com
How Can It be achieved?
As you can see I've tried str.contains But I need something for extact match
You could simply use ==
string_a == string_b
It should return True if the two strings are equal. But this does not solve your issue.
Edit 2: You should use len(df1.index) instead of len(df1.columns). Indeed, len(df1.columns) will give you the number of columns, and not the number of rows.
Edit 3: After reading your second post, I've understood your problem. The solution you propose could lead to some errors.
For instance, if you have:
ls=['tathagata.kar#ae.com','a.kar#ae.com', 'tathagata.kar#ae.co']
the first and the third element will match str.contains(r'(?:\s|^|Ei:|EI:|EI-)'+ls[i])
And this is an unwanted behaviour.
You could add a check on the end of the string: str.contains(r'(?:\s|^|Ei:|EI:|EI-)'+ls[i]+r'(?:\s|$)')
Like this:
for i in range(len(ls)):
df1 = df[df['A'].str.contains(r'(?:\s|^|Ei:|EI:|EI-)'+ls[i]+r'(?:\s|$)')]
if len(df1.index != 0):
print (ls[i])
(Remove parenthesis in the "print" if you use python 2.7)
Thanks for the help. But seems like I found a solution that is working as of now.
Must use str.contains(r'(?:\s|^|Ei:|EI:|EI-)'+ls[i])
This seems to solve the problem.
Although thanks to #IsaacDj for his help.
Why not just use:
df1 = df[df['A'].[str.match][1](ls[i])
It's the equivalent of regex match.
I have this line:
randomIndex = Int(drand48() % Double(alphabetColors.count))
And Xcode 8 (Swift 3) tells me:
'%' is unavailable: Use truncatingRemainder instead
Is there no operator anymore? How should I convert my code?
You can simply follow the diagnostic message:
let randomIndex = Int(drand48().truncatingRemainder(dividingBy: Double(alphabetColors.count)))
Or using arc4random_uniform(_:) would be a better alternative.
let randomIndex = Int(arc4random_uniform(UInt32(alphabetColors.count)))
This seems to be available for me, currently on Swift 3.1, so possible it was added back.
My guess is that it's somewhere in Foundation and needs an explicit import Foundation
Update
This is for Int types only. It seems that for doubles, truncating remainder is required.
Cast your variable to Int before mod
example
let result = Int(value) % 3
As per new Guideline Swift 5, it's changed
value.truncatingRemainder(dividingBy: 2)
Use https://developer.apple.com/documentation/swift/double/2884269-remainder
If you use truncatingRemainder (as mentioned in the other comments) then it is going to floor the value first, which means 3.14 is going to become 3.
You can use this code to find the modulus of 2 numbers.
label.text is to display the result in a label that I have already designed
let result = Int(num1) % Int(num2)
label.text = String(Int(result))
As part of a school project we are creating a trouble shooting program. I have come across a problem that I cannot solve:
begin=['physical','Physical','Software','software',]
answer=input()
if answer in begin[2:3]:
print("k")
software()
if answer in begin[0:1]:
print("hmm")
physical()
When I try to input software/Software no output is created. Can anybody see a hole in my code as it is?
In Python, slice end values are exclusive. You are slicing a smaller list than you think you are:
>>> begin=['physical','Physical','Software','software',]
>>> begin[2:3]
['Software']
>>> begin[0:1]
['physical']
Use begin[2:4] and begin[0:2] or even begin[2:] and begin[:2] to get all elements from the 3rd to the end, and from the start until the 2nd (inclusive):
>>> begin[2:]
['Software', 'software']
>>> begin[2:4]
['Software', 'software']
>>> begin[:2]
['physical', 'Physical']
>>> begin[0:2]
['physical', 'Physical']
Better yet, use str.lower() to limit the number of inputs you need to provide:
if answer.lower() == 'software':
With only one string to test, you can now put your functions in a dictionary; this gives you the option to list the various valid answers too:
options = {'software': software, 'physical': physical}
while True:
answer = input('Please enter one of the following options: {}\n'.format(
', '.join(options))
answer = answer.lower()
if answer in options:
options[answer]()
break
else:
print("Sorry, {} is not a valid option, try again".format(answer))
Your list slicing is wrong, Try the following script.
begin=['physical','Physical','Software','software',]
answer=input()
if answer in begin[2:4]:
print("k")
software()
if answer in begin[0:2]:
print("hmm")
physical()
this is my first post to Stack Overflow. I have used it many times before but this is the first error that I have not found a solution to thus far. I am having a problem with a Python script where when I am changing an element in a list, the item replacing the element in the list drops the scientific notation aspect (i.e. E+08). Here is a snippet of the code and terminal output that is very strange to me. Thanks in advance for all help!
for i in range(0, len(EinsteinCof)):
for j in range(0, len(finalindexing)):
if i == finalindexing[j][0]:
if len(finalindexing[j]) == 3:
EinsteinFinal = float(EinsteinCof[finalindexing[j][0]]) + float(EinsteinCof[finalindexing[j][1]]) + float(EinsteinCof[finalindexing[j][2]])
print str(EinsteinFinal)
EinsteinCof[finalindexing[j][0]] = str(EinsteinFinal)
print str(EinsteinCof[finalindexing[j][0]])
Terminal Output:
4.1079763384e+13
4.1079763384 # <-- Where did e+13 go?
2269472400.0
2269472400.0
3.1777892e+12
3.1777892e+1 # <--where did e+12 go?
9.062911e+11
9.062911e+11