Removing Specific Substrings from String Used for Data Slice [duplicate] - python

This question already has answers here:
Regular expression to return text between parenthesis
(11 answers)
Closed 1 year ago.
I have a string where I need to keep two key numbers and remove the rest which need to be converted to a string that can be used to slice data.
sample = 'range(0, 286)range(300, 511)'
I will always need the first two numbers (ex. 0 and 286). Each of these numbers will not always be 0 and 286. They can have multiple positions like 10 and 1000 or 100 and 10000. They will always be in parentheses and have a comma to separate each number. The second set of numbers do not need to be extracted from the string.
Expected Output: My end product would be a string that looks like a slice:
print([sample])
[0:286]
How do I extract just the first two numbers from this text, zero and two-hundred-eighty-six?

Assuming the following input:
series = pd.Series(['range(0, 286)range(300, 511)', 'range(100, 1000)range(300, 511)'])
you can use str.extract with named groups:
series.str.extract('range\((?P<number1>\d+),\s+(?P<number2>\d+)\)')
output:
number1 number2
0 0 286
1 100 1000

Related

Read multiple integers on separate lines from TextEdit.toPlainText() [duplicate]

This question already has answers here:
Change a string of integers separated by spaces to a list of int
(8 answers)
Closed 1 year ago.
In textEdit there are numbers separated by line breaks and I want to use them, but these numbers are a string and I couldn't convert them to integers.
This error is shown
ValueError: invalid literal for int() with base 10:' 15\n300\n2000\n'.
How can I use this content of TextEdit as integers?
t = self.TextEdit_numbers.toPlainText()
numbers = int(t)
I see there is a new line (\n) symbol in your TextEdit_numbers value.
It is not possible to convert something as
15
300
2000
to single integer.
I believe you want pass array of integers to your pie method.
You should try something like that:
self.numbers_string = self.TextEdit_numbers.toPlainText()
self.t = [int(n) for n in self.numbers_string.split('\n')
pyplot.pie(self.t, normalize=True)
In second line I use list comprehensions, you can read more about it here (https://www.w3schools.com/python/python_lists_comprehension.asp), and standard python string .split() method, more info here: https://www.w3schools.com/python/ref_string_split.asp
If you have integers on each line, this would give you a list of all those integers:
with open('TextEdit_numbers.txt') as f:
numbers = [int(x) for x in f]
Assuming the txt file has inputs like:
15
200
3000
The output would be the array number as:
[15, 200, 3000]

Split string into two integers, python [duplicate]

This question already has answers here:
Given n, take tsum of the digits of n. If that value has more than one digit, continue reducing a single-digit number is produced
(4 answers)
Closed 1 year ago.
I have problem and trying to get next:
new_string = "35" #and this result must be like new_int = 3+5.
How im available to do this? I know the type conversion, but not a clue how i should do this.
As you are new to the python, i suggest you doing it using
int(new_string[0]) # 3
int(new_string[1]) # 5
So now you have 2 integers, you can to whatever you want

Python - why are my numbers not storing correctly [duplicate]

This question already has answers here:
How to convert a string of space- and comma- separated numbers into a list of int? [duplicate]
(6 answers)
How to split a string of space separated numbers into integers?
(9 answers)
Closed 4 years ago.
I am trying to take in a string of numbers, such as:
1 2 3 4 55 33 15
and store them in a list as they appear so that I can operate on the items at each index in the list, but they aren't splitting and storing right. They store like:
1,2,3,4,5,5,3,3,1,5
which is not what I want. My current code is attached below. I thought they would store as:
1,2,3,4,5,55,33,15
I do convert them to ints later on and that works fine, this is just the part that I am stuck on.
numbersArrayTemp.append(userInput.readline().strip(' ').strip('\n'))
for item in numbersArrayTemp:
print(item)
Type = item.strip('\n')
print(Type)
j = item.replace(" ", '')
numbersArrayString.extend(j)

How to pad a string with leading zeros in Python 3 [duplicate]

This question already has answers here:
How do I pad a string with zeroes?
(19 answers)
Closed 3 years ago.
I'm trying to make length = 001 in Python 3 but whenever I try to print it out it truncates the value without the leading zeros (length = 1). How would I stop this happening without having to cast length to a string before printing it out?
Make use of the zfill() helper method to left-pad any string, integer or float with zeros; it's valid for both Python 2.x and Python 3.x.
It important to note that Python 2 is no longer supported.
Sample usage:
print(str(1).zfill(3))
# Expected output: 001
Description:
When applied to a value, zfill() returns a value left-padded with zeros when the length of the initial string value less than that of the applied width value, otherwise, the initial string value as is.
Syntax:
str(string).zfill(width)
# Where string represents a string, an integer or a float, and
# width, the desired length to left-pad.
Since python 3.6 you can use fstring :
>>> length = 1
>>> print(f'length = {length:03}')
length = 001
There are many ways to achieve this but the easiest way in Python 3.6+, in my opinion, is this:
print(f"{1:03}")
Python integers don't have an inherent length or number of significant digits. If you want them to print a specific way, you need to convert them to a string. There are several ways you can do so that let you specify things like padding characters and minimum lengths.
To pad with zeros to a minimum of three characters, try:
length = 1
print(format(length, '03'))
I suggest this ugly method but it works:
length = 1
lenghtafterpadding = 3
newlength = '0' * (lenghtafterpadding - len(str(length))) + str(length)
I came here to find a lighter solution than this one!

python 3 How to append number to number [duplicate]

This question already has answers here:
How to concatenate two integers in Python?
(17 answers)
Closed 8 years ago.
How would I append a number to the end of another number?
for example:
a = 1
b = 2
a = a + b
print(a)
Output = 3
I want to get my output as 12 but instead I get 3. I understand that when you do number + number you get the addition of that but I would like to do is append b to a and so I would get 12. I have tried appending a number to a number but I get an error. I think that you can only append a list.
My question is:
How do I append a number to a number?
or is there a better way of doing it?
The reason you get 3 is because a and b contain integers. What you want is string concatenation to get 12. In order to use string concatenation you need strings. You can type cast the integers to string using str() and then use int() to type cast the string to an integer.
a = 1
b = 2
a = str(a) + str(b)
a = int(a)
print(a)
The oneliner solution is already provided in the comments.

Categories

Resources