第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何添加到元組

如何添加到元組

長風秋雁 2022-07-26 17:05:15
我有一個將 3 個項目傳遞給 HTML 頁面的元組,我希望它傳遞第四個。我已經在網上閱讀了要做到這一點,我必須將我的元組變成一個列表,但我試過了,但仍然沒有得到任何東西(沒有錯誤,對象最后沒有出現(xiàn)) . 所以為了清楚起見,我把它藏起來了。我正在嘗試將“maybe_existing_user.fb_pic”添加到“詳細信息”元組中。PYTHON@app.route('/results/<int:id>')def results(id):    rate = 0  # either 0 or num/total    article_list_of_one = Article.query.filter_by(id=id)    a_obj = article_list_of_one[0]    avs_obj = retrieve_article_vote_summary(a_obj.id) # vote_summary is a list of [tuples('True', numOfTrue), etc]    total_votes = avs_obj.getTotalVotes()    vote_choices = []    vote_choice_list = VoteChoice.getVoteChoiceList()    for item in vote_choice_list: # looping over VoteChoice objects        num = avs_obj.getVoteCount(item.choice)        if total_votes > 0:        # protecting against no votes            rate = num/total_votes         vote_choices.append([item.choice, item.color, num, rate*100, total_votes])    details = avs_obj.getVoteDetails() # 10/02 - retrieve array of tuples [(user, VoteChoice, Comments)]    print("Inside results(" + str(id) + "):")    details_count = 0    for detail in details:        maybe_existing_user = User.query.filter_by(name=detail[0]).first()        detail += (maybe_existing_user.fb_pic,)        print(detail)        #print("    " + str(details_count) + ": " + details[0] + " " + details[1] + " " + details[2])        details_count += 1    return render_template('results.html', title=a_obj.title, id=id,                           image_url=a_obj.image_url, url=a_obj.url,                           vote_choices=vote_choices, home_data=Article.query.all(),                           vote_details=details)HTML<!DOCTYPE html><html><body>{% for detail in vote_details %}<strong>User:</strong> {{ detail[0] }} &nbsp; <strong>Vote:</strong> {{ detail[1] }} &nbsp; <strong>Comments:</strong> {{ detail[2] }}<br>{{ detail[3] }}{% endfor %}</body></html>
查看完整描述

4 回答

?
瀟湘沐

TA貢獻1816條經驗 獲得超6個贊

您需要創(chuàng)建一個新的元組“詳細信息”列表。在 Python 中,您通常無法“就地”更改列表。如果您只是在 for 循環(huán)中創(chuàng)建一個新的“詳細信息”,它將不會傳遞到“詳細信息”列表。

因此,您需要用for detail in details:以下行替換完整的循環(huán):

updated_details = [(user, VoteChoice, Comments, User.query.filter_by(name=user).first().fb_pic)
                   for (user, VoteChoice, Comments) in details]

最后,您將這些更新的詳細信息用于返回的 render_template:

return render_template(..., vote_details=updated_details)


查看完整回答
反對 回復 2022-07-26
?
拉風的咖菲貓

TA貢獻1995條經驗 獲得超2個贊

元組是不可變的,因此您不能直接修改它們。


在您的 for 循環(huán)中,從原始detail元組中創(chuàng)建一個列表,并將附加值附加到該列表中。然后,您可以將列表轉換回元組:


detail_list = list(detail)

detail_list += [maybe_existing_user.fb_pic]

detail = tuple(detail_list)


查看完整回答
反對 回復 2022-07-26
?
哈士奇WWW

TA貢獻1799條經驗 獲得超6個贊

使用 splat 操作符在一行中重新打包


detail_list = *detail_list, maybe_existing_user.fb_pic

例如,在 python3 shell 中:


>>> detail_list = ("cat", "dog")

>>> detail_list = *detail_list, "rabbit"

>>> detail_list

('cat', 'dog', 'rabbit')

>>> 


查看完整回答
反對 回復 2022-07-26
?
ABOUTYOU

TA貢獻1812條經驗 獲得超5個贊

您已經正確添加到元組中。問題是detail += ...創(chuàng)建一個新元組而不是附加到details列表中的現(xiàn)有元組(因為元組是不可變的)。當您print(detail)在循環(huán)中時,它似乎已正確更改,但如果您要打印整個列表details,您會看到其中的元組沒有附加信息。


一種解決方案是使用新元組重建詳細信息列表。


def add_more_info(t):

    maybe_existing_user = User.query.filter_by(name=detail[0]).first()

    return t + (maybe_existing_user, )


details = [add_more_info(detail) for detail in details]

另一種解決方案是首先將所有詳細信息轉換為列表。然后你可以附加到它們。


details = [list(detail) for detail in details]

for detail in details:

    maybe_existing_user = User.query.filter_by(name=detail[0]).first()

    detail.append(maybe_existing_user)


查看完整回答
反對 回復 2022-07-26
  • 4 回答
  • 0 關注
  • 135 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號