Last active
September 7, 2017 06:20
-
-
Save suganoo/2fa6fb93d746b092423c951c49a94742 to your computer and use it in GitHub Desktop.
setでlistの差分を取る方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
listA = ["d","a","b","c"] | |
listB = ["e","b","c"] | |
def extract_not_exist(list_a, list_b): | |
""" | |
# forでなんとか抽出する方法 | |
not_exist_items = [] | |
for item in list_a: | |
if not (item in list_b): | |
not_exist_items.append(item) | |
return sorted(not_exist_items) | |
""" | |
# setで抽出する方法 | |
return list(set(list_a) - set(list_b)) | |
print set(listA) - set(listB) | |
print extract_not_exist(listA,listB) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set(['a', 'd']) | |
['a', 'd'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment