Python Exercise 2 --List Comprehension

Python can be written like this! Example.

Let's output the common element (AND), unique element (XOR), and union (OR) of set 1 and set 2 using the list comprehension notation.

set.py


#!/usr/bin/env python
#-*- coding:utf-8 *-*

set1 = [ u'craziness', u'Loud', u'Naked' ]
set2 = [ u'Naked', u'Loud', u'Void' ]
print 'SET1 = ' + ', '.join(set1)
print 'SET2 = ' + ', '.join(set2)
print ''

set_and = [ c for c in set1 if c in set2 ]
print 'AND  = ' + ', '.join(set_and)

set_xor = []
set_xor.extend([ c for c in set1 if c not in set2 ])
set_xor.extend([ c for c in set2 if c not in set1 ])
print 'XOR  = ' + ', '.join(set_xor)

set_or = []
set_or.extend(set_and)
set_or.extend(set_xor)
print 'OR   = ' + ', '.join(set_or)

exit(None)

Execution result

SET1 =craziness,Loud,Naked
SET2 =Naked,Loud,Void

AND  =Loud,Naked
XOR  =craziness,Void
OR   =Loud,Naked,craziness,Void

Recommended Posts

Python Exercise 2 --List Comprehension
Python> Comprehension / Comprehension> List comprehension
Python list comprehension speed
Python comprehension
List comprehension
Python comprehension
List comprehension
[Python] list
About python comprehension
Python basics: list
Note: List comprehension
Python list manipulation
Python basic operation 1st: List comprehension notation
Python comprehension (list and generator expressions) [additional]
[Python] List Comprehension Various ways to create a list
List of python modules
Python> list> extend () or + =
FizzBuzz in list comprehension
Python Exercise 1-Breadth-first search
[Introduction to Udemy Python3 + Application] 60. List comprehension notation
Filter List in Python
python unittest assertXXX list
Python3 List / dictionary memo
[Memo] Python3 list sort
OpenCV3 Python API list
Python error list (Japanese)
Not just list comprehension
List find in Python
Python exception class list
Quicksort 2 | Easy list comprehension
Initialize list with python
list comprehension because operator.methodcaller cannot be used in python 2.5
Python3 comprehension (List, dictionary) that I have seen somewhere
Python hand play (two-dimensional list)
Python list, for statement, dictionary
Summary of Python3 list operations
[Python] Convert list to Pandas [Pandas]
[Python] How to use list 1
Create ToDo List [Python Django]
Specify multiple list indexes (Python)
Python Basic Course (5 List Tuples)
Python list is not a list
[Python] Copy of multidimensional list
[Introduction to Python] <list> [edit: 2020/02/22]
Python list and tuples and commas
Paiza Python Primer 4: List Basics
Python list comprehensions and generators
[Python / PyQ] 4. list, for statement
Python #list for super beginners
Getting list elements in Python
[Road to intermediate Python] Use if statement in list comprehension
Extract multiple list duplicates in Python
Difference between list () and [] in Python
[python] Manage functions in a list
Output 2017 Premium Friday list in Python
Python
Python Exercise for Beginners # 2 [for Statement / While Statement]
Judgment of if by list comprehension
Convert list to DataFrame with python
[Python beginner] Divide one list (5 lines).
python / Make a dict from a list.