2

大文字と潜在的に小文字の両方を含む文字列から小文字の値を取得する方法を探しています

これが例です

sequences = ['CABCABCABdefgdefgdefgCABCAB','FEGFEGFEGwowhelloFEGFEGonemoreFEG','NONEARELOWERCASE'] #sequences with uppercase and potentially lowercase letters

これは私が出力したいものです

upper_output = ['CABCABCABCABCAB','FEGFEGFEGFEGFEGFEG','NONEARELOWERCASE'] #the upper case letters joined together
lower_output = [['defgdefgdefg'],['wowhello','onemore'],[]] #the lower case letters in lists within lists
lower_indx = [[9],[9,23],[]] #where the lower case values occur in the original sequence

だから、lower_output リストを SUBLIST のリストにしたい。SUBLISTS にはすべて小文字の文字列が含まれます。

私は正規表現を使用することを考えていました。. .

import re

lower_indx = []

for seq in sequences:
    lower_indx.append(re.findall("[a-z]", seq).start())

print lower_indx

私が試していた小文字のリストの場合:

lower_output = []

for seq in sequences:
    temp = ''
    temp = re.findall("[a-z]", seq)
    lower_output.append(temp)

print lower_output

しかし、値は別々のリストにありません(まだ結合する必要があります)

[['d', 'e', 'f', 'g', 'd', 'e', 'f', 'g', 'd', 'e', 'f', 'g'], ['w', 'o', 'w', 'h', 'e', 'l', 'l', 'o', 'o', 'n', 'e', 'm', 'o', 'r', 'e'], []]
4

2 に答える 2

0

正規表現とは別に、itertools.groupbyここでも使用できます:

In [39]: sequences = ['CABCABCABdefgdefgdefgCABCAB','FEGFEGFEGwowhelloFEGFEGonemoreFEG','NONEARELOWERCASE'] #sequences with uppercase and potentially lowercase letters

In [40]: lis=[["".join(v) for k,v in groupby(x,key=lambda z:z.islower())] for x in sequences]

In [41]: upper_output=["".join(x[::2]) for x in lis]

In [42]: lower_output=[x[1::2] for x in lis]

In [43]: upper_output
Out[43]: ['CABCABCABCABCAB', 'FEGFEGFEGFEGFEGFEG', 'NONEARELOWERCASE']

In [44]: lower_output
Out[44]: [['defgdefgdefg'], ['wowhello', 'onemore'], []]

In [45]: lower_indx=[[sequences[i].index(y) for y in x] for i,x in enumerate(lower_output)]

In [46]: lower_indx
Out[46]: [[9], [9, 23], []]
于 2013-04-04T21:04:15.690 に答える