Find an item of a list in a string [on hold]
$begingroup$
I have a list of verbs all of length 2(about 1000 verbs), and a file containing millions of lines which all contain just one of the verbs. What I should do is find the index of the verb in each line.
And here is my code:
lines = [......] # many many lines from a file, that can be read line by line
verbs = defaultdict(int)
for v in verbs:
verbs[v] += 1 # the frequency is also available
def find_verb_index(line):
for i in range(0, len(line)-1):
if verbs.get([line[i:i+2], 0):
return i
return None
count = 0
for line in lines:
idx = fine_verb_index(line)
if idx is not None:
count += 1
Any suggestions for improvements? Or is using re.finditer more effecient? Why? Thanks.
python strings search
$endgroup$
put on hold as off-topic by Graipher, 200_success, Mast, Ludisposed, Toby Speight 12 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Graipher, 200_success, Mast, Toby Speight
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
$begingroup$
I have a list of verbs all of length 2(about 1000 verbs), and a file containing millions of lines which all contain just one of the verbs. What I should do is find the index of the verb in each line.
And here is my code:
lines = [......] # many many lines from a file, that can be read line by line
verbs = defaultdict(int)
for v in verbs:
verbs[v] += 1 # the frequency is also available
def find_verb_index(line):
for i in range(0, len(line)-1):
if verbs.get([line[i:i+2], 0):
return i
return None
count = 0
for line in lines:
idx = fine_verb_index(line)
if idx is not None:
count += 1
Any suggestions for improvements? Or is using re.finditer more effecient? Why? Thanks.
python strings search
$endgroup$
put on hold as off-topic by Graipher, 200_success, Mast, Ludisposed, Toby Speight 12 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Graipher, 200_success, Mast, Toby Speight
If this question can be reworded to fit the rules in the help center, please edit the question.
3
$begingroup$
This sounds a lot like pseudo code and not your real code. Can you add a (short) example of the file (and thereforelines
)? How are you doing that file read? The iteration oververbs
will not do anything because it is empty as currently defined. The lineif verbs.get([line[i:i+2], 0):
contains aSyntaxError
(get rid of the first opening[
). Your function is calledfind_verb_index
, but you call it withfine_verb_index
.
$endgroup$
– Graipher
14 hours ago
add a comment |
$begingroup$
I have a list of verbs all of length 2(about 1000 verbs), and a file containing millions of lines which all contain just one of the verbs. What I should do is find the index of the verb in each line.
And here is my code:
lines = [......] # many many lines from a file, that can be read line by line
verbs = defaultdict(int)
for v in verbs:
verbs[v] += 1 # the frequency is also available
def find_verb_index(line):
for i in range(0, len(line)-1):
if verbs.get([line[i:i+2], 0):
return i
return None
count = 0
for line in lines:
idx = fine_verb_index(line)
if idx is not None:
count += 1
Any suggestions for improvements? Or is using re.finditer more effecient? Why? Thanks.
python strings search
$endgroup$
I have a list of verbs all of length 2(about 1000 verbs), and a file containing millions of lines which all contain just one of the verbs. What I should do is find the index of the verb in each line.
And here is my code:
lines = [......] # many many lines from a file, that can be read line by line
verbs = defaultdict(int)
for v in verbs:
verbs[v] += 1 # the frequency is also available
def find_verb_index(line):
for i in range(0, len(line)-1):
if verbs.get([line[i:i+2], 0):
return i
return None
count = 0
for line in lines:
idx = fine_verb_index(line)
if idx is not None:
count += 1
Any suggestions for improvements? Or is using re.finditer more effecient? Why? Thanks.
python strings search
python strings search
edited 16 hours ago
lerner
asked 17 hours ago
lernerlerner
1828
1828
put on hold as off-topic by Graipher, 200_success, Mast, Ludisposed, Toby Speight 12 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Graipher, 200_success, Mast, Toby Speight
If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as off-topic by Graipher, 200_success, Mast, Ludisposed, Toby Speight 12 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Graipher, 200_success, Mast, Toby Speight
If this question can be reworded to fit the rules in the help center, please edit the question.
3
$begingroup$
This sounds a lot like pseudo code and not your real code. Can you add a (short) example of the file (and thereforelines
)? How are you doing that file read? The iteration oververbs
will not do anything because it is empty as currently defined. The lineif verbs.get([line[i:i+2], 0):
contains aSyntaxError
(get rid of the first opening[
). Your function is calledfind_verb_index
, but you call it withfine_verb_index
.
$endgroup$
– Graipher
14 hours ago
add a comment |
3
$begingroup$
This sounds a lot like pseudo code and not your real code. Can you add a (short) example of the file (and thereforelines
)? How are you doing that file read? The iteration oververbs
will not do anything because it is empty as currently defined. The lineif verbs.get([line[i:i+2], 0):
contains aSyntaxError
(get rid of the first opening[
). Your function is calledfind_verb_index
, but you call it withfine_verb_index
.
$endgroup$
– Graipher
14 hours ago
3
3
$begingroup$
This sounds a lot like pseudo code and not your real code. Can you add a (short) example of the file (and therefore
lines
)? How are you doing that file read? The iteration over verbs
will not do anything because it is empty as currently defined. The line if verbs.get([line[i:i+2], 0):
contains a SyntaxError
(get rid of the first opening [
). Your function is called find_verb_index
, but you call it with fine_verb_index
.$endgroup$
– Graipher
14 hours ago
$begingroup$
This sounds a lot like pseudo code and not your real code. Can you add a (short) example of the file (and therefore
lines
)? How are you doing that file read? The iteration over verbs
will not do anything because it is empty as currently defined. The line if verbs.get([line[i:i+2], 0):
contains a SyntaxError
(get rid of the first opening [
). Your function is called find_verb_index
, but you call it with fine_verb_index
.$endgroup$
– Graipher
14 hours ago
add a comment |
0
active
oldest
votes
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
3
$begingroup$
This sounds a lot like pseudo code and not your real code. Can you add a (short) example of the file (and therefore
lines
)? How are you doing that file read? The iteration oververbs
will not do anything because it is empty as currently defined. The lineif verbs.get([line[i:i+2], 0):
contains aSyntaxError
(get rid of the first opening[
). Your function is calledfind_verb_index
, but you call it withfine_verb_index
.$endgroup$
– Graipher
14 hours ago