좌석은 A, 1 A, 2 등을해야한다 ..
from pprint import pprint as pp
missing = (("B",4),("C",4),("D",4),("E",4))
seats = {(x,y):"x" if (x,y) not in missing else "-" for x in ["A","B","C","D","E"] for y in range(1,8)}
pp(seats)
{('A', '1'): '+',
('A', '2'): '+',
('A', '3'): '+',
('A', '4'): '+',
('A', '5'): '+',
('A', '6'): '+',
('A', '7'): '+',
('B', '1'): '+',
('B', '2'): '+',
('B', '3'): '+',
('B', '4'): '-',
('B', '5'): '+',
('B', '6'): '+',
('B', '7'): '+',
('C', '1'): '+',
('C', '2'): '+',
('C', '3'): '+',
('C', '4'): '-',
('C', '5'): '+',
('C', '6'): '+',
('C', '7'): '+',
('D', '1'): '+',
('D', '2'): '+',
('D', '3'): '+',
('D', '4'): '-',
('D', '5'): '+',
('D', '6'): '+',
('D', '7'): '+',
('E', '1'): '+',
('E', '2'): '+',
('E', '3'): '+',
('E', '4'): '-',
('E', '5'): '+',
('E', '6'): '+',
('E', '7'): '+'}
당신은 내가 DICT 피클 것 변경 사항을 유지하려면 다음
missing = (("B",4),("C",4),("D",4),("E",4))
import os
import pickle
# if file does not exist, this is the first booking
if not os.path.isfile("bookings.pkl"):
seats = {(x,str(y)):"+" if (x,y) not in missing else "-" for x in ["A","B","C","D","E"] for y in range(1,8)}
else:
# else use the previous updated dict
with open("bookings.pkl") as f:
seats = pickle.load(f)
while True:
print("Seats marked + are available\n")
srt = sorted(seats.iteritems())
# split into sections based on row and print rows
sections = [srt[n:n+7] for n in xrange(0,len(srt),7)]
for sec in sections:
for seat, status in sec:
print("{}{}:{status} ".format(*seat,status=status)),
print("")
inp = tuple(raw_input("Choose your seat in format row number ie A 1:").upper().split())
if inp in seats:
# and not already taken
if seats[inp] != "*":
print("You have chosen row: {} seat number: {}\n".format(*inp))
# ask user to confirm their choice
confirm = raw_input("Enter y to accept any n to change").lower()
if confirm == "y":
seats[inp] = "*"
with open("bookings.pkl","w") as f:
pickle.dump(seats,f)
print("Booking confirmed, goodbye.")
break
else:
# else let user choose again
continue
print("Seat unavailable\n")
else:
print("Invalid choice")
끔찍한 생각입니다. 10000 석이 있다면 어떨까요? – eliasah
파일에 쓸 필요가없는 경우 'dict' – user2097159
CSV 대신 사용할 수 있습니까? –