我的目标是检测 2 个字符串是否相同但顺序不同。
Example
"hello world my name is foobar" is the same as "my name is foobar world hello"
Run Code Online (Sandbox Code Playgroud)
我已经尝试过将两个字符串拆分为列表并在循环中进行比较。
text = "hello world my name is foobar"
textSplit = text.split()
pattern = "foobar is my name world hello"
pattern = pattern.split()
count = 0
for substring in pattern:
if substring in textSplit:
count += 1
if (count == len(pattern)):
print ("same string detected")
Run Code Online (Sandbox Code Playgroud)
它返回了我的意图,但这实际上是正确和有效的方式吗?也许还有另一种方法。任何有关该主题的期刊建议都非常好。
编辑 1:重复的单词很重要
text = "fish the fish the fish fish fish"
pattern = "the …Run Code Online (Sandbox Code Playgroud) 我已经初始化了数据。
data () {
return {
current_product: {},
current_ID: '',
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我从生命周期创建挂钩上的 REST API 获取数据。
created () {
var skuID = this.$store.state.selected_productSKU.productSKU_ID
axios.get(`http://localhost:8081/api/products/${skuID}`)
.then(response => {
this.current_ID = response.data.product_ID
this.current_product = response.data
})
.catch(e => {
alert(e)
})
}
Run Code Online (Sandbox Code Playgroud)
最后,我使用计算属性来获取一些值
// THIS JUST RETURN ['XL', 'M']
focusedProduct_SKUS_NoDupSizes () {
var newArr = this.current_product.product_SKU.filter((sku, index, self) =>
index === self.findIndex(t => (
t.productSKU_size === sku.productSKU_size
))
)
var x = newArr.map(a => a.productSKU_size)
return x
}
Run Code Online (Sandbox Code Playgroud)
vue 实例显示预期结果 …