list of word from a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DDCane
    New Member
    • Oct 2007
    • 16

    #1

    list of word from a string

    Im a beginner and im tryin to do a couple things.
    #1. i want to def a function "func(ListOWord s)" for exampleand what i want it to do is take a list of words for example (["dod","fre","sd f","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","sd f","ser","ftre" ,"dfr"])
    ["dod","sdf","ft re"],["fre","ser","df r"]

    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.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by DDCane
    Im a beginner and im tryin to do a couple things.
    #1. i want to def a function "func(ListOWord s)" for exampleand what i want it to do is take a list of words for example (["dod","fre","sd f","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","sd f","ser","ftre" ,"dfr"])
    ["dod","sdf","ft re"],["fre","ser","df r"]

    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:[code=Python]>>> words = ["dod","fre","sd f","ser","ftre" ,"dfr"]
    >>> even = range(0, len(words), 2)
    >>> odd = range(1, len(words), 2)
    >>> [words[i] for i in even], [words[i] for i in odd]
    (['dod', 'sdf', 'ftre'], ['fre', 'ser', 'dfr'])
    >>> [/code]

    Comment

    • KaezarRex
      New Member
      • Sep 2007
      • 52

      #3
      Originally posted by DDCane
      Im a beginner and im tryin to do a couple things.
      #1. i want to def a function "func(ListOWord s)" for exampleand what i want it to do is take a list of words for example (["dod","fre","sd f","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","sd f","ser","ftre" ,"dfr"])
      ["dod","sdf","ft re"],["fre","ser","df r"]

      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.
      [CODE=python]>>> words = "dod,fre,sdf,se r,ftre,dfr"
      >>> ListOWords = words.split("," )
      >>> print ListOWords
      ['dod', 'fre', 'sdf', 'ser', 'ftre', 'dfr'][/CODE]
      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.
      [CODE=python]>>> ListOWords = ['dod', 'fre', 'sdf', 'ser', 'ftre', 'dfr']
      >>> print ListOWords[1:4]
      ['fre', 'sdf', 'ser']
      >>> print ListOWords[:4]#starts at the first index
      ['dod', 'fre', 'sdf', 'ser']
      >>> print ListOWords[1:]#ends at the last index
      ['fre', 'sdf', 'ser', 'ftre', 'dfr']
      >>> print ListOWords[:]#starts at the first index and ends at the last index
      ['dod', 'fre', 'sdf', 'ser', 'ftre', 'dfr'][/CODE]
      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.
      [CODE=python]>>> ListOWords = ["dod","fre","sd f","ser","ftre" ,"dfr"]
      >>> list1 = ListOWords[::2]#same as list1 = ListOWords[0:6:2]
      >>> list2 = ListOWords[1::2]#same as list1 = ListOWords[1:6:2]
      >>> print list1, list2
      ['dod', 'sdf', 'ftre'] ['fre', 'ser', 'dfr'][/CODE]
      If you want, you can do it all in one step.
      [CODE=python]>>> ListOWords = ['dod', 'fre', 'sdf', 'ser', 'ftre', 'dfr']
      >>> print ListOWords[::2], ListOWords[1::2]
      ['dod', 'sdf', 'ftre'] ['fre', 'ser', 'dfr'][/CODE]
      I hope that all makes sense.

      Comment

      • DDCane
        New Member
        • Oct 2007
        • 16

        #4
        Thanks u guys this really helped. i ahve some more questions but ill make a new discussion. hop eu read it soon!

        Comment

        Working...