Find the answer to your Linux question:
Results 1 to 5 of 5
Hi again, I'm writing a program that builds words letter by letter, and I was wondering if there's a better solution than this: Code: word = ""; for i in ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942

    Efficient concatenation in Python?


    Hi again, I'm writing a program that builds words letter by letter, and I was wondering if there's a better solution than this:

    Code:
    word = "";
    for i in range(0, length):
       ...
        word = word + newChar
    return word
    Is there something like a string buffer that I can use, or a method that converts a list into a string?

    Thanks a lot for the help!

  2. #2
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    I am a mere python novice, but as far as I know, something like

    Code:
    for item in list1: 
      string1 = string1 + item
    is the way to go. I checked the docs and didn't see a function that converts a list object to a string object.

    Could you give a little more background on what it is you're doing? There may be another answer from a different angle. I don't understand why you are building words letter by letter.

  3. #3
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942
    I'm writing a program to generate random words based on a given definition. For example,

    a V 100 50 50

    in the input file tells the program that there's a vowel (V) named 'a' with an initial frequency of 100, a medial frequency of 50, and final frequency of 50. The input file also defines a set of patterns, such as CVC, so the code looks something like this:

    Code:
    words = {}
    for i in range(0, numWordsToGenerate):
       pattern = selectPattern(patterns) # patterns is a list of patterns
       word = ""
       for j in range(0, len(pattern)):
          char = pattern[j]
          options = phonemes[char] # phonemes is a dictionary with word
                                                 # components
          phoneme = selectPhoneme(phonemes, j)
          word = word + phoneme
       if(word in words):
          i-- # Don't allow repeats
          continue
       words{word} = 0 # Just a kind of marker, faster lookup
    for k in words.keys():
       print k
    I just don't like the idea of creating so many new strings each iteration; it seems like a tax on the garbage collector!

    Also, do you guys think a program like this is freshmeat worthy? I'd like to start giving back to the open source community.

    Thanks again!

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    Well, it's an interesting project.

    I don't worry too much about extra strings being created if it is getting the job done. If you have really limited resources to work with then maybe it's more of an issue for you, I don't know.

    One comment that I can't resist:
    Code:
    if(word in words):
       i-- # Don't allow repeats
       continue
    If you are concerned about performance then allow the repeats. I would be curious to know how many times it's hitting that clause and starting that iteration over! (Not to mention that I don't like messing with iterators - makes things tricky to debug.)

    edit: Actually, I believe the code I cited above (that messes with the iterator) is not even doing anything. If you would just let it create the new dictionary key it would work itself out (since the keys are unique anyway).

    As for "freshmeat worthy" I am definitely no judge of that. There are a couple OSS app maintainers who hang around here who can hopefully give you some advice.

    Just my two cents.

  6. #5
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    To illustrate my point about dictionaries (and how they will enforce unique keys on their own), here is an example:

    Code:
    dict1 = {'blonde': 0, 'brunette': 0}
    
    print dict1.keys()
    
    dict1['red'] = 0
    dict1['blonde'] = 0
    
    print dict1.keys()
    This produces
    Code:
    ['blonde', 'brunette']
    ['blonde', 'brunette', 'red']
    Note that in spite of being added twice, "blonde" only existed once as a dictionary key.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •