码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 分组后成员作为集合两两运算


    【问题】

    Suppose I have a MySQL database in 1NF with the following tables:

    1. sandwich\_id | sandwich\_name | sandwich_price
    2. \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
    3. 0| BLT | 5.5
    4. 1| Reuben | 7.0
    5. 3| Grilled Cheese | 3.75
    6. ...

    and a separate table that stores all the ingredient values:

    1. sandwich_id | ingredient
    2. \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
    3. 0| bacon
    4. 0| lettuce
    5. 0| tomato
    6. 1| corned beef
    7. 1| swiss cheese
    8. ...

    How can I compare all the sandwiches by their ingredients to determine which are the most similar?

    (Also, is there a technical term for that second table that I’m missing? I want to call it a map table, but I know that’s not quite right, since a map table stores foreign keys for two tables and this one’s more of an offshoot of the first…)

    【回答】

    把不同 sandwich 的 ingredient 集合两两做交集,计算交集成员数量再排序即可。但 SQL 没有显式集合,代码很难懂。这种数据量不大时用 SPL 更方便:

    A
    1$select i.sandwich_id sandwich_id, i.ingredient ingredient, s.sandwich_name sandwich_namefrom ingredientTable i,sandwichTable s where i.sandwich_id=s.sandwich_id
    2=A1.group(sandwich_id;sandwich_name,~.(ingredient):collection)
    3=xjoin(A2;A2).select(_1.sandwich_id<_2.sandwich_id)
    4=A3.new((_1.collection ^ _2.collection).len():sameCount,_1.sandwich_name,_2.sandwich_name)
    5=A4.sort(-sameCount)

    A2: 将 A1 按 sandwich_id 分组。collection 是每种 sandiwich 的 ingredient 集合。

    A3: 将 sandwich 两两分为一组。比如 0,1,3 可分为三组:[0,1],[0,3],[1,3]

    A4: 求各组交集的成员数量。“^”表示求交集。

    A5: 将 A4 的结果集降序排序

    关于显式集合请参考【集算器的集合思维】。更多例子可参考【集算器的集合运算举例】

     

  • 相关阅读:
    【论文阅读】 Cola-Dif; An explainable task-specific synthesis network
    JavaScript设计模式——命令模式
    轮播图的实现
    持续交付到底有什么价值?
    MATLAB环境下使用二维高分辨时频分析方法提取波状分量
    docker启动已存在容器
    Git:下载和安装
    一文详解窄脉冲LIV测试系统的特点和功能
    Vue借助浏览器实现代码断点调试
    LeetCode58——最后一个单词的长度
  • 原文地址:https://blog.csdn.net/raqsoft/article/details/127645700
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号