我有"dinosaur", "dosimetry", and "moist"
一词。我正在考虑有数十万个单词的情况。我想返回字符串中任何地方包含"s", "i", "o", "m"
的所有单词。该函数应返回"dosimetry", "moist"
。
是否有一种有效的方法来执行此操作,或者我必须进行迭代并检查?
答案 0 :(得分:3)
A = ['o', 'i', 's', 'm']
words = ["dinosaur", "dosimetry", "moist", "personal", "since",
"including", "guide", "shop", "directory", "board", "location",
"change", "white", "text", "small", "emotions", "rating",
"rate", "movies", "government"]
这里有两种方法返回words
中的单词,这些单词包含A
中包含的所有字母。
#1
def select_some(words)
words.select { |word| A & word.chars == A }
end
select_some(words)
#=> ["dosimetry", "moist", "emotions", "movies"]
手术线可以更改为
words.select { |word| (A-str.chars).empty? }
#2
n = 1
H = A.each_with_object({}) do |c,h|
h[c] = n
n <<= 1
end
#=> {"s"=>1, "i"=>2, "o"=>4, "m"=>8}
N = n - 1
#=> 15
def select_some(words)
words.select do |word|
n = 0
word.each_char do |c|
x = H[c]
n |= x if x
end
n == N
end
end
select_some(words)
#=> ["dosimetry", "moist", "emotions", "movies"]
答案 1 :(得分:3)
只是为了经验
使用正则表达式正向查找
words = %w(dinosaur dosimetry moist)
words.select { |word| word.match?(/(?=.*m)(?=.*s)(?=.*i)(?=.*o).*/) }
#=> ["dosimetry", "moist"]
为了提高搜索速度,我按照English Letter Frequency在正则表达式中排列了字母。
答案 2 :(得分:3)
根据要求,以更具可读性/永久性的形式发布我的比较基准。
side = convert(float(format(side, '.3f')))
结果
from tkinter import *
from math import *
# Custom number format code from wwii:
def convert(thing):
if isinstance(thing, str):
a,b = thing.split('+')
b,*d = b.split('.')
d = '00' if not d else d[0]
thing = round(float(f'{a}{b}.{d}'), 2)
elif isinstance(thing, (int,float)):
thing = str(round(thing, 2))
thing,*d = thing.split('.')
d = '00' if not d else d[0]
thing = thing if len(thing) > 2 else '0'+thing
thing = f'{thing[:-2]}+{thing[-2:]}.{d}'
return thing
def show_entry_fields():
try:
a, c, d, e = float(e1.get()), float(e3.get()), convert(str(e4.get())), convert(str(e5.get()))
b = e - d
s = (a + b + c) / 2
height = (sqrt (s * (s - a) * (s - b) * (s - c)) * 2) / b
height = float(format(height, '.3f'))
height_label['text'] = str(height)
side =((sqrt ((a ** 2) - (height ** 2))) + b)
side = convert(float(format(side, '.3f')))
side_label['text'] = str(side)
except ValueError:
pass
master.after(100, show_entry_fields)
master = Tk()
master.attributes("-topmost", True)
master.title("Triangulation Plotting")
Label(master, text="Measurement #1 Station Line Location").grid(row=1, column=0, sticky=W, pady=4)
e4 = Entry(master)
e4.grid(row=1, column=1, sticky=E)
Label(master, text="Triangulation Measurement #1").grid(row=2, column=0, sticky=W, pady=4)
e1 = Entry(master)
e1.grid(row=2, column=1, sticky=E)
Label(master, text="Measurement #2 Station Line Location").grid(row=3, column=0, sticky=W, pady=4)
e5 = Entry(master)
e5.grid(row=3, column=1, sticky=E)
Label(master, text="Triangulation Measurement #2").grid(row=7, column=0, sticky=W, pady=4)
e3 = Entry(master)
e3.grid(row=7, column=1, sticky=E, pady=4)
Label(master, text="Offset from station line").grid(row=8, column=0, sticky=W, pady=4)
height_label = Label(master, text="")
height_label.grid(row=8, column=1)
Label(master, text="Measurement on Station Line").grid(row=9, column=0, sticky=W, pady=4)
side_label = Label(master, text="")
side_label.grid(row=9, column=1)
master.after(100, show_entry_fields)
master.mainloop()