Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@suganoo
Last active September 7, 2017 06:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suganoo/2fa6fb93d746b092423c951c49a94742 to your computer and use it in GitHub Desktop.
Save suganoo/2fa6fb93d746b092423c951c49a94742 to your computer and use it in GitHub Desktop.
setでlistの差分を取る方法
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)
set(['a', 'd'])
['a', 'd']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment