@ Introducing Python: Modern Computing in Simple Packages by Bill Lubanovic
(No. 1792 / 12833)
'&'や.intersection()
などが登場。
上陸班(away team)で試してみた。
http://ideone.com/zxu8YP
teams = {
'away_team_1' : { '7of9', 'Janeway', 'Tuvok' },
'away_team_2' : { '7of9', "B'Elanna Torres", 'Kim' },
'away_team_3' : { '7of9', 'Janeway', 'Jean-Luc Picard' }
}
for name, contents in teams.items():
print("%s %s" % (name, contents))
team1 = teams['away_team_1']
team2 = teams['away_team_2']
print(team1)
print(team2)
print("::and::")
print(team1 & team2)
print(team1.intersection(team2))
print("::or::")
print(team1 | team2)
print(team1.union(team2))
print("::difference::")
print(team1 - team2)
print(team1.difference(team2))
結果
Success time: 0 memory: 23304 signal:0
away_team_1 set(['Tuvok', '7of9', 'Janeway'])
away_team_2 set(["B'Elanna Torres", '7of9', 'Kim'])
away_team_3 set(['Jean-Luc Picard', '7of9', 'Janeway'])
set(['Tuvok', '7of9', 'Janeway'])
set(["B'Elanna Torres", '7of9', 'Kim'])
::and::
set(['7of9'])
set(['7of9'])
::or::
set(["B'Elanna Torres", 'Tuvok', '7of9', 'Janeway', 'Kim'])
set(["B'Elanna Torres", 'Tuvok', '7of9', 'Janeway', 'Kim'])
::difference::
set(['Janeway', 'Tuvok'])
set(['Janeway', 'Tuvok'])
教えていただいた事項
@shiracamus さんのコメントにてsetの演算のリンクを教えていただきました。
情報感謝です。