list和set

【Python系列5】set和list的妙用

    <div class="article_manage clearfix">
    <div class="article_r">
        <span class="link_postdate">2015-11-29 21:50</span>
        <span title="阅读次数" class="link_view">5263人阅读</span>
        <span title="评论次数" class="link_comments"> <a onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_pinglun'])" href="#comments">评论</a>(0)</span>
        <span class="link_collect tracking-ad" data-mod="popu_171"> <a title="收藏" onclick="javascript:collectArticle('%e3%80%90Python%e7%b3%bb%e5%88%975%e3%80%91set%e5%92%8clist%e7%9a%84%e5%a6%99%e7%94%a8','50099657');return false;" href="javascript:void(0);" target="_blank">收藏</a></span>
         <span class="link_report"> <a title="举报" onclick="javascript:report(50099657,2);return false;" href="#report">举报</a></span>

    </div>
</div>    <style type="text/css">        
        .embody{
            padding:10px 10px 10px;
            margin:0 -20px;
            border-bottom:solid 1px #ededed;                
        }
        .embody_b{
            margin:0 ;
            padding:10px 0;
        }
        .embody .embody_t,.embody .embody_c{
            display: inline-block;
            margin-right:10px;
        }
        .embody_t{
            font-size: 12px;
            color:#999;
        }
        .embody_c{
            font-size: 12px;
        }
        .embody_c img,.embody_c em{
            display: inline-block;
            vertical-align: middle;               
        }
         .embody_c img{               
            width:30px;
            height:30px;
        }
        .embody_c em{
            margin: 0 20px 0 10px;
            color:#333;
            font-style: normal;
        }
</style>
<script type="text/javascript">
    $(function () {
        try
        {
            var lib = eval("("+$("#lib").attr("value")+")");
            var html = "";
            if (lib.err == 0) {
                $.each(lib.data, function (i) {
                    var obj = lib.data[i];
                    //html += '<img src="' + obj.logo + '"/>' + obj.name + "&nbsp;&nbsp;";
                    html += ' <a href="' + obj.url + '" target="_blank">';
                    html += ' <img src="' + obj.logo + '">';
                    html += ' <em><b>' + obj.name + '</b></em>';
                    html += ' </a>';
                });
                if (html != "") {
                    setTimeout(function () {
                        $("#lib").html(html);                      
                        $("#embody").show();
                    }, 100);
                }
            }      
        } catch (err)
        { }

    });
</script>
  <div class="category clearfix">
    <div class="category_l">
       <img src="http://static.blog.csdn.net/images/category_icon.jpg">
        <span>分类:</span>
    </div>
    <div class="category_r">
                <label onclick="GetCategoryArticles('5776633','zongzhiyuan','top','50099657');">
                    <span onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_fenlei']);">Python<em>(7)</em></span>
                  <img class="arrow-down" style="display:inline;" src="http://static.blog.csdn.net/images/arrow_triangle _down.jpg">
                  <img class="arrow-up" style="display:none;" src="http://static.blog.csdn.net/images/arrow_triangle_up.jpg">
                    <div class="subItem">
                        <div class="subItem_t"><a href="http://blog.csdn.net/zongzhiyuan/article/category/5776633" target="_blank">作者同类文章</a><i class="J_close">X</i></div>
                        <ul class="subItem_l" id="top_5776633">                            
                        </ul>
                    </div>
                </label>                    
    </div>
</div>
    <div class="bog_copyright">         
        <p class="copyright_p">版权声明:本文为博主原创文章,未经博主允许不得转载。</p>
    </div>

set和list是Python常用的结构类型,这里不再多述。本文主要是总结了一些它们配合起来的一些妙用。

(1)去重

比如一个序列:

[python] view plain copy
print ?
  1. >>>line = [‘a’,‘b’,‘a’]  
>>>line = ['a','b','a']

为了去除重复的’a’,可以进行如下操作:

[python] view plain copy
print ?
  1. >>> list(set(line))  
  2. [’a’‘b’]  
>>> list(set(line))
['a', 'b']

(2)提取两个序列中出现过的非重复元素

比如两个序列:

[python] view plain copy
print ?
  1. >>> line1=[‘a’,‘b’,‘a’]  
  2. >>> line2=[’a’,‘c’]  
>>> line1=['a','b','a']
>>> line2=['a','c']

为了得到这两个序列中出现的不同元素,可以进行如下操作:

[python] view plain copy
print ?
  1. >>> line=line1+line2  
  2. >>> list(set(line))  
  3. [’a’‘c’‘b’]  
>>> line=line1+line2
>>> list(set(line))
['a', 'c', 'b']


(3)两个集合的并集

比如两个集合为:

[python] view plain copy
print ?
  1. >>> set1=set([‘a’,‘b’])  
  2. >>> set2=set([’a’,‘c’])  
>>> set1=set(['a','b'])
>>> set2=set(['a','c'])

为了得到并集,可进行如下操作:

[python] view plain copy
print ?
  1. >>> set(list(set1)+list(set2))  
  2. set([’a’‘c’‘b’])  
>>> set(list(set1)+list(set2))
set(['a', 'c', 'b'])

(4)计算两个集合的雅可比相似度

雅可比相似度=两个集合交集/两个集合并集。

首先,通过(3)中的方法,可以得到两个集合的并集,进而知道并集中的元素个数是3;

其次,根据set1中的元素个数为2,set2中的元素个数为2,得到两个集合相同元素的个数为2+2-3=1(验证确实是1,只有’a’是共同元素)

最后,雅可比相似度=1/3。


(5)判断一个集合是否包含在另一个集合中

基于(4),可以求出两个集合的共同元素的个数c。此时,如果c等于一个集合的元素总个数,则该集合必定包含在另一个集合中;否则,不完全包含。




document.getElementById("bdshell_js").src = "http://bdimg.share.baidu.com/static/js/shell_v2.js?cdnversion=" + Math.ceil(new Date()/3600000)
    <div id="digg" articleid="50099657">
        <dl class="digg digg_enable" id="btnDigg" onclick="btndigga();">

             <dt>顶</dt>
            <dd>1</dd>
        </dl>


        <dl class="digg digg_enable" id="btnBury" onclick="btnburya();">

              <dt>踩</dt>
            <dd>0</dd>               
        </dl>

    </div>
 <div class="tracking-ad" data-mod="popu_222"><a href="javascript:void(0);" target="_blank">&nbsp;</a>   </div>
<div class="tracking-ad" data-mod="popu_223"> <a href="javascript:void(0);" target="_blank">&nbsp;</a></div>
<script type="text/javascript">
    function btndigga() {
        $(".tracking-ad[data-mod='popu_222'] a").click();
    }
    function btnburya() {
        $(".tracking-ad[data-mod='popu_223'] a").click();
    }
        </script>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值