在Python 3.7中查找成对类型的列表(字符串的集合)的交集? [英] Finding the Intersection of the paired-typed Lists (collection of strings) in Python 3.7?

查看:121
本文介绍了在Python 3.7中查找成对类型的列表(字符串的集合)的交集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下成对类型的列表.我想计算这些列表的交集以生成only the common words not the numbers.

Suppose I am having the following paired-typed lists. I want to calculate the intersection of these lists to generate only the common words not the numbers.

列表是

l1 = [('state', 3537), ('t', 2320), ('system', 2086), ('transition', 1882), ('φ', 1703), ('path', 1423), ('ϕ', 1310), ('formula', 1273), ('property', 1194), ('π', 1165), ('α', 1065), ('ctl', 1048), ('action', 1034), ('ψ', 881), ('finite', 845), ('model', 828), ('algorithm', 790), ('process', 734), ('checking', 701), ('equivalence', 692), ('ltl', 663), ('trace', 654), ('automaton', 617), ('example', 610), ('bisimulation', 579), ('consider', 569), ('fragment', 552), ('σ', 545), ('hold', 527), ('variable', 519), ('stutter', 504), ('condition', 498), ('following', 495), ('act', 493), ('fairness', 491)]
l2 = [('state', 4123), ('model', 3541), ('system', 2619), ('checking', 2443), ('formula', 1812), ('program', 1706), ('automaton', 1694), ('verification', 1480), ('transition', 1459), ('k', 1403), ('property', 1326), ('logic', 1302), ('algorithm', 1291), ('variable', 1291), ('springer', 1217), ('σ', 1194), ('ϕ', 1177), ('ed', 1115), ('heidelberg', 1096), ('vol', 1087), ('analysis', 1076), ('example', 1053), ('abstraction', 1033), ('path', 980), ('lncs', 980), ('process', 966), ('language', 850), ('given', 845), ('α', 835), ('finite', 834), ('function', 824), ('problem', 809), ('theory', 777), ('value', 776), ('abstract', 743)]
l3 = [('φ', 2185), ('formula', 1056), ('ψ', 954), ('logic', 802), ('state', 716), ('model', 627), ('proof', 563), ('rule', 468), ('example', 465), ('function', 368), ('hold', 338), ('true', 332), ('case', 331), ('path', 323), ('predicate', 316), ('program', 313), ('variable', 309), ('value', 295), ('k', 284), ('boolean', 276), ('node', 263), ('system', 261), ('show', 253), ('propositional', 251), ('ctl', 223), ('given', 222), ('prove', 221), ('tree', 205), ('checking', 197), ('valid', 197), ('statement', 193), ('truth', 192), ('premise', 191), ('first', 190), ('number', 187)]
l4 = [('state', 2276), ('variable', 1535), ('process', 1192), ('input', 1141), ('value', 1071), ('output', 1016), ('system', 984), ('component', 834), ('task', 766), ('job', 734), ('mode', 689), ('formula', 601), ('model', 582), ('execution', 540), ('clock', 533), ('figure', 466), ('ϕ', 440), ('property', 430), ('transition', 422), ('given', 406), ('consider', 405), ('initial', 391), ('k', 391), ('event', 386), ('requirement', 364), ('channel', 356), ('deadline', 350), ('schedule', 330), ('timed', 328), ('instance', 321), ('example', 309), ('controller', 308), ('first', 305), ('region', 302), ('invariant', 298)]
l5 = [('state', 660), ('automaton', 584), ('property', 444), ('model', 372), ('system', 366), ('example', 280), ('transition', 263), ('variable', 232), ('checking', 183), ('logic', 180), ('formula', 172), ('temporal', 163), ('execution', 157), ('timed', 156), ('liveness', 149), ('ctl', 143), ('safety', 138), ('case', 135), ('ctr', 129), ('verification', 126), ('tool', 125), ('value', 114), ('reachability', 109), ('behavior', 99), ('method', 93), ('user', 89), ('fairness', 87), ('true', 86), ('clock', 83), ('note', 82), ('given', 79), ('number', 78), ('abstraction', 78), ('problem', 76), ('form', 76)]

我尝试使用&运算符,但该运算符未能以当前形式生成结果.请帮忙.

I have tried using the & operator which failed to generate the result in its present form. Please help.

print(l1 & l2 & l3 & l4 & l5)

推荐答案

您可以使用

You can use set.intersection and feed an unpacked generator expression:

from operator import itemgetter

res = set.intersection(*(set(map(itemgetter(0), i)) for i in [L1, L2, L3, L4, L5]))

print(res)

{'state', 'model', 'system', 'example', 'formula', 'variable'}

这篇关于在Python 3.7中查找成对类型的列表(字符串的集合)的交集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆