【问题标题】:Python List Index ComprehensionPython列表索引理解
【发布时间】:2013-12-23 06:27:21
【问题描述】:

我有一个类,它返回一个不同长度和结构的列表。示例输出为:

['Lead Attorneys', 'Defendant', 'Creighton, David Walter', '3661 Braeden Ct', 'Middleburg, FL 32068', 'Defendant', 'Creighton, Mary Lynette', '3661 Braeden Ct', 'Middleburg, FL 32068', 'Defendant', 'Unknown Spouse', 'ConvUnknown', 'Defendant', 'Unknown Tenant #1', 'ConvUnknown', 'Plaintiff', 'PNC Bank National Association', 'Josh D Donnelly', 'Retained', '', '813-915-8660(W)', '813-915-0559(F)', 'AttorneyNotice@Consuegralaw.com']

我想通过被告之间的索引来分割这个列表。我可以通过以下方式确定每次出现的被告的位置:

      i = -1
      try:
         while 1:
            i = partyinfo.index('Defendant', i+1)
            print "match at", i
      except ValueError:
         pass

“被告”的输出位于位置 1、5、9、12。我想连接每次出现之间的位置。由于这些发生在 1、5、9 和 12,我需要对派对信息进行切片,本质上是:

defendant += partyinfo[i+1: to the next occurence of i-1 and append to a new value for storage.

我终其一生都无法理解如何做到这一点,这似乎很简单。任何帮助将不胜感激。

预期的输出是:

'Creighton, David Walter', '3661 Braeden Ct', 'Middleburg, FL 32068', 'Creighton, Mary Lynette', '3661 Braeden Ct', 'Middleburg, FL 32068', 'Unknown Spouse', 'ConvUnknown','Unknown Tenant #1', 'ConvUnknown'

【问题讨论】:

  • 我希望名称/地址被混淆
  • 他们不是,但这是公共记录的问题。如果复制此类内容违反本站条款;我很乐意删除细节。

标签: python string list while-loop indexing


【解决方案1】:
[s for s in partyinfo[partyinfo.index('Defendant'):] if s != 'Defendant']

您似乎想从partyinfo 创建一个新列表,从'Defendant' 的第一次出现开始并过滤掉所有'Defendant's。这个列表推导找到'Defendant'的第一次出现,从该位置开始获取partyinfo的切片,然后过滤掉所有出现的'Defendant'

【讨论】:

  • 这很好。但是,如果碰巧partyinfo 没有“被告”元素,这将引发错误
  • @jpwagner:是的。目前尚不清楚在这种情况下会发生什么。也许它应该是一个错误,或者它应该返回一个空列表。
【解决方案2】:

这有帮助吗

resultinglist=[]
for i in range(len(indexes)): #indexes being the indexes of defendent
   if i!= len(indexes)-1: #if its not the last index
     resultinglist+=partyinfo[indexes[i]+1:indexes[i+1]] # add the stuff between YAY

【讨论】:

    【解决方案3】:
    >>> L = ['Lead Attorneys', 'Defendant', 'Creighton, David Walter', '3661 Braeden Ct', 'Middleburg, FL 32068', 'Defendant', 'Creighton, Mary Lynette', '3661 Braeden Ct', 'Middleburg, FL 32068', 'Defendant', 'Unknown Spouse', 'ConvUnknown', 'Defendant', 'Unknown Tenant #1', 'ConvUnknown', 'Plaintiff', 'PNC Bank National Association', 'Josh D Donnelly', 'Retained', '', '813-915-8660(W)', '813-915-0559(F)', 'AttorneyNotice@Consuegralaw.com']
    >>> from itertools import groupby
    >>> [' '.join(g) for k,g in groupby(L, key="Defendant".__ne__) if k]
    ['Lead Attorneys',
     'Creighton, David Walter 3661 Braeden Ct Middleburg, FL 32068', 
     'Creighton, Mary Lynette 3661 Braeden Ct Middleburg, FL 32068',
     'Unknown Spouse ConvUnknown',
     'Unknown Tenant #1 ConvUnknown Plaintiff PNC Bank National Association Josh D Donnelly Retained  813-915-8660(W) 813-915-0559(F) AttorneyNotice@Consuegralaw.com']
    

    【讨论】:

      【解决方案4】:

      将列表拼接成字符串,然后拆分字符串:

      >>> alist = ['Lead Attorneys', 'Defendant', 'Creighton, David Walter',....]
      >>> astr = ''
      >>> for i in alist:
      ...    astr = '%s,%s' % (astr,i)
      >>> astr = astr[1:]
      >>> astr.split('Defendant')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-05
        • 2013-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-20
        • 1970-01-01
        • 2019-07-05
        相关资源
        最近更新 更多