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.
- 09-27-2005 #1
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:
Is there something like a string buffer that I can use, or a method that converts a list into a string?Code:word = ""; for i in range(0, length): ... word = word + newChar return word
Thanks a lot for the help!
- 09-27-2005 #2
I am a mere python novice, but as far as I know, something like
is the way to go. I checked the docs and didn't see a function that converts a list object to a string object.Code:for item in list1: string1 = string1 + item
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.
- 09-27-2005 #3
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:
I just don't like the idea of creating so many new strings each iteration; it seems like a tax on the garbage collector!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
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!
- 09-27-2005 #4
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: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.)Code:if(word in words): i-- # Don't allow repeats continue
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.
- 09-27-2005 #5
To illustrate my point about dictionaries (and how they will enforce unique keys on their own), here is an example:
This producesCode:dict1 = {'blonde': 0, 'brunette': 0} print dict1.keys() dict1['red'] = 0 dict1['blonde'] = 0 print dict1.keys()
Note that in spite of being added twice, "blonde" only existed once as a dictionary key.Code:['blonde', 'brunette'] ['blonde', 'brunette', 'red']


Reply With Quote
