Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

python - Get a certain string from the first elements - array

Given an array: arr = ['Get your face right in there.', 'You’re like, wait, though, isn’t that too close? PUH-LEEZ. You and I both know that a person can never be too close to those browned lasagna edges that are the perfect amount of chewy, and that’s exactly why we like to hang out together and get wild about all the foods. That’s this lasagna florentine.']

I want to get a string from the array with elements with a total of 15 words or more, in it. So, since the first element does no have 15 words, the output will go over to the next element, and will output the whole array (in this case). ex. If the first element has 12 words and second has 20, the output string will have 12+20 = 32 words.

Here's my code:

def CleanText(text):
    text = str(text)
    forbidden = [r'
', r'', r'.', r'?', r'!', r'(', r')', r'/', r'<', r'>', '\', '\r', '</div>', '</a>']
    for i in forbidden:
        text.replace(i, '')
    return text

def ExtractText(arr):
    for i in arr:
        #return i
        if len(i) >= 15:
            return CleanText(i)
        elif len(arr[i]) < 15:
            attachedString = CleanText(arr[i+1])
            return CleanText(arr[i]) + attachedString

arr = ['Get your face right in there.', 'You&#8217;re like, wait, though, isn&#8217;t that too close? PUH-LEEZ. You and I both know that a person can never be too close to those browned lasagna edges that are the perfect amount of chewy, and that&#8217;s exactly why we like to hang out together and get wild about all the foods. That&#8217;s this lasagna florentine.']
print(ExtractFirstPara(arr))

Output: Get your face right in there. However, it comes alright when I don't generalize it. That is, when I explicitly say this:

def ExtractText(arr):
    for i in arr[0]:
        if len(i) >= 15:
            return CleanText(arr[0])
        else:
            attachedString = CleanText(arr[1])
            return CleanText(arr[0]) + attachedString

Output:

Get your face right in there.You&#8217;re like, wait, though, isn&#8217;t that too close? PUH-LEEZ. You and I both know that a person can never be too close to those browned lasagna edges that are the perfect amount of chewy, and that&#8217;s exactly why we like to hang out together and get wild about all the foods. That&#8217;s this lasagna florentine.

However, in certain cases, where a string's length with the first two elements does not add up to 15, this explicitness (if that's even a word) is not possible. For this purpose, I need to generalize.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

here try this:

def CleanText(text):
    text = str(text)
    forbidden = [r'
', r'', r'.', r'?', r'!', r'(', r')', r'/', r'<', r'>', '\', '\r', '</div>', '</a>']
    for i in forbidden:
        text.replace(i, '')
    return text
    
def ExtractText(arr):
    string = ''
    for i in arr:
        string += i
        if len(string.split()) >= 15:
            return string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...