1. 程式人生 > >python之列表集合加減與或的計算

python之列表集合加減與或的計算

## set_calc.py

import sys
import os
import re

sList = []
dList = []

def getLinesOfFile(fname):
    fp = open(fname,"r")
    cnt = fp.read()
    return re.split(r'\s',cnt)



def SubList(aList,bList):# in a ,not in b
    tmp = []
    for a in aList:
        if a not in bList:
            tmp.append(a)
    return tmp

def AndList(aList,bList):# in a and in b
    tmp = []
    for a in aList:
        if a in bList:
            tmp.append(a)
    return tmp

def OrList(aList,bList):# in a or in b
    tmp = OnceForList(aList)
    bList = OnceForList(bList)
    for a in bList:
        if a not in tmp:
            tmp.append(a)
    return tmp
    
def OnceForList(aList):# ele not repeat in list
    tmp = []
    for a in aList:
        if a not in tmp:
            tmp.append(a)
    return tmp


def showList(list):
    print "len = ",len(list)
    print "=============================="
    for it in list:
        print "item = ",it



sList = getLinesOfFile(sys.argv[1])
dList = getLinesOfFile(sys.argv[2])

showList(SubList(dList,sList))
showList(AndList(dList,sList))

@@用法:

src.txtAA BB CC

des.txt CC DD EE

calc.py src.txt des.txt

相關推薦

python列表集合計算

## set_calc.py import sys import os import re sList = [] dList = [] def getLinesOfFile(fname): fp = open(fname,"r") cnt = fp.re

Pythonset集合collections系列

update common ren date 原理 symmetric pda () http 1》set集合:是一個無序且不重復的元素集合;訪問速度快,解決了重復的問題;   s2 = set(["che","liu","haha"])  

python 列表字典

ppi 實現 for 對象的引用 false key default abc sid 1.4 列表與字典 列表與字典,這兩種類型,都是各種類型的集合,以列表為例,如果列表中包含列表,就形成嵌套。 這兩種類型幾乎是所有python腳本的主要工作組件 。 這種結構信息是可變

python 列表和元組

ott ase dig pdb awb data whl w3g htc list Python內置的一種數據類型是列表:list。list是一種有序的集合,可以隨時添加和刪除其中的元素。 比如,列出班裏所有同學的名字,就可以用一個list表示: >>

python列表生成式

list if判斷 實現 css == tor xcod 例如 列表 列表生成式即List Comprehensions,是Python內置的非常簡單卻強大的可以用來創建list的生成式。 舉個例子,要生成list [1, 2, 3, 4, 5, 6, 7, 8, 9, 1

Python列表

python 列表 duyuheng 列表概念:序列是Python中最基本的數據結構。序列中的每個元素都分配一個數字 - 它的位置,或索引,第一個索引是0,第二個索引是1,依此類推。序列都可以進行的操作包括索引,切片,加,乘,檢查成員。此外,Python已經內置確定序列的長度以及確定最大和最小的元

PythonNumPy(axis=0 axis=1)區分

tail ner def ros som 分享 然而 article 本質 轉自:http://blog.csdn.net/wangying19911991/article/details/73928172 https://www.zhihu.com/

python路_day85_blog 點贊評論

nbsp body 回滾 一個 操作 銀行 賬戶 事務回滾 color 一、事務(transaction)   舉個簡單的例子,在銀行轉賬過程中,我們必須要保證一賬戶余額減少,另一賬戶余額相應增加。但絕對不允許一方余額減少,而另一方賬戶余額沒有相應增加,為了解決這種事情發生

what' the python遞歸函數二分算法

hat 簡單的 not post col 組成 成了 n) spa what‘s the 遞歸?   遞歸函數的定義:在函數裏可以再調用函數,如果這個調用的函數是函數本身,那麽就形成了一個遞歸函數。   遞歸的最大深度為997,這個是程序強制定義的,997完全可以滿足一般情

python路——集合and 深淺copy

amp 女神 div 差集 del add body 並集 不可變類 補充: id 查詢內存地址 print(id(name)) is判斷的是內存地址 int str 小數據池 int: -5 — 256 str: 如果字符串都是由字母組成,都是指向一個內存地址。   

python列表操作

括號 刪除 元素 非負數 不一致 str mis 長度 添加 1、創建列表 1)把逗號分隔的不同的數據項使用方括號括起來,列表中的元素的數據類型可以不一致。列表索引從0開始。列表可以進行截取、組合等。 1 list1 = [‘physics‘, ‘chemistry

python列表(list)

list python 列表 #列表 names = ["Zhangwuji","Zhaomin","Yangguo","Xiaolongnv","Guojing","Huangrong"] # print(names) # print(type(names)) #切片 # print(na

python函數嵌套閉包

geb login clas 所有 home 功能 嵌套 模擬 倒數 一:函數的嵌套:在函數內部在定義一個函數,一層套一層 def father(name): print("from father %s" %name) def son():

編程開發--Java集合類繼承實現必備知識

編程開發 next() int end long dha cos dHash IV 1、LinkedHashSet有序鏈式集合 舉例: long startTime=System.currentTimeMillis(); LinkedHashSet oprTypeSe

python列表/元組/字典/字符串

blue spa climb del 直接 else copy keys lse 一、列表 格式:list = [‘xxx‘,‘xxx‘,‘xxx‘] 性質:可以修改列表內容 copy用法: import copy names = [‘Yangdanhua‘,[‘01‘,

Python列表和元組

出現 函數 lse abc remove 不可 unicode end 分片 Python 6種內建序列類型:列表、元組、字符串、Unicode字符串、buffer對象、xrange對象。 1、列表與元組的區別,列表可以修改,元組則不能。 2、通用序列操作:a、索引(從0

Python路-Day07區域性變數全域性變數,遞迴函式

區域性變數和全域性變數的含義 在子程式中定義的變數稱為區域性變數,在程式的一開始定義的變數稱為全域性變數. 全域性變數作用域是整個程式,區域性變數作用域是定義該變數的子程式. 當全域性變數於區域性變數同名時: 在定義區域性變數的子程式內,區域性變數起作用,在其它地方全域性變數起作用.

pythonpandas的層級索引資料重構

import numpy as np import pandas as pd #層級索引 s1 = pd.Series(np.random.randint(-5,10,12),index=[list('aaabbbcccddd'),[1,2,4,1,2,3,1,2,3,1,2,3]]) p

python列表,元組,字典。

在博主學習列表,元組以及字典的時候,經常搞混這三者。因為他們都是用括號表示的。分別是[],(),{}. 列表(list): [1,'abc',1.26,[1,2,3],(1,2,3),{'age:18'}] 列表中的元素可以是整型,浮點型,字串,也可以是元組,列表,字典。 列表中的元素是有序的,而且元

Redis---API有序集合的理解使用

一種比較新的資料結構, 結構:可以直接排序,按序取結果,通過分值實現有序。       集合 VS 有序集合 列表 VS 有序集合 重要API,有序集合的操作以Z為開頭的命令。 這裡score可以重複,elem