How to subtract strings in python
Easy Solution is:
>>> string1 = 'AJYF'
>>> string2 = 'AJ'
>>> if string2 in string1:
... string1.replace(string2,'')
'YF'
>>>
Subtract two strings in python
You need to convert numbers to float
s, and if the elements cannot be converted to numbers, insert a '-'
between them.
diffs = []
for i, j in zip(a, b):
try:
diffs.append(str(float(j) - float(i)))
except ValueError:
diffs.append('-'.join([j, i]))
>>> print(diffs)
['3.5', '-23.8', 'AMM-FFD']
Since python is strongly typed (not to be confused with static vs. dynamic) it does not implicitly perform arithmetic on the numeric interpretation of strings if it encounters an arithmetic operator between strings. There is no obvious behavior of the minus operator with regard to strings the way there is an obvious behavior of plus (i.e., concatenate). Would you expect it to remove instances of the second string from the first string? If so, there's already a more explicit str.replace
method you can use. Or would you expect it to remove the second string from the first only if the first string ends with the second string? The expected behavior isn't 100% obvious, so the python authors did not include __sub__
method support for strings.
Also, the operator
module isn't used in your code, so no need to import it.
How to subtract 2 given strings In Java?
While looping on the array of character that you wish to delete, you can use replaceFirst
. The character on the original string that you have deleted should be tagged with a specific character so that you can revisit later when rebuilding the result.
public class Test1 {
public static void main(String[] args) {
String result = remove("committee", "meet");
System.out.println(result);
}
//str1 is the original string, str2 is the
//array of chars to be removed from str1
public static String remove(String str1, String str2) {
for (int i = 0; i < str2.length(); i++) {
//tag the one deleted using specific character
str1 = str1.replaceFirst(Character.toString(str2.charAt(i)), "#");
} //end for
StringBuilder sb = new StringBuilder();
//Populate the non deleted chars
for (int i = 0; i < str1.length(); i++) {
//only copy character which has not yet been deleted
if (str1.charAt(i) != '#') {
sb.append(str1.charAt(i));
} //end if
} //end for
return sb.toString();
}
}
Sample Output:
comit
How can I subtract 2 string or list in python?
You could do:
a = 'ababaab'
b = 'abaaaaa'
a = ''.join(x if x != y else '0' for x, y in zip(a, b))
# '000b00b'
# OR
a = ''.join(x for x, y in zip(a, b) if x != y)
# 'bb'
Subtract two strings from each other
We can use Map
after splitting each of the columns by ,
get the setdiff
, paste
them together, set the names of the list
output with 'ID' column, stack
it to 'data.frame' and set
the names to 'ID' and 'Output' for the columns.
setNames(stack(setNames(Map(function(x,y) toString(setdiff(x,y)),
strsplit(as.character(df1$variable1), ","),
strsplit(as.character(df1$variable2), ",")),
df1$ID))[2:1], c("ID", "Output"))
# ID Output
#1 1 b, d
#2 2 g, f
#3 3 p, m, n
Or a compact option would be
library(splitstackshape)
cSplit(df1, 2:3, ",", "long")[, .(Output = toString(setdiff(variable1, variable2))) , ID]
# ID Output
#1: 1 b, d
#2: 2 g, f
#3: 3 p, m, n
C++ Can't subtract two strings
You should use std::string::substr
. Subtracting strings like that is invalid.
firstName = employeeName.substr(0, employeeName.find(" "));
The first parameter is the starting index of the substring you want to extract and the second parameter is the length of the substring.
Related Topics
String Concatenate Typeerror: Can Only Concatenate Str (Not "Int") to Str"
Test If Dictionary Key Exists, Is Not None and Isn't Blank
Getting the Bounding Box of the Recognized Words Using Python-Tesseract
Python Command Not Working in Command Prompt
How to Pass a Dictionary Object as Parameter for a Function in Python
Python - How to Fix "Valueerror: Not Enough Values to Unpack (Expected 2, Got 1)"
Python - Converting a List of 2 Digit String Numbers to a List of 2 Digit Integers
Opencv Typeerror: Expected Cv::Umat for Argument 'Src' - What Is This
Beautifulsoup: Get the Contents of a Specific Table
Unable Log in to the Django Admin Page With a Valid Username and Password
How to Smooth a Curve in the Right Way
How to Create a Common Function to Execute a Python Script in Jenkins
Webscraping Financial Data from Morningstar