sum is actually a built-in function so you can easily change it a single line: sum(range(1, n+1, 2))
SoulMazer commented: Continued, quality support. +1
vegaseat commented: clever indeed, thanks +12
sum is actually a built-in function so you can easily change it a single line: sum(range(1, n+1, 2))
A class variable is shared across instances of the class, while an instance variable is unique to that instance of the class. Consider the following example:
class Foo:
name = "FOO"
def __init__(self, value):
self.value = value
mine = Foo("bar")
mine_2 = Foo("taz")
print mine.value, mine.name
print mine_2.value, mine.name
Foo.name = "BAR"
print mine.value, mine.name
print mine_2.value, mine.name
#output is:
# bar FOO
# taz FOO
# bar BAR
# taz BAR
Use the "in" keyword.
**--- will return a positive for words like "strunk" found = False for word in ["trunk", "branches"] : if word in words[1]: found = True if not found: print "processing this"
Wouldn't it make sense to do this the opposite way? i.e.
if words[1] in ["trunk", "branches"]:
found = True
else:
found = False
Short answer, you don't do this. Usually when you want to get values from a thread you pass it a queue, and have a your main thread get values from the same queue.
A couple other things: you're calling join(), which will block until the thread is finished making the while loop directly after it unnecessary, and don't use "str" as a variable name since it's already the name of a builtin function.
import threading
import Queue
def stringFunction(value, out_queue):
my_str = "This is string no. " + value
out_queue.put(my_str)
my_queue = Queue.Queue()
thread1 = threading.Thread(stringFunction("one", my_queue))
thread1.start()
thread1.join()
func_value = my_queue.get()
print func_value
Always use absolute path+file names.
filePath = "Dataset/parameter feature vectors" for fname in os.listdir(filePath): complete_name = os.path.join(filePath, fname) data_str = open(complete_name).read() index = data_str.find("female") if index != -1: females.append(index) print fname else: print "append the ones that aren't female to a males"
I don't think this is doing what bol0gna wants actually. In the original post bol0gna wants to sort on the filenames, while your code is actually searching the content of each file. Here's a version that works on the filename:
filePath = "Dataset/parameter feature vectors"
for fname in os.listdir(filePath):
if fname.count('female'):
females.append(fname)
elif fname.count('male'):
males.append(fname)
Opening it unbuffered means that when you call the file's write function the data will be written to disk immediately. This means that you could potentially be accessing the hard drive more frequently. If you are calling flush after each write already though then there won't be any difference there.
You can also open a file in line buffer mode by changing the '0' to a '1'. As the name implies this will buffer writing until you've written an entire line. Depending on what you're doing that might be a good middle ground.
The problem is that you're creating a raw string, which isn't doing what you think it will. Instead of allowing you to create a hex value attached to the '\x' escape, it's instead creating a string that contains the characters exactly as '\' + 'x' + 'f' + '4'.
Here's a version that will do what you want (I hope :) ):
import re
def convert_to_unicode(match_obj):
raw_chr_val = match_obj.group(1)
return unichr(int(raw_chr_val, 16))
def func(mylist):
for e in mylist:
e = re.sub(' ?&# x([0-9a-f]*);', convert_to_unicode, e)
print e
mylist = ['12 angry men', 'Rash &# xf4;mon']
func(mylist)
Seems like a good case for a regular expression
import re
test_input = "AND Category 07|Spec 01|ABC 01 AND Category 07|Spec 02|XYZ 02 AND Category 07|Spec 03|PQR 03 "
test_output = re.sub('\|[A-Z]{3} [0-9]{2}', '', test_input)
There's always a more clever way :-)
start = 10
end = 20
open(outfile, 'w').writelines(open(infile).readlines()[start:end])