Python 學習

  1. 打印出在文件test1中存在而 test2中不存在的用戶
BJ$ cat test1
shi
zhen
ning
zhang
san
li
si
yan
jun
BJ$ cat test2
zhang
san
li
si
yan
jun

BJ$ cat diff.py 
#!/usr/bin/python

with open('/Users/momo/test1','r') as x:
  list1 = x.readlines()
with open('/Users/momo/test2','r') as y:
  list2 = y.readlines()
  for i in list1:
      if i  not in list2:
         print i

執行:


BJ$ python diff.py 
shi

zhen

ning

shell:

BJ$ cat 1.sh 
#!/bin/bash
for i in `cat test1`
do
  temp=`grep $i test2`
  if [ -z $temp ];then
      echo $i   
  fi 
done
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章