辞書とリストを処理するPythonのwhileループ

whileループのリストと辞書の処理

forループはリストをトラバースする効果的な方法ですが、リストをforループで変更しないでください。変更しないと、Pythonが要素を追跡しにくくなります。リストの反復中にリストを変更するには、whileループを使用します。大量の入力を収集、保存、および整理して、後で表示および表示するために、whileループを通じてリストや辞書と組み合わせて使用​​できます。

  1. リスト間で要素を移動する
    たとえば、まだ確認されていない新しく登録されたWebサイトユーザーを含むリストがあります。これらのユーザーを確認した後、別の確認済みユーザーリストに移動するにはどうすればよいですか?1つの方法は、whileループを使用して、ユーザーの確認中に未確認ユーザーのリストからユーザーを抽出し、確認済みユーザーの別のリストに追加することです。
unconfirmed_users = ['alice' , 'brian' , 'candace']
confirmed_users = []

while unconfirmed_users:
	current_user = unconfirmed_users.pop()
	print("Verifying user: " + current_user.title())
	confirmed_users.append(current_user)
	
print("\nThe following users have been confirmed: ")
for confirmed_user in confirmed_users:
	print(confirmed_user.title())
Verifying user: Candace
Verifying user: Brian
Verifying user: Alice

The following users have been confirmed: 
Candace
Brian
Alice


------------------
(program exited with code: 0)
Press return to continue

  1. 特定の値を含むすべてのリスト要素の削除
    前のセクションでは、メソッドremove()を使用してリスト内の特定の値を削除したと説明しましたが、これは、削除する値がリストに一度だけ表示されるため可能です。特定の値を含むリスト内のすべての要素を削除する場合は、whileループを使用できます。
    たとえば、次のとおりです。
pets = ['dog' , 'cat' , 'dog' , 'goldfish' , 'cat' , 'rabbit' , 'cat']
print(pets)

while 'cat' in pets:
	pets.remove('cat')
	
print(pets)
['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
['dog', 'dog', 'goldfish', 'rabbit']


------------------
(program exited with code: 0)
Press return to continue

  1. ユーザー入力を使用してディクショナリを入力し
    ます。whileループを使用して、ユーザーに任意の量の情報を入力するように求めることができます。以下は、調査プログラムの例です。これらの中で、ループが実行されるたびに、回答者の名前と回答の入力を求められます。収集するデータ回答を回答者に関連付けるために辞書に保存されます。
responses = {}

polling_active = True

while polling_active:
	name = input("\nWhat is your name?")
	response = input("Which mountain would you like to climb someday?")
	
	responses[name] = response
	
	repeat = input("Would you like to let another person respond?(yes/no)")
	if repeat == 'no':
		polling_active = False
		
print("\n----Poll Results------")
for name,response in responses.items():
	print(name + "Would like to climb " + response + ".")

What is your name?aa
Which mountain would you like to climb someday?aa
Would you like to let another person respond?(yes/no)yes

What is your name?bb
Which mountain would you like to climb someday?jj
Would you like to let another person respond?(yes/no)yes

What is your name?oo
Which mountain would you like to climb someday?pp
Would you like to let another person respond?(yes/no)no

----Poll Results------
aaWould like to climb aa.
bbWould like to climb jj.
ooWould like to climb pp.


------------------
(program exited with code: 0)
Press return to continue

例:

print("wu xiang jiang niu rou mai wan liao!")
sandwich_orders = ['hanbao' ,'pastrami', 'regou' , 'pastrami','zhurou' , 'niurou','pastrami']
finished_sandwiches = []

while sandwich_orders:
	sandwich_order = sandwich_orders.pop()
#	print("I made your " + sandwich_order)
	if sandwich_order == 'pastrami':
		continue
	else:
		finished_sandwiches.append(sandwich_order)
	
for finished_sandwiche in finished_sandwiches:
	print(finished_sandwiche)
wu xiang jiang niu rou mai wan liao!
niurou
zhurou
regou
hanbao


------------------
(program exited with code: 0)
Press return to continue

例:

vocations = {}

select = True

while select:
	name = input("plese input your name: ")
	vaction = input("your want go to place: ")
	
	vocations[name] = vaction
	
	repeat = input("whether who join,if have:yes,else:no")
	if repeat == 'no':
		select = False
		
for name,vaction in vocations.items():
	print(name + "want go to " + vaction + "playing")
plese input your name: oo
your want go to place: dd
whether who join,if have:yes,else:noyes
plese input your name: pp
your want go to place: dd
whether who join,if have:yes,else:noyes
plese input your name: rr
your want go to place: gg
whether who join,if have:yes,else:nono
ppwant go to ddplaying
rrwant go to ggplaying
oowant go to ddplaying


------------------
(program exited with code: 0)
Press return to continue

53件の元の記事を公開 賞賛された16件 訪問2213件

おすすめ

転載: blog.csdn.net/m0_37757533/article/details/105469649