我一直在努力寻找一种有效的方法来存储和调用数据到我的程序中,这是一种NBA模拟类型的游戏。我试图将数据保存在单独文件中的列表中,然后我只使用导入并在使用时调用团队列表。
示例:
# Player Stats and Skills
# [player name / height / strength / speed / jump / blocks / steals / dribble / pass / reb / reb aggr]
uth = [["Trey Burke", 27, 12, 82, 58, 30, 57, 79, 75, 14, 0.10],
["Alec Burks", 48, 27, 79, 52, 35, 39, 56, 23, 24, 0.25],
["Gordon Hayward", 52, 29, 58, 54, 28, 36, 52, 47, 27, 0.45],
["Derrick Favors", 74, 36, 37, 66, 86, 30, 29, 25, 74, 0.75],
["Rudy Gobert", 90, 38, 29, 36, 76, 37, 15, 29, 62, 1.00]]
lac = [["Chris Paul", 36, 40, 86, 59, 23, 98, 98, 98, 37, 0.10],
["J.J. Redick", 22, 14, 73, 40, 17, 35, 67, 47, 25, 0.20],
["Jamal Crawford", 48, 32, 76, 59, 22, 54, 91, 52, 31, 0.30],
["Blake Griffin", 88, 88, 75, 57, 76, 56, 43, 43, 92, 0.60],
["DeAndre Jordan", 96, 96, 40, 94, 96, 40, 32, 36, 96, 1.00]]
问题是我将存储相当多的数据。
然后我尝试将Sqlite3用于python,但是无法弄清楚如何将数据部分存储到团队中,然后将它们保存为列表,以便我可以在我的代码中调用数据。
我创建的数据库示例:
import sqlite3
conn = sqlite3.connect("player_info.db")
c = conn.cursor()
def createTable():
c.execute(''' CREATE TABLE Player_Info
(id INTEGER PRIMARY KEY, First TEXT, Last TEXT,
Position TEXT, Height TEXT, Weight TEXT, Years INTEGER, College TEXT)
''')
conn.commit()
def celticsPlayerInfo():
players = [("Brandon", "Bass", "C/PF", "6'8", "250lbs", 9, "Louisiana State"),
("Avery", "Bradley", "SG/PG", "6'2", "180lbs", 4, "Texas"),
("Jae", "Crowder", "SF", "6'6", "235lbs", 2, "Marquette"),
("Luigi", "Datome", "SF/PF", "6'8", "215lbs", 1, "Italy"),
("Jonas", "Jerebko", "PF", "6'10", "231lbs", 4, "Sweden"),
("Kelly", "Olynyk", "C/PF", "7'0", "238lbs", 1, "Gonzaga"),
("Phil", "Pressey", "PG/SG", "5'11", "175lbs", 1, "Missouri"),
("Shavlik", "Randolph", "PF", "6'10", "236lbs", 7, "Duke"),
("Marcus", "Smart", "PG/SG", "6'4", "220lbs", 0, "Oklahoma State"),
("Jared", "Sullinger", "PF", "6'9", "260lbs", 2, "Ohio State"),
("Isaiah", "Thomas", "PG", "5'9", "185lbs", 3, "Washington"),
("Evan", "Turner", "SG/SF", "6'7", "220lbs", 4, "Ohio State"),
("Gerald", "Wallace", "SF", "6'7", "220lbs", 13, "Alabama"),
("James", "Young", "SG/SF", "6'6", "215lbs", 0, "Kentucky"),
("Tyler", "Zeller", "C/PF", "7'0", "253lbs", 2, "North Carolina")]
c.executemany(''' INSERT INTO Player_Info(First, Last, Position, Height, Weight, Years, College)
VALUES(?,?,?,?,?,?,?)''', players)
conn.commit()
def netsPlayerInfo():
players = [("Alan", "Anderson", "SG/SF", "6'6", "220lbs", 5, "Michigan State"),
("Bojan", "Bogdanovic", "SG/SF", "6'8", "216lbs", 0, "Croatia"),
("Markel", "Brown", "SG", "6'3", "190lbs", 0, "Oklahoma State"),
("Jarrett", "Jack", "PG", "6'3", "200lbs", 9, "Georgia Tech"),
("Cory", "Jefferson", "SF", "6'9", "218lbs", 0, "Baylor"),
("Joe", "Johnson", "SF/SG", "6'7", "240lbs", 13, "Arkansas"),
("Jerome", "Jordan", "C", "7'0", "253lbs", 1, "Tulsa"),
("Sergey", "Karasev", "SG/SF", "6'7", "208lbs", 1, "Russia"),
("Brook", "Lopez", "C", "7'0", "275lbs", 6, "Stanford"),
("Darius", "Morris", "SG", "6'4", "195lbs", 3, "Michigan"),
("Mason", "Plumlee", "C/PF", "6'11", "235lbs", 1, "Duke"),
("Mirza", "Teletovic", "PF", "6'9", "242lbs", 2, "Bosnia"),
("Deron", "Williams", "PG", "6'3", "200lbs", 9, "Illinois"),
("Thaddeus", "Young", "SF/PF", "6'8", "221lbs", 7, "Georgia Tech")]
c.executemany(''' INSERT INTO Player_Info(First, Last, Position, Height, Weight, Years, College)
VALUES(?,?,?,?,?,?,?)''', players)
conn.commit()
然后我会调用数据并将其放在这样的列表中:
import sqlite3
conn = sqlite3.connect("player_info.db")
cursor = conn.cursor()
teams = cursor.execute("SELECT * FROM Player_Info")
teams = teams.fetchall()
test = [list(i) for i in teams]
print test
问题是我无法弄清楚如何将这两个团队直接从数据库中分成单独的列表,以便我可以操纵游戏中的数据。
我应该使用Sqlite3还是其他方法存储数据?
答案 0 :(得分:0)
我不熟悉sqlite,但你考虑过使用对象和词典吗?给每个玩家对象一个名字,高度等,然后将玩家放入代表他们团队的字典中,然后将每个团队放入代表联盟的字典中。这应该在Python 2.7或3.x中运行。
class Player(object):
def __init__(self, first, last, position, height_feet, height_inches, weight, jump, origin):
self.first = first
self.last = last
self.position = position
self.height = height_feet*12 + height_inches
self.weight = weight
self.jump = jump
self.origin = origin
all_teams = {}
all_teams['Celtics'] = {"Brandon Bass":Player('Brandon', 'Bass', 'C/PF', 6, 8, 250, 9, 'Louisiana State'),
"Avery Bradley":Player('Avery', 'Bradley', 'SG/PG', 6, 2, 180, 4, 'Texas')} # and so on
然后,要获得Brandon Bass的体重,例如,你会做这样的事情:
>>> print(all_teams.get('Celtics').get('Brandon Bass').weight)
250
看看Brandon Bass与Avery Bradley相比有多高:
>>> print(all_teams.get('Celtics').get('Brandon Bass').height - all_teams.get('Celtics').get('Avery Bradley').height)
6
让Brandon Bass的身高恢复到英尺和英寸:
>>> divmod(all_teams.get('Celtics').get('Brandon Bass').height, 12)
(6, 8)
我希望能给你一些想法。