简体   繁体   中英

Python List Index Comprehension

I have a class that returns a list of varying length and structure. A sample output is:

['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 want to slice this list by the index between the word Defendant. I can determine the position of each occurrence of Defendant with:

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

Which outputs that 'Defendant' is at positions 1, 5, 9, 12. I want to concatenate the positions between each occurrence. Since these occur at 1,5,9, and 12, I need to slice partyinfo, essentially:

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

I cannot comprehend for the life of me how to accomplish this and it seems so simple. Any help would be appreciated.

The expected output would be:

'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'
[s for s in partyinfo[partyinfo.index('Defendant'):] if s != 'Defendant']

It looks like you want to create a new list from partyinfo , starting at the first occurence of 'Defendant' and filtering out all 'Defendant' s. This list comprehension finds the first occurence of 'Defendant' , gets a slice of partyinfo starting at that position, and then filters out all occurences of 'Defendant' .

Does this help

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
>>> 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']

Concatenate the list into a string, and then split the string:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM