在附带的代码中,我有一个矩阵如下:
guyprefers = {
' bob':[' abi',' dee',' fay',' bea','扬&#39],
' gav':[' abi',' bea',' dee',' jan','费伊&#39],
' ian':[' bea',' dee',' abi',' fay','扬&#39],
' jon':[' bea',' abi',' fay',' jan',' DEE']}
我想从工作表preferences.xls中读取上面的矩阵
preferences.xls如下所示:
bob abi dee fay bea jan
gav abi bea dee jan fay
ian bea dee abi fay jan
jon bea abi fay jan dee
随后产生结果。我试图将矩阵行1替换为如下:
' worksheet.cell(0,0).value':[' worksheet.cell(0,1).value,' worksheet.cell(0,2).value&#39 ;,' worksheet.cell(0,3).value',' worksheet.cell(0,4).value',' worksheet.cell(0,5) .value的&#39],
但它显示错误。代码出错的地方?
import copy
import xlrd
import xlwt
workbook = xlrd.open_workbook('preferences.xls')
worksheet = workbook.sheet_by_name('guys')
guyprefers = {
'bob': ['abi', 'dee', 'fay', 'bea', 'jan'],
'gav': ['abi', 'bea', 'dee', 'jan', 'fay'],
'ian': ['bea', 'dee', 'abi', 'fay', 'jan'],
'jon': ['bea', 'abi', 'fay', 'jan', 'dee']}
galprefers = {
'abi': ['gav', 'bob', 'jon', 'ian'],
'bea': ['bob', 'gav', 'jon', 'ian'],
'dee': ['jon', 'ian', 'gav', 'bob'],
'fay': ['bob', 'ian', 'jon', 'gav'],
'jan': ['gav', 'bob', 'jon', 'ian']}
guys = sorted(guyprefers.keys())
gals = sorted(galprefers.keys())
def check(engaged):
inverseengaged = dict((v,k) for k,v in engaged.items())
for she, he in engaged.items():
shelikes = galprefers[she]
shelikesbetter = shelikes[:shelikes.index(he)]
helikes = guyprefers[he]
helikesbetter = helikes[:helikes.index(she)]
for guy in shelikesbetter:
guysgirl = inverseengaged[guy]
guylikes = guyprefers[guy]
if guylikes.index(guysgirl) > guylikes.index(she):
print("%s and %s like each other better than "
"their present partners: %s and %s, respectively"
% (she, guy, he, guysgirl))
return False
for gal in helikesbetter:
girlsguy = engaged[gal]
gallikes = galprefers[gal]
if gallikes.index(girlsguy) > gallikes.index(he):
print("%s and %s like each other better than "
"their present partners: %s and %s, respectively"
% (he, gal, she, girlsguy))
return False
return True
def matchmaker():
guysfree = guys[:]
engaged = {}
guyprefers2 = copy.deepcopy(guyprefers)
galprefers2 = copy.deepcopy(galprefers)
while guysfree:
guy = guysfree.pop(0)
guyslist = guyprefers2[guy]
gal = guyslist.pop(0)
fiance = engaged.get(gal)
if not fiance:
# She's free
engaged[gal] = guy
print(" %s and %s" % (guy, gal))
else:
# The bounder proposes to an engaged lass!
galslist = galprefers2[gal]
if galslist.index(fiance) > galslist.index(guy):
# She prefers new guy
engaged[gal] = guy
print(" %s dumped %s for %s" % (gal, fiance, guy))
if guyprefers2[fiance]:
# Ex has more girls to try
guysfree.append(fiance)
else:
# She is faithful to old fiance
if guyslist:
# Look again
guysfree.append(guy)
return engaged
print('\nEngagements:')
engaged = matchmaker()
print('\nCouples:')
print(' ' + ',\n '.join('%s is engaged to %s' % couple
for couple in sorted(engaged.items())))
print()
print('Engagement stability check PASSED'
if check(engaged) else 'Engagement stability check FAILED')

答案 0 :(得分:0)
我相信您正尝试使用"bob"
检索值'worksheet.cell(0, 0).value'
。
但是,由于您的引号,'worksheet.cell(0, 0).value'
被解释为字符串文字,并且无法解析为"bob"
。如果删除单引号,它将按照您想要的方式工作。
例如,以下代码可以替换guyprefers
所拥有的内容:
guyprefers = {
worksheet.cell(0, 0).value: [worksheet.cell(0, 1).value, worksheet.cell(0, 2).value, worksheet.cell(0, 3).value, worksheet.cell(0, 4).value, worksheet.cell(0, 5).value],
worksheet.cell(1, 0).value: [worksheet.cell(1, 1).value, worksheet.cell(1, 2).value, worksheet.cell(1, 3).value, worksheet.cell(1, 4).value, worksheet.cell(1, 5).value],
worksheet.cell(2, 0).value: [worksheet.cell(2, 1).value, worksheet.cell(2, 2).value, worksheet.cell(2, 3).value, worksheet.cell(2, 4).value, worksheet.cell(2, 5).value],
worksheet.cell(3, 0).value: [worksheet.cell(3, 1).value, worksheet.cell(3, 2).value, worksheet.cell(3, 3).value, worksheet.cell(3, 4).value, worksheet.cell(3, 5).value]}
这应该让你开始。请注意,使用for循环可能更容易。