如何计算字符串末尾的特定字符数忽略重复?

时间:2017-02-11 16:47:31

标签: python

我有一系列字符串,如:

my_text = "one? two three??"

我只想算数?在字符串的末尾。上面应该返回2(而不是3)。

到目前为止我尝试过:

my_text.count("?") # returns 3

4 个答案:

答案 0 :(得分:9)

没有内置的方法。但是像这样简单的事情应该可以解决问题:

>>> len(my_text) - len(my_text.rstrip('?'))
2

答案 1 :(得分:1)

您还可以使用正则表达式来计算尾随问号的数量:

import re

def count_trailing_question_marks(text):
    last_question_marks = re.compile("\?*$")
    return len(last_question_marks.search(text).group(0))

print count_trailing_question_marks("one? two three??")
# 2
print count_trailing_question_marks("one? two three")
# 0

答案 2 :(得分:0)

不太干净但很简单:

WITH 
ALLPUBLISHERSUNFILTERED AS
(
SELECT              'NOT SUBMITTED' AS PublisherType,
                    c2.FullName AS ContactFullName, 
                    c2.LastName AS ContactLastName, 
                    c2.ContactId AS PublisherGUID,
                    NULL AS FSGName, 
                    NULL AS FieldServiceGroupGUID, 
                    NULL AS ReportGUID, 
                    NULL AS ReportID,
                    NULL AS ReportMonthGUID,
                    NULL AS ReportYearGUID,  
                    NULL AS ReportMonthName, 
                    NULL AS ReportMonthCalDate, 
                    NULL AS ReportYearName  
FROM                ContactBase AS c2 
WHERE               c2.StateCode = '0' 
AND                 NOT c2.jajw_CongregationAssignment 
                    IN (640840001, 640840005, 640840006, 640840007)
),

REPORTERS AS
(
SELECT              'SUBMITTED' AS PublisherType,
                    c2.FullName AS ContactFullName, 
                    c2.LastName AS ContactLastName, 
                    c2.ContactId AS PublisherGUID, 
                    f2.jajw_groupname AS FSGName, 
                    b2.jajw_FieldServiceGroup AS FieldServiceGroupGUID, 
                    b2.jajw_reportId AS ReportGUID, 
                    b2.jajw_id AS ReportID,
                    b2.jajw_ReportMonthId AS ReportMonthGUID,
                    b2.jajw_ReportYearId AS ReportYearGUID,  
                    r2.jajw_name AS ReportMonthName, 
                    r2.jajw_CalendarDate AS ReportMonthCalDate, 
                    y2.jajw_name AS ReportYearName 

FROM                ContactBase AS c2 
LEFT JOIN           jajw_reportBase AS b2 ON c2.ContactId = b2.jajw_PublisherId 
LEFT JOIN           jajw_reportyearBase AS y2 ON b2.jajw_ReportYearId = y2.jajw_reportyearId 
LEFT JOIN           jajw_reportmonthBase AS r2 ON b2.jajw_ReportMonthId = r2.jajw_reportmonthId 
LEFT JOIN           jajw_fieldservicegroupBase AS f2 ON b2.jajw_FieldServiceGroup = f2.jajw_fieldservicegroupId
WHERE               c2.StateCode = '0' 
AND                 b2.jajw_ReportYearId = 'A507DFBF-B9CE-E611-A953-002248013EC3'
AND                 NOT c2.jajw_CongregationAssignment 
                    IN (640840001, 640840005, 640840006, 640840007)
)

SELECT   *
FROM     ALLPUBLISHERSUNFILTERED a
WHERE    NOT EXISTS  
         (SELECT     1
          FROM       REPORTERS rp
          WHERE      a.PublisherGUID = rp.PublisherGUID)

UNION ALL

SELECT   *
FROM     REPORTERS

答案 3 :(得分:0)

使用我最喜欢的itertools的单行:

首先反转字符串,然后在满足条件时继续迭代(取值)(值=='?')。这将返回一个迭代,我们将其排入列表并最终获取其长度。

len(list(itertools.takewhile(lambda x:x=='?',reversed(my_text))))