我有一串由空格分隔的数字
test_string = '2.02.02.02.02.02.02.02.0'
当我执行len(lest_string)时,它返回24,这意味着它计算小数点和小数位。如何计算字符串中的元素,使2.0计为1个元素而不是3个元素?
答案 0 :(得分:0)
你可以试试这个:
8
输出:
test_string = '2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0'
print len(test_string.split())
如果test_string中提供了空格:
private void theFilter(string FilterValue) {
string thisQuery = "SELECT * FROM [Customer] WHERE CONCAT([Name], [Address], [Discount]) LIKE @FilterValue";
using(SqlConnection thisSqlConnection = new SqlConnection(theConnectionString))
using(SqlCommand thisSqlCommand = new SqlCommand(thisQuery, thisSqlConnection)) {
thisSqlCommand.Parameters.AddWithValue("@FilterValue", "%" + SqlLikeEscape(FilterValue) + "%");
using(SqlDataAdapter thisSqlDataAdapter = new SqlDataAdapter(thisSqlCommand))
using(DataTable thisDataTable = new DataTable()) {
thisSqlDataAdapter.Fill(thisDataTable);
DataGrid_Customer.ItemsSource = thisDataTable.DefaultView;
}
}
}
// This function is important, since otherwise if there is a % in the table (or other special LIKE characters) then it is hard to search for them
//see https://stackoverflow.com/questions/18693349/how-do-i-find-with-the-like-operator-in-sql-server
public static string SqlLikeEscape(string value)
{
if (string.IsNullOrEmpty(value)) return value;
return Regex.Replace(value, @"(?<ch>%|_|\[)", @"[${ch}]");
}
答案 1 :(得分:0)
如果您的号码实际上是用空格分隔的,那么
test_string = '2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0'
nums = [float(f) for f in test_string.split()]
how_many = len(nums) # 8
或者,如果您某些,则数字之间只有一个空格,
how_many = test_string.count(" ") + 1 # 8
或者你可以
how_many = test_string.count(".") # 8
答案 2 :(得分:0)
在test_string
中,数字不会以空格分隔。如果是,您可以split()
test_string
并获取结果列表的长度:
test_string = '2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0'
l = len(test_string.split())
print("Debug: l =", l)
返回:
Debug: l = 8
答案 3 :(得分:0)
test_string = '2.02.02.02.02.02.02.02.0'
或
test_string = '2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0'
count = len(re.findall(r'2.0', test_string))
输出:8