입력 및 출력 형식을 수정하지 않고 정보를 그룹화하여 그룹화 한 루프가 발생하지 않도록하고 해당 정보를 플레이어별로 추출하려고합니다. 당신이를 교환 어디
from collections import Counter
if __name__=='__main__':
x = {1: [{1, 2, 3}], 2: [{1, 2, 3}, {2, 3, 4}], 3: [{1, 2, 3}, {2, 3, 4}, {3, 4, 5}], 4: [{2, 3, 4}, {3, 4, 5}], 5: [{3, 4, 5}]}
d = {player: Counter([p for match in matches for p in match]) for player, matches in x.items()}
print(d)
: 입력하면 같은 뭔가를 갈 수 수정할 수 있습니다 경우
from collections import defaultdict, Counter
if __name__=='__main__':
x = {'match1': [1, 2, 3], 'match2': [2, 3, 4], 'match3': [3, 4, 5]}
d = defaultdict(Counter)
for key, value in x.items():
for i in value:
d[i].update(value)
print(d)
: 당신이 할 실제로 수있는 것은 중첩 루프 내부의 Counter
을 작성하여 마지막 루프를 피할 수있다 더 효율적이어야하는 dict 및 list comprehension을위한 중첩 된 루프. 아마도 선수와 경기는 int
과 list
s가 int
입니다. 그래서 조금 더 붉게 할 수 있습니다. 예를 들어 :
는
from collections import defaultdict, Counter
def printMatrix(matrix):
print(' '.join([' |'] + list(map(str, matrix.keys()))))
print('---+-' + '-'*len(matrix)*2)
for row, values in matrix.items():
fmt = ' {row} |'
for col in matrix.keys():
fmt += ' {values[' + str(col) + ']}'
print(fmt.format(row=row, values=values))
class Entity:
def __init__(self):
self._id = None
@classmethod
def register(cls, value):
if value in cls.ids:
raise ValueError("The provided ID is already in use")
cls.ids.add(value)
@classmethod
def unregister(cls, value):
if value is not None:
cls.ids.remove(value)
@property
def id(self):
return self._id
@id.setter
def id(self, value):
if value == self.id:
return
self.register(value)
self.unregister(self.id)
self._id = value
class Player(Entity):
ids = set()
def __init__(self, pid):
super().__init__()
self.id = pid
self.__matches = set()
def __repr__(self):
return 'Player<{}>'.format(self.id)
@property
def matches(self):
return set(self.__matches)
def inscribe(self, match):
if match not in self.__matches:
self.__matches.add(match)
def delist(self, match):
self.__matches.remove(match)
class Match(Entity):
ids = set()
def __init__(self, mid, players):
super().__init__()
self.id = mid
self.__players = set()
self.players = players
for player in players:
player.inscribe(self)
def __repr__(self):
return 'Match<{}>'.format(self.id)
@property
def players(self):
return set(self.__players)
@players.setter
def players(self, value):
for player in self.__players:
player.delist(self)
self.__players = set(value)
for player in self.__players:
player.inscribe(self)
if __name__=='__main__':
players = [Player(i) for i in range(1, 6)]
matches = [Match(i, {players[i-1], players[i], players[i+1]}) for i in range(1, 4)]
for player in players:
print(player, player.matches)
for match in matches:
print(match, match.players)
d = {player.id: Counter([p.id for match in player.matches for p in match.players]) for player in players}
printMatrix(d)
printMatrix()
기능은 내가 화면에 출력을 꽤 - 인쇄 제작 단지 도우미입니다.
Entity
클래스는 Player
및 Match
클래스 둘 다에 고유 한 ID가 있으므로 두 코드에 필요한 중복 코드를 방지합니다. 생성자는 빈 _id
특성을 만듭니다. register()
및 unregister()
메서드는 클래스 속성 ids
에서 ID를 추가하고 제거하는 작업을 처리합니다. 또한 getter 및 setter로 id
속성을 선언합니다. 어린이 클래스는 생성자에서 super().__init__()
을 호출하고 Player
및 Match
과 같이 ID 고유성을 적용하려는 수준에서 ids
클래스 속성을 만들면됩니다.
클래스에는 matches
인스턴스 추가 읽기 전용 속성이 있으며 각각 inscribe()
및 delist()
메서드로 채워져 있습니다. Match
클래스에는 getter 및 setter 메서드가있는 players
속성이 있습니다.
첫째는 players
및 matches
(ID 1과 Player
가 players[0]
오도록리스트 위치 0에서 시작 기억) 두 지능형리스트로 만들어 대응 관계와 함께 인쇄된다 (그들은 플레이어와 플레이어가 플레이 일치하는지 성냥을 위해 참가하십시오).
두 유형 모두 다른 유형에 대한 참조를 유지하므로 players
에서 요청한 dict
이 Counter
인 빌드에 필요한 모든 정보를 얻을 수 있습니다.
매트릭스에 오타가 있다고 생각합니다. '5'는 '1'이어야합니다. 그렇지 않습니까? – Adirio
오류가 수정되었습니다. – Misha
행 = 2 경로 = 2 결과 = 2가 무엇을 의미합니까? –