473,414 Members | 1,667 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

list of word from a string

16
Im a beginner and im tryin to do a couple things.
#1. i want to def a function "func(ListOWords)" for exampleand what i want it to do is take a list of words for example (["dod","fre","sdf","ser","ftre","dfr"]) and i want it to split them at the commas and rewrite them printing everyother word first then the remaining words after. that sounds alittle weird but it should look like this:

>>>func(["dod","fre","sdf","ser","ftre","dfr"])
["dod","sdf","ftre"],["fre","ser","dfr"]

anyone know how to do this?

this is what i got so far
def func(ListOWords):
[i.split() for i in ListOWords]
return ListOWords

can someone please help.
Oct 24 '07 #1
3 1526
bvdet
2,851 Expert Mod 2GB
Im a beginner and im tryin to do a couple things.
#1. i want to def a function "func(ListOWords)" for exampleand what i want it to do is take a list of words for example (["dod","fre","sdf","ser","ftre","dfr"]) and i want it to split them at the commas and rewrite them printing everyother word first then the remaining words after. that sounds alittle weird but it should look like this:

>>>func(["dod","fre","sdf","ser","ftre","dfr"])
["dod","sdf","ftre"],["fre","ser","dfr"]

anyone know how to do this?

this is what i got so far
def func(ListOWords):
[i.split() for i in ListOWords]
return ListOWords

can someone please help.
Maybe this will help:
Expand|Select|Wrap|Line Numbers
  1. >>> words = ["dod","fre","sdf","ser","ftre","dfr"]
  2. >>> even = range(0, len(words), 2)
  3. >>> odd = range(1, len(words), 2)
  4. >>> [words[i] for i in even], [words[i] for i in odd]
  5. (['dod', 'sdf', 'ftre'], ['fre', 'ser', 'dfr'])
  6. >>> 
Oct 24 '07 #2
Im a beginner and im tryin to do a couple things.
#1. i want to def a function "func(ListOWords)" for exampleand what i want it to do is take a list of words for example (["dod","fre","sdf","ser","ftre","dfr"]) and i want it to split them at the commas and rewrite them printing everyother word first then the remaining words after. that sounds alittle weird but it should look like this:

>>>func(["dod","fre","sdf","ser","ftre","dfr"])
["dod","sdf","ftre"],["fre","ser","dfr"]

anyone know how to do this?

this is what i got so far
def func(ListOWords):
[i.split() for i in ListOWords]
return ListOWords

can someone please help.
When you first get the list, you don't have to split it on the commas, because the commas aren't actually there. They are just a way of showing the separate elements in a list when your making a list or printing a list to the screen. You would want to use the split function to make a list if you had one long string with commas in it.
Expand|Select|Wrap|Line Numbers
  1. >>> words = "dod,fre,sdf,ser,ftre,dfr"
  2. >>> ListOWords = words.split(",")
  3. >>> print ListOWords
  4. ['dod', 'fre', 'sdf', 'ser', 'ftre', 'dfr']
You already have the words in the form of a list, so you can skip that part.

To accomplish what you want to do, I'd recommend slicing the list. To slice a list you put brackets after the list name like you would to access an item in the list, but instead of putting the index of one item inside the brackets, you put two separated with a semicolon. That will return a list of everything between those two indexes.
Expand|Select|Wrap|Line Numbers
  1. >>> ListOWords = ['dod', 'fre', 'sdf', 'ser', 'ftre', 'dfr']
  2. >>> print ListOWords[1:4]
  3. ['fre', 'sdf', 'ser']
  4. >>> print ListOWords[:4]#starts at the first index
  5. ['dod', 'fre', 'sdf', 'ser']
  6. >>> print ListOWords[1:]#ends at the last index
  7. ['fre', 'sdf', 'ser', 'ftre', 'dfr']
  8. >>> print ListOWords[:]#starts at the first index and ends at the last index
  9. ['dod', 'fre', 'sdf', 'ser', 'ftre', 'dfr']
Now there is one more option you have when slicing. By using another semicolon and number inside the brackets, you can change the "step" size. This allows you to pick every second or third or fourth or ... number. That's the key to solving your problem.
Expand|Select|Wrap|Line Numbers
  1. >>> ListOWords = ["dod","fre","sdf","ser","ftre","dfr"]
  2. >>> list1 = ListOWords[::2]#same as list1 = ListOWords[0:6:2]
  3. >>> list2 = ListOWords[1::2]#same as list1 = ListOWords[1:6:2]
  4. >>> print list1, list2
  5. ['dod', 'sdf', 'ftre'] ['fre', 'ser', 'dfr']
If you want, you can do it all in one step.
Expand|Select|Wrap|Line Numbers
  1. >>> ListOWords = ['dod', 'fre', 'sdf', 'ser', 'ftre', 'dfr']
  2. >>> print ListOWords[::2], ListOWords[1::2]
  3. ['dod', 'sdf', 'ftre'] ['fre', 'ser', 'dfr']
I hope that all makes sense.
Oct 24 '07 #3
DDCane
16
Thanks u guys this really helped. i ahve some more questions but ill make a new discussion. hop eu read it soon!
Oct 24 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: tjland | last post by:
Okay so im working on a very simple encryption method using just loops. Kind of novel i think. Okay so first i set up a list of the alphabet with just every seperate letter, then user is prompted...
2
by: s | last post by:
I'm getting compile errors on the following code: <code> #include <iostream> #include <fstream> #include <list> #include <string> using namespace std;
5
by: Little | last post by:
I have this program and I need to work on the test portion, which tests if a Val is in the list. It returns false no matter what could you look at the part and see what might need to be done to fix...
23
by: comp.lang.tcl | last post by:
I have a TCL proc that needs to convert what might be a list into a string to read consider this: ]; # OUTPUTS Hello World which is fine for PHP ]; # OUTPUT {{-Hello}} World, which PHP...
7
by: aine_canby | last post by:
Hi, Im totally new to Python so please bare with me. Data is entered into my program using the folling code - str = raw_input(command) words = str.split() for word in words:
9
by: Sheldon | last post by:
Hi, I am trying to understand linked lists and the different ways to write a linked list and double linked list. I have been trying to get this function called insert_word to work but to no...
6
by: nagar | last post by:
I need to get the list of Autocorrect entries in word. Is there a way to do it without connecting to Word? Is the list saved somewhere? If I need to connect to Word, how can I detect if it's...
9
by: shapper | last post by:
Hello, How can I filter a List(Of String)? I need to get the list elements which start with the letters contained in the variable Text. Thanks, Miguel
0
by: saijin | last post by:
I'm planning to call a list of data from an XML file but when I duplicate the content inside the <data></data> it is not showing anything Here's the ActionScript 3.0 import...
4
by: jerry | last post by:
i have a problem when i read c++ primer ,i don't know what the follow codes meaing. char *word={"frank","english","edali","slina"}; size_t word_size=sizeof(word)/sizeof(char*);...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.