Python 碰到一题,大家有没有更好的解法
source link: https://www.v2ex.com/t/791154
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
nput_data = [
['格力', '格力电器'],
['格力', '美的', '格力电器', '美的集团'],
['中国民生投资股份有限公司', '中民投'],
['格力', '格力电器', '格力集团'],
['工银', '中信证券', '工行', '中国工商银行'],
['建行', '建设银行', '北大光华'],
['中国银行', '工行', '工商银行', '港交所'],
['中行', '建行', '工行', '中金', '工商银行', '中国银行', '建设银行'],
['农行', '建设银行', '建行', '中行'],
['中国民生投资股份有限公司', '上海金融法院', '中民投', '启信宝']
]
输出:
{('格力', '格力电器'): 3,
('美的', '美的集团'): 1,
('中民投', '中国民生投资股份有限公司'): 2,
('格力', '格力集团'): 1,
('工银', '中国工商银行'): 1,
('建行', '建设银行'): 3,
('中行', '中国银行'): 2,
('工银', '工商银行'): 2}
from itertools import chain
from collections import defaultdict
data = list(chain(*nput_data))
res = [set(i) for i in data]
count = defaultdict(int)
for index,row in enumerate(res):
for index2,i in enumerate(res):
if row.issuperset(i) and row != i:
count[(data[index2],data[index])] += 1
break
第 1 条附言 · 1 天前
原题就是
nput_data = [xx] ,
结果是:
('格力', '格力电器'): 3,
('格力', '格力集团'): 1, 等,请补全结果,
我也是嗯做,然后说是 key 全包含另一个 key 作为一个分类,
统计所有分类数量
key1 全包含 key2 就算一类,例如 '中国民生投资股份有限公司' 包含 '中民投'
Recommend
-
105
程序员 - @kiyo - 今天面试碰到一个纯英文简历(12 年开发经验)的,简历上写着精通 40 多种语言。。。几乎涵盖了市面上能看到的所有语言。。。 然后问他主流框架,答曰看 API 文档。问他原理,只会用不知道
-
42
程序员 - @justou - 因为要离开现公司, 两个月前招到一个来接替我的小伙子, 此子只用过 C, 做了半年嵌入式, 我对他进行了两个月的 C++培训, 带着他看 C++ primer 这本书(这本书既厚又杂,想着有我帮着梳
-
16
今天是 LeetCode题解系列第21篇 ,今天来看一道人狠话不多的题目。 题面 题目非常简单,只有一句话,给定一个整数数组,要求返回最小的不在数组当中的正整数。 看起来有...
-
13
本文始发于个人公众号: TechFlow ,原创不易,求个关注 今天是 LeetCode系列第22篇 文章,今天讲的内容是高精度算法。 今天和大家讨论的算法是高精度,对应的LeetCo...
-
28
题目信息 时间: 2019-07-04 题目链接: Leetcode ta...
-
12
为了让大家更好地学习python爬虫,我们做了一个“靶子”上海交通大学 计算机应用技术硕士1很多学习 python 的同学应该都听说过“网络爬虫”的概念,也可能听说过...
-
7
基本算法问题的 Python 解法——图(Graph)问题 发表于 2021-02-20 ...
-
7
基本算法问题的 Python 解法——约束满足问题(CSP) 发表于 2021-02-03 ...
-
4
OSC 可能碰到难关了,大家一起给 OSC 加油 开源问答 ...
-
8
抓取Leetcode的每日一题信息 思路一(发送GraphQL Query获取数据) 参考文章:https://www.cnblogs.com/ZhaoxiCheung/p/9333476.html 主要的数据存在于graphql/接口中: https://leetcode-cn.com/graphql/ 首页...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK