ã¯ããã«
ãã¡ãã®è¨äºãæè¦ããç´¹ä»ããã¦ãã Python ã®ã³ã¼ãã®å¥è§£ãèãã¦ã¿ã¾ãã
- åä½ç¢ºèªç°å¢
- Python 3.6.7, 3.7.5
ãã£ã¦ãããã¨
èæ¯ã¯ããã§ã¯å²æãã¾ãããdocker ã¤ã¡ã¼ã¸åããªã¹ãåããã2ã¤ã®ãã¡ã¤ã«
checklist.txt
imagelist.txt
ããããchecklist.txt
ã«ãã£ã¦ãimagelist.txt
ã«ãªãã¤ã¡ã¼ã¸åãç»é¢ã«è¡¨ç¤ºãããã®ã§ãã
ãã¨ãã°ã2ã¤ã®ãã¡ã¤ã«ããããã
checklist.txt
image1 image2 image3 image4 image5
imagelist.txt
image1 image3 image5
ã¨ããç¶æ ã§ããã°ã
image2 image4
ã¨åºåãã¾ãã
å ã®ã³ã¼ã
#checkimage.py checklist = open("checklist.txt","r") imagelist = open("imagelist.txt", "r") checklists = [] imagelists = [] for line1 in checklist: checklists.append(line1) for line2 in imagelist: imagelists.append(line2) for check in checklists: if check not in imagelists: print(check.rstrip('\n')) checklist.close() imagelist.close()
ããããé åã«å ¥ããå¿ è¦ã¯ãªãã®ããããã¾ããããããã¯ç¥èã¨æéä¸è¶³ã§ãããã ãããã£ã¨ã¹ãã¼ããªæ¸ãæ¹ãç¥ã£ã¦ããæ¹ã¯æãã¦ãã ããðââï¸
ã¨ã®ãã¨ã§ãã®ã§ãç§ãªãã«4ãã¿ã¼ã³ã»ã©èãããã調ã¹ãããã¾ããã
â ãã¿ã¼ã³1: ãã¡ã¤ã«èªã¿è¾¼ã¿ã with
ã§å²ã
ãã¡ã¤ã«ãæé»çããã¤ç¢ºå®ã« close
ãããããã« open
ã with
ã§å²ããã¿ã¼ã³ã§ãã
ä»ã¯ãå
ã®ã³ã¼ãã¨åãã§ãã
checklists = [] imagelists = [] with open("checklist.txt", "r") as f: for line1 in f: checklists.append(line1) with open("imagelist.txt", "r") as f: for line2 in f: imagelists.append(line2) for check in checklists: if check not in imagelists: print(check.rstrip('\n'))
â ãã¿ã¼ã³2: ãªã¹ãåã« splitlines()
ãå©ç¨ãã
ãã¿ã¼ã³1ããã¼ã¹ã«ãã¦ããã¡ã¤ã«å
容ã®ãªã¹ãåã« splitlines()
ãå©ç¨ãããã¿ã¼ã³ã§ãã
åãåããªã¹ãã®åæå㨠append
ãä¸è¦ã«ãªãã¾ããã¾ããæ¹è¡åºåã㧠split ãããã®ã§ã表示æã® .rstrip('\n')
ãä¸è¦ã«ãªãã¯ãã
with open("checklist.txt", "r") as f: checklists = f.read().splitlines() with open("imagelist.txt", "r") as f: imagelists = f.read().splitlines() for check in checklists: if check not in imagelists: print(check)
â ãã¿ã¼ã³3: å·®åã -
ã§æ±ãã
ãã¿ã¼ã³2ããã¼ã¹ã«ãã¦ãå·®åã -
ã§æ±ãããã¿ã¼ã³ã§ãã
æ¯è¼ããéã®ã«ã¼ããä¸è¦ã«ãªãã¾ãããã ããã»ããã«å¤æããã®ã§é çªã¯ç¶æããã¾ããã
with open("checklist.txt", "r") as f: checklists = f.read().splitlines() with open("imagelist.txt", "r") as f: imagelists = f.read().splitlines() diff = list(set(checklists) - set(imagelists)) for check in diff: print(check)
â ãã¿ã¼ã³4: å·®åã filter
ã§æ±ãã
ãã¿ã¼ã³3ã§ãã¼ã¹ã«ãã¦ãé çªãç¶æããããæ¹ããªããã¨èª¿ã¹ãã¨ããã以ä¸ã®ãã¼ã¸ãåèã«ãªãã¾ããã
lcjvEm - Online Python3 Interpreter & Debugging Tool - Ideone.com
ï¼ã³ã¡ã³ãã«ãéè¤ãä¿æããããã¨ããã¾ãããé çªããã®ãã¨ã ã¨æãã¾ãï¼
å·®åã filter
ã§æ±ãããã¿ã¼ã³ã§ãã
with open("checklist.txt", "r") as f: checklists = f.read().splitlines() with open("imagelist.txt", "r") as f: imagelists = f.read().splitlines() diff = list(filter(lambda x: x not in imagelists, checklists)) for check in diff: print(check)
ãããã«
表示ã®ä»æ¹ãå«ããã¨ãã£ã¨ãããããªæ¹æ³ãããã¨æãã¾ãã