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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用“python -c”控制臺(tái)命令模式實(shí)現(xiàn)for循環(huán)

使用“python -c”控制臺(tái)命令模式實(shí)現(xiàn)for循環(huán)

慕容3067478 2024-01-27 14:55:12
我想python -c通過 shell 腳本運(yùn)行以下代碼:import json,sysobj=json.loads(open('/temp/209/209formatted_response.json','r').read())revision=obj['component']['referencingComponents'][0]['revision']for element in list(revision):    if 'lastModifier' in element:        del revision['lastModifier']print(revision)使用python -c,它在沒有 for 循環(huán)的情況下運(yùn)行良好,但如果我在其中包含 for 循環(huán),它會(huì)拋出錯(cuò)誤:下面工作正常:python -c "import json,sys; obj=json.loads(open('/temp/209/209formatted_response.json','r').read());revision=obj['component']['referencingComponents'][0]['revision']"下面給出錯(cuò)誤:$ /opt/app/anaconda3/envs/anaconda3/bin/python -c "import json,sys; obj=json.loads(open('/temp/209/209formatted_response.json','r').read());revision=obj['component']['referencingComponents'][0]['revision'];for element in list(revision):    if 'lastModifier' in element:        del revision['lastModifier']; print(revision)"  File "<string>", line 1    import json,sys; obj=json.loads(open('/temp/209/209formatted_response.json','r').read());revision=obj['component']['referencingComponents'][0]['revision'];for element in list(revision):    if 'lastModifier' in element:        del revision['lastModifier']; print(revision)                                                                                                                                                                                      ^SyntaxError: invalid syntax我什至嘗試在行尾的\n循環(huán)的每個(gè)語句之前使用,但沒有成功;
查看完整描述

2 回答

?
GCT1015

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超4個(gè)贊

一種可能的解決方案是使用換行符,但不要直接添加它們:


$ python3 -c "exec(\"n = 3\nfor i in range(n):\n  print(i)\")"

0

1

2

像您一樣添加\n,但也將代碼括起來"exec(\"...\")"


或者您可以插入真正的換行符:


$ python3 -c "n = 3

> for i in range(n):

>   print(i)"

0

1

2


查看完整回答
反對(duì) 回復(fù) 2024-01-27
?
慕碼人8056858

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超6個(gè)贊

問題在于縮進(jìn),因此您可以使用過濾函數(shù)來代替使用 for 和 if 語句

revision = list(filter(lambda x: not 'lastModifier' in x, list(revision)));


查看完整回答
反對(duì) 回復(fù) 2024-01-27
  • 2 回答
  • 0 關(guān)注
  • 251 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)