1

我试图在每行上显示我的列表,每行有 7 个状态,这是我到目前为止所拥有的,但它显示为所有状态的一个长字符串,每个状态都带有引号。我忘了说这是给我的 CS 课的,我们还没有学过 iter,所以我们不允许使用它。给我的唯一提示是将 STATE_LIST 转换为字符串,然后使用 '\n' 将其分解

state = str(STATE_LIST)

displaystates = Text(Point(WINDOW_WIDTH/2, WINDOW_HEIGHT/2), state.split('\n'))
displaystates.draw(win)

STATE_LIST 看起来像这样

STATE_VOTES = {
"AL" : 9, # Alabama
"AK" : 3, # Alaska
"AZ" : 11, # Arizona
"AR" : 6, # Arkansas
"CA" : 55, # California
"CO" : 9, # Colorado
"CT" : 7, # Connecticut
"DE" : 3, # Delaware
"DC" : 3, # Washington DC
"FL" : 29, # Florida
"GA" : 16, # Georgia
"HI" : 4, # Hawaii
"ID" : 4, # Idaho
"IL" : 20, # Illinois
"IN" : 11, # Indiana
"IA" : 6, # Iowa
"KS" : 6, # Kansas
"KY" : 8, # Kentucky
"LA" : 8, # Louisiana
"ME" : 4, # Maine
"MD" : 10, # Maryland
"MA" : 11, # Massachusetts
"MI" : 16, # Michigan
"MN" : 10, # Minnesota
"MS" : 6, # Mississippi
"MO" : 10, # Missouri
"MT" : 3, # Montana
"NE" : 5, # Nebraska
"NV" : 6, # Nevada
"NH" : 4, # New Hampshire
"NJ" : 14, # New Jersey
"NM" : 5, # New Mexico
"NY" : 29, # New York
"NC" : 15, # North Carolina
"ND" : 3, # North Dakota
"OH" : 18, # Ohio
"OK" : 7, # Oklahoma
"OR" : 7, # Oregon
"PA" : 20, # Pennsylvania
"RI" : 4, # Rhode Island
"SC" : 9, # South Carolina
"SD" : 3, # South Dakota
"TN" : 11, # Tennessee
"TX" : 38, # Texas
"UT" : 6, # Utah
"VT" : 3, # Vermont
"VA" : 13, # Virginia
"WA" : 12, # Washington
"WV" : 5, # West Virginia
"WI" : 10, # Wisconsin
"WY" : 3 # Wyoming
}

STATE_LIST = sorted(list(STATE_VOTES.keys()))

我试图让它看起来像这样 样本

4

3 回答 3

2

像这样的东西怎么样:

STATE_LIST = sorted(STATE_VOTES)

def my_iter(iterable):
    for item in iterable:
        yield item

from itertools import izip_longest

s = ',\n'.join(', '.join(state for state in group if state) for
                        group in izip_longest(*[my_iter(STATE_LIST)]*7))

print s

输出:

AK, AL, AR, AZ, CA, CO, CT,
DC, DE, FL, GA, HI, IA, ID,
IL, IN, KS, KY, LA, MA, MD,
ME, MI, MN, MO, MS, MT, NC,
ND, NE, NH, NJ, NM, NV, NY,
OH, OK, OR, PA, RI, SC, SD,
TN, TX, UT, VA, VT, WA, WI,
WV, WY

该示例使用izip_longest()来自itertools模块。如果由于某种原因您不允许使用该标准库,那么文档中有相应的源代码。

于 2012-11-06T23:25:51.327 回答
0

有了这条线:

state = str(STATE_LIST)

您只是将列表转换为字符串,接受 Python 选择使用的任何格式。在这种情况下,字符串表示将只是

["AL", "AK", ..., "WY"]

根本没有换行符。只是一个看起来像列表的长字符串。

您真正想要的字符串似乎是这个:

AL AK AR AZ CO CT DC DE FL
GA HI ...
              ... WI WV WY

(您似乎在每行上放了 9 或 10 个项目;不确定这将如何在 5 行中工作,列出 51 个项目,但让我们让它灵活一些)

假设你想把它分成五行。你可以这样做:

# Start with an empty list
state_lines = []

# Calculate the maximum number of items in each row
items_per_row = (len(STATE_LIST)+4)/5 # If you wanted, you could just say "11" here.

 # First item will be 0, 11, 22, etc.
 for first_item in range(0, len(STATE_LIST), items_per_row):

    # This contains all of the states for this line. For example,
    # ["AL", "AK", "AR", "AZ", "CO", "CT", "DC", "DE", FL", "GA", "HI"]
    states = STATE_LIST[first_item : first_item + items_per_row]

    # state_line will be a string of states separated by spaces,
    # like "AL AK AR AZ CO CT DC DE FL GA HI"
    state_line = ' '.join(states)

    # Add the new state line to the list
    state_lines.append(state_line)

# Finally, join the lines together with newlines in between them
state = '\n'.join(state_lines)

然后,您的state变量将包含其余代码所需的字符串。

于 2012-11-06T23:35:32.867 回答
0

你为什么不尝试这样简单的事情:

>>> counter = 1
>>> for i in STATE_LIST:
...    sys.stdout.write(i+' ')
...    if not counter % 7:
...        print '\n'
...    counter += 1
... 
AK AL AR AZ CA CO CT 

DC DE FL GA HI IA ID 

IL IN KS KY LA MA MD 

ME MI MN MO MS MT NC 

ND NE NH NJ NM NV NY 

OH OK OR PA RI SC SD 

TN TX UT VA VT WA WI 

WV WY
于 2012-11-07T11:18:47.870 回答