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
Sample Input (in Plaintext)
1
1 2
Description- The first line contains an integer t, denoting the number of test cases. Next t lines contain two integers, a and b separated by a space.
My question is how can I load these inputs into variables using Python 2.7? Also I need to load this into IDE without using "xxx.py > input.txt"
raw_input() takes in a line of input as a string. The .split() string method produces a list of the whitespace-separated words in a string. int() parses a string as an int. So putting that together, you can read the input you want using
t = int(raw_input())
cases = []
for i in range(t):
cases.append( map(int, raw_input().split()) )
This will give you a list of a,b pairs if the input is in the correct format, but won't do any error checking.
Related
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
I am having a problem with my addition. The way I set it up is to take a problem (ex 5+3) and assign each number to a variable. I converted them to integers using int() but when I run my script it would come back as 53. Does anyone know a solution?
I am very new to python so my code is a mess.
string = (pr)
new_string = string.replace("+", " " )
txt = (new_string)
x = txt.split(" ")
a,b = x
int(a)
int(b)
print (a + b)
Because int(a) does not change a, the generated integer number becomes an unused temporary variable. So a is still of a string type.
You need to use a=int(a) as well as next line b=int(b)
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 9 months ago.
Improve this question
I have a text file containing bits so it’s like „1000101011010110000…“ in this text. I want python to interpret this text as bytes and perform different byte transformations with it. But how do I red it on as bytes without python thinking it’s a string?
The built-in int function has an parameter base for specifying the base.
To convert a string into an integer with base 2 (binary), pass 2 into it:
s = input()
num = int(s, 2)
# Manipulate `num` as you like
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
Example:
Input : "Base derived derived You are great"
Output: "Base->derived:derived You are great"
Here, the first two spaces are replaced with -> and : respectively, the rest of the string remains the same.
Nothing fancy, but if it's a one-off application, this would do the trick, using the Python str.split() method to create a list, splitting the string into three chunks on the first two spaces, then create a new string with those three chunks separated by '->' and ':'.
your_string = "Base derived derived You are great"
split_string = your_string.split(maxsplit=2)
result = f"{split_string[0]}->{split_string[1]}:{split_string[2]}"
As suggested in comments by #yatu, this can be reduced to a single statement using the *-operator to unpack the list:
result = '{}->{}:{}'.format(*your_string.split(maxsplit=2))
result in both cases:
'Base->derived:derived You are great'
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
I need to add the integers from double digit number(triple etc.) together
Block quote 22 = 2+2 = 4
I considered doing it manually but because I have 999 number to get through that'll take a really long time.
I tried making two separate lists and adding it together but continually received syntax errors.
*edited for extra detail *
You can convert the integer to a string, and then sum the values up:
count = sum((int(digit) for digit in str(22)))
print(count)
This iterates over every digit, converts it back to an integer, and finally computes the sum over all digits.
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 9 years ago.
Improve this question
I have thousands of lines with data in the form: 6580,f|S,17:42:29.3,-39:01:48,2.19,2.41,-0.22
I would like to take only the last number (in this case -0.22) so I can perform some calculations.
My problem is: the lines aren't strings, and they have variable length (slicing from the beginning/end isn't an option). How can separate those commas and create a list of strings or floats?
Is there a way to separate that sequence in way that I can use, for example, "line[6]" to get that last value?
If the floating number you need is always the last entry in comma-separated line, float(line.split(',')[-1]) should do the trick.
Otherwise I would do something like the following:
def isFloat(val):
try:
float(val)
return True
except ValueError:
return False
number = [x for x in line.split(',') if isFloat(x)][-1]
If all the lines you have are stored in a text file, open it and process your lines:
with open("my_file.txt") as f:
for line in f:
# Here goes the code where you're getting the floating number from the line
Maybe you're looking for this syntax line[:-1] will always give you the last element in an array.
It could possibly be line[::-1]. Somebody should correct me if I'm wrong.
BTW if they are not strings, what are they?
Edit.
It's line[-1], Getting the last element of a list in Python