Python:双条件字符串比较
Python: string comparison with double conditions
正在尝试在 2 个列表中搜索常用字符串。 1-st 列表是一个包含文本的文件,而 2-nd 是在实际单词之前具有对数概率的单词列表 - 以匹配,一个词不仅需要在两个列表中,而且还具有一定的最小对数概率(例如,在 -2,123456 和 0,000000 之间;即负 2 increasing up到 0)。制表符分隔列表可能如下所示:
-0.962890 dog
-1.152454 lol
-2.050454 cat
我在做这样的事情时遇到了困难:
common = []
for i in list1:
if i in list2 and re.search("\-[0-1]\.[\d]+", list2):
common.append(i)
简单地预处理列表以删除低于某个阈值的行的想法当然是有效的,但是由于单词及其概率都在同一行上,条件不是也可以吗? (正则表达式不是必需的,但对于有无正则表达式的比较解决方案都会很有趣。)
编辑:下面这个问题。
假设您的列表包含 "-0.744342 dog"
等字符串,而 my_word_list
是一个字符串列表,那么如何:
worddict = dict(map(lambda x: (x, True), my_word_list))
import re
for item in my_list:
parts = re.split("\s+", item)
if len(parts) != 2:
raise ValueError("Wrong data format")
logvalue, word = float(parts[0]), parts[1]
if logvalue > TRESHHOLD and word in worddict:
print("Hurrah, a match!")
注意第一行,它从您的单词列表中创建了一个字典。如果你不这样做,你会浪费很多时间在你的单词列表中进行线性搜索,导致你的算法的时间复杂度为 O(n*m),而我提出的解决方案更接近于 O (n+m),n为my_list
行数,m为my_word_list
.
字数
这是我没有使用 regex
的解决方案。首先创建一个接受范围内的单词的字典,然后检查文本中的每个单词是否在字典中。
word_dict = {}
with open('probability.txt', 'r') as prob, open('text.txt', 'r') as textfile:
for line in prob:
if (-2.123456 < float(line.split()[0]) < 0):
word_dict[line.split()[1]] = line.split()[0]
for line in textfile:
for word in line.split():
if word in word_dict.keys():
print('MATCH, {}: {}'.format(word, word_dict[word]))
在经过数小时的反复试验后回答我自己的问题,并阅读各处的提示。事实证明,我从一开始就在朝着正确的方向思考,但需要将单词检测和模式匹配分开,并将后者与对数概率检查结合起来。因此创建一个具有所需日志概率的临时项目列表,然后将其与文本文件进行比较。
common = []
prob = []
loga , rithmus = -9.87 , -0.01
for i in re.findall("\-\d\.\d+", list2):
if (loga < float(i.split()[0]) < rithmus):
prob.append(i)
prob = "\n".join(prob)
for i in list1:
if i in prob:
common.append(i)
正在尝试在 2 个列表中搜索常用字符串。 1-st 列表是一个包含文本的文件,而 2-nd 是在实际单词之前具有对数概率的单词列表 - 以匹配,一个词不仅需要在两个列表中,而且还具有一定的最小对数概率(例如,在 -2,123456 和 0,000000 之间;即负 2 increasing up到 0)。制表符分隔列表可能如下所示:
-0.962890 dog
-1.152454 lol
-2.050454 cat
我在做这样的事情时遇到了困难:
common = []
for i in list1:
if i in list2 and re.search("\-[0-1]\.[\d]+", list2):
common.append(i)
简单地预处理列表以删除低于某个阈值的行的想法当然是有效的,但是由于单词及其概率都在同一行上,条件不是也可以吗? (正则表达式不是必需的,但对于有无正则表达式的比较解决方案都会很有趣。)
编辑:
假设您的列表包含 "-0.744342 dog"
等字符串,而 my_word_list
是一个字符串列表,那么如何:
worddict = dict(map(lambda x: (x, True), my_word_list))
import re
for item in my_list:
parts = re.split("\s+", item)
if len(parts) != 2:
raise ValueError("Wrong data format")
logvalue, word = float(parts[0]), parts[1]
if logvalue > TRESHHOLD and word in worddict:
print("Hurrah, a match!")
注意第一行,它从您的单词列表中创建了一个字典。如果你不这样做,你会浪费很多时间在你的单词列表中进行线性搜索,导致你的算法的时间复杂度为 O(n*m),而我提出的解决方案更接近于 O (n+m),n为my_list
行数,m为my_word_list
.
这是我没有使用 regex
的解决方案。首先创建一个接受范围内的单词的字典,然后检查文本中的每个单词是否在字典中。
word_dict = {}
with open('probability.txt', 'r') as prob, open('text.txt', 'r') as textfile:
for line in prob:
if (-2.123456 < float(line.split()[0]) < 0):
word_dict[line.split()[1]] = line.split()[0]
for line in textfile:
for word in line.split():
if word in word_dict.keys():
print('MATCH, {}: {}'.format(word, word_dict[word]))
在经过数小时的反复试验后回答我自己的问题,并阅读各处的提示。事实证明,我从一开始就在朝着正确的方向思考,但需要将单词检测和模式匹配分开,并将后者与对数概率检查结合起来。因此创建一个具有所需日志概率的临时项目列表,然后将其与文本文件进行比较。
common = []
prob = []
loga , rithmus = -9.87 , -0.01
for i in re.findall("\-\d\.\d+", list2):
if (loga < float(i.split()[0]) < rithmus):
prob.append(i)
prob = "\n".join(prob)
for i in list1:
if i in prob:
common.append(i)