搜索列表列表并显示结果

时间:2014-03-18 02:19:02

标签: python list tabular

我有一个列表如下:

[['De Aza, Alejandro', 'CWS', '153', '607', '84', '160', '27', '4', '17'],
 ['Hunter, Torii', 'DET', '144', '606', '90', '184', '37', '5', '17'],
 ['Hamilton, Josh', 'LAA', '151', '576', '73', '144', '32', '5', '21'],
 ['Choo, Shin-Soo', 'CIN', '154', '569', '107', '162', '34', '2', '21'],
 ['Upton, Justin', 'ATL', '149', '558', '94', '147', '27', '2', '27'],
 ['Cabrera, Miguel', 'DET', '148', '555', '103', '193', '26', '1', '44'],
 ['Posey, Buster', 'SF', '148', '520', '61', '153', '34', '1', '15'],
 ['Suzuki, Ichiro', 'NYY', '150', '520', '57', '136', '15', '3', '7'],
 ['Holliday, Matt', 'STL', '141', '520', '103', '156', '31', '1', '22'],
 ['Headley, Chase', 'SD', '141', '520', '59', '130', '35', '2', '13'],
 ['Cabrera, Asdrubal', 'CLE', '136', '508', '66', '123', '35', '2', '14'],
 ['Pierzynski, A.J.', 'TEX', '134', '503', '48', '137', '24', '1', '17'],
 ['Hoes, L.J.', 'HOU', '46', '167', '24', '48', '7', '2', '1'],
 ['Young Jr., Eric', 'COL', '57', '165', '22', '40', '9', '3', '1'],
 ['Hairston, Scott', 'CHC', '52', '99', '13', '17', '2', '0', '8'],
 ['Arnaud, Travis', 'NYM', '31', '99', '4', '20', '3', '0', '1'],
 ['Ankiel, Rick', 'NYM', '20', '66', '7', '12', '4', '1', '2'],
 ['Ankiel, Rick', 'HOU', '25', '62', '6', '12', '3', '0', '5'],
 ['den Dekker, Matt', 'NYM', '27', '58', '7', '12', '1', '0', '1'],
 ['Sanchez, Angel', 'CWS', '1', '2', '0', '0', '0', '0', '0']]

我希望用户输入一个命令,例如" TEAM DET"以及生成该团队元素的所有列表的列表,因此" TEAM DET"会想出类似的东西:

Name          | Team | Games played | at bats | runs scored | hits | doubles | triples | homeruns |
Hunter, Torii | DET  | 144          | 606     | 90          | 184  | 37      | 5       | 17       | 

所有DET成员

等等

编辑:我使用的是python 3.3.3

3 个答案:

答案 0 :(得分:2)

使用defaultdict简化查找。创建一个字典,其中团队名称是关键,具有该团队的所有条目都是值。所以这些值将是一个列表。因此defaultdict(list)是合适的。

from collections import defaultdict
from pprint import pprint

data_dict = defaultdict(list)
for item in data:
    data_dict[item[1]].append(item)
pprint(data_dict['DET'])

这就是你得到的:

[['Hunter, Torii', 'DET', '144', '606', '90', '184', '37', '5', '17'],
 ['Cabrera, Miguel', 'DET', '148', '555', '103', '193', '26', '1', '44']]

然后,您可以使用csv模块格式化并以您需要的方式显示输出。希望它有所帮助。

答案 1 :(得分:0)

不使用元组,而是使用字典列表。

示例:

players = []
player = {'Player': 'Hunter, Torii', 'Team': 'DET', 'Games Played': '144', 'At Bats': '606', 'Runs Scored': '90', 'Hits': '184', 'Doubles': '37', 'Triples': '5', 'Homeruns': '17']
players.append(player)
#Put each player in that format and append to the list

input = input()

for player in players:
    if player['Team'] == input:
        print(player)

显然,您可以将print语句格式化为您想要的方式。如果您有任何问题,请在下面提问。

答案 2 :(得分:0)

这是一个满足您要求的紧凑示例程序:

l=[['De Aza, Alejandro', 'CWS', '153', '607', '84', '160', '27', '4', '17'],
 ['Hunter, Torii', 'DET', '144', '606', '90', '184', '37', '5', '17'],
 ['Hamilton, Josh', 'LAA', '151', '576', '73', '144', '32', '5', '21'],
 ['Choo, Shin-Soo', 'CIN', '154', '569', '107', '162', '34', '2', '21'],
 ['Upton, Justin', 'ATL', '149', '558', '94', '147', '27', '2', '27'],
 ['Cabrera, Miguel', 'DET', '148', '555', '103', '193', '26', '1', '44']]
header="|Name              | Team | Games played | at bats | runs scored | hits | doubles | triples | homeruns |"
formatstring="".join(["|%-"+str(columnwidth)+"s" for columnwidth in [len(i) for i in header.split("|")[1:-1]]])+"|"
command=raw_input("Enter a command!\n")
cmdparts=command.split()
if cmdparts[0]=="TEAM" and len(cmdparts)>=2:
  print header
  for team in cmdparts[1:]:
    for item in l:
      if  team in item[1]:
        print formatstring % tuple(item)

当它被调用时,它要求命令。如果您键入TEAM DET LAA,它将在表格中列出DET和LAA团队的所有条目:

$ python test.py
Enter a command!
TEAM DET LAA
|Name              | Team | Games played | at bats | runs scored | hits | doubles | triples | homeruns |
|Hunter, Torii     |DET   |144           |606      |90           |184   |37       |5        |17        |
|Cabrera, Miguel   |DET   |148           |555      |103          |193   |26       |1        |44        |
|Hamilton, Josh    |LAA   |151           |576      |73           |144   |32       |5        |21        |