Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6715171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:34:58+00:00 2026-05-26T08:34:58+00:00

Python 2.4.x I have a series of lines that I’m reading in – rearranging

  • 0

Python 2.4.x

I have a series of lines that I’m reading in – rearranging and then making each line into a list which is nested inside another list.

I’ve got the following.

testposition = (22, 3, 1, 2, 18, 19, 5, 6, 8, 9, 12, 23, 24, 25, 26, 27, 28)

def giveme(s, words=()):
    lista = s.split()
    return [lista[item-1] for item in words]

for rec in testline:
    testlist.append(giveme(rec, testposition))

In other words – if the rec being passed is.

Lorem ipsum dolor sit amet consectetur adipiscing elit In vitae neque nec magna tristique ornare Cras faucibus risus eu odio pharetra interdum Nunc dui mi rhoncus ut aliquet

then the list for this line would be. (after rearranging). (this list would get appended to another list – nested lists).

['interdum', 'dolar', 'Lorem', 'ipsum', 'risus', 'eu', 'amet', 'consectetur', 'elit', 'In', 'nec', 'Nunc', 'dui', 'mi', 'rhoncus', 'ut', 'aliquet']

Which it does fantastically well. However what I’d like to do is take the last 6 and group them together like this.

['interdum', 'dolar', 'Lorem', 'ipsum', 'risus', 'eu', 'amet', 'consectetur', 'elit', 'In', 'nec', 'Nunc dui mi rhoncus ut aliquet']

And if the line being passed does not have 28 elements but say only 25 – than it just groups whatever beyond the 22nd element – (the text is not the same line after line).

i.e the above example has 28 elements, this one has 25.

Lorem ipsum dolor sit amet consectetur adipiscing elit In vitae neque nec magna tristique ornare Cras faucibus risus eu odio pharetra interdum Nunc dui mi

and the resulting list would be:

['interdum', 'dolar', 'Lorem', 'ipsum', 'risus', 'eu', 'amet', 'consectetur', 'elit', 'In', 'nec', 'Nunc dui mi']

Hope that’s clear – any ideas?

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:34:58+00:00Added an answer on May 26, 2026 at 8:34 am

    If you supply the optional second argument to str.split, you can control how many splits are performed:

    testposition = (22, 3, 1, 2, 18, 19, 5, 6, 8, 9, 12, 23)
    
    def giveme(s, words=()):
        lista = s.split(' ',22)
        result=[lista[item-1] for item in words]
        return result
    
    rec='Lorem ipsum dolor sit amet consectetur adipiscing elit In vitae neque nec magna tristique ornare Cras faucibus risus eu odio pharetra interdum Nunc dui mi rhoncus ut aliquet'
    print(giveme(rec, testposition)[-1])
    

    yields

    'Nunc dui mi rhoncus ut aliquet'
    

    while

    rec='Lorem ipsum dolor sit amet consectetur adipiscing elit In vitae neque nec magna tristique ornare Cras faucibus risus eu odio pharetra interdum Nunc dui mi'
    print(giveme(rec, testposition)[-1])
    

    yields

    'Nunc dui mi'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Python script that manages a series of CasperJS tasks and processes
I have a CSV-like text file that has about 1000 lines. Between each record
I have a python script that makes a series of url calls using urllib2.
I have a Python program that runs a series of experiments, with no data
I'm a Python newbie. I have a series of objects that need to be
I have a series of maintenance tasks for a python WSGI application that are
I have a series of Python classes in a file. Some classes reference others.
So I have this code in python that writes some values to a Dictionary
So I have a square that's made up of a series of points. At
When I execute my python script from the command line I have no problems

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.