Pythonの学習(辞書、ユーザ入力とwhileループ)

はじめに:への最後の研究ではif语句、研究が続きます。

辞書

実際のオブジェクトのさまざまな辞書を正確にモデル化することができ、一緒に関連する情報を関連付けること。

辞書を使用してください

辞書のシリーズがあり键——值对、建物に対応する値は、数値、文字列、などかもしれ
内の辞書とPythonで、花括号{}値を表す-鍵の束。

バリューアクセス辞書

score = {'shuxu':'80','yuwen':'90'}

print(score['shuxu'])
print(score['yuwen'])
#输出结果:
80
90

値のペア - キーを追加します。

辞書は、いつでもキーを追加する動的構造である-の値
の時間加算方括号[]囲まを

score = {'shuxu':'80','yuwen':'90'}

print(score)

score['wuli'] = 60
score['yingyu'] = 90
print(score)
#输出结果:
{'shuxu': '80', 'yuwen': '90'}
{'shuxu': '80', 'yuwen': '90', 'wuli': 60, 'yingyu': 90}

空の辞書を作成します。

score = {}

score['wuli'] = 60
score['yingyu'] = 90
print(score)
#输出结果:
{'wuli': 60, 'yingyu': 90}

辞書の値を変更します。

score = {'yuwen':'80'}
print(score)
score['yuwen'] = '90'
print(score)
#输出结果:
{'yuwen': '80'}
{'yuwen': '90'}

Deleteキー - 値のペア

使用して、del 语句完全に削除し、デル・ステートメントを使用し、あなたが辞書の名前を指定する必要がありますし、あなたがキーを削除するに値-適切なキーかもしれません。

score = {'yuwen':80,'shuxu':90}
print(score)
del score['shuxu']
print(score)
#输出结果:
{'yuwen': 80, 'shuxu': 90}
{'yuwen': 80}

類似したオブジェクトの辞書
オブジェクト辞書は、同じ情報の数を格納する場合、これは、の形であってもよいです

yuwen_score = {
    'me': '90',
    'you':'80',
    'he':'70',
    'she':'60',
    }
print("he score is"+
    yuwen_score['he'])
#输出结果:
he score is70

キーの上にループ - 値のペア

トラバーサルキー - たとき、二つの変数は、キー値と鍵ペアを記憶するために、宣言することができます。これら二つの変数のために、あなたは任意の名前を使用することができます。

yuwen_score = {
    'me': '90',
    'you':'80',
    'he':'70',
    'she':'60',
    }
for key,value in yuwen_score.items():
    print("\nkey: " + key)
    print("value: " + value)
#items() 函数以列表返回可遍历的(键, 值) 元组数组。
#输出结果:
key: me
value: 90

key: you
value: 80

key: he
value: 70

key: she
value: 60

すべてのキーは、辞書をスクロールします

キー()関数は、すべてのキーのリストを返します。

yuwen_score = {
    'me': '90',
    'you':'80',
    'he':'70',
    'she':'60',
    }
for key in yuwen_score.keys():
    print("\nkey: " + key)
#输出结果:
key: me

key: you

key: he

key: she

辞書をトラバースするために、すべての値

ファンクションsorted()取得する按特定顺序排列キーリストのコピーを

yuwen_score = {
    'me': '90',
    'you':'80',
    'he':'70',
    'she':'60',
    }
for key in sorted(yuwen_score.keys()):
    print("\nkey: " + key)
#输出结果:
key: he

key: me

key: she

key: you    

すべての値トラバーサル辞書

方法値()、値のリストを返し、任意のキーが含まれていません。

yuwen_score = {
    'me': '90',
    'you':'80',
    'he':'70',
    'she':'60',
    }
for score in yuwen_score.values():
    print("\nscore: " + score)
#输出结果:
score: 90

score: 80

score: 70

score: 60

重複する値がある場合は使用することができ集合set、例えば:

yuwen_score = {
    'me': '90',
    'you':'80',
    'he':'70',
    'she':'90',
    }
for score in set(yuwen_score.values()):
    print("\nscore: " + score)
#输出结果:
score: 90

score: 80

score: 70

ネスティング

リストに格納された辞書のシリーズ、またはネストされたと呼ばれる辞書に格納された値のリスト、

辞書リスト

score_0 = {'subject':'yuwen','point':60}
score_1 = {'subject':'shuxu','point':70}
score_2 = {'subject':'yingyu','point':80}

scores = [score_0,score_1,score_2]

for score in scores:
    print(score)
#输出结果:
{'subject': 'yuwen', 'point': 60}
{'subject': 'shuxu', 'point': 70}
{'subject': 'yingyu', 'point': 80}

辞書に格納されたリスト

辞書内のキーの複数の値を関連付けるために必要なときはいつでも、リストは辞書にネストすることができます。

school = {
    'teacher': 'wang',
    'subjects': ['shuxu','yuwen'],
    }
print(school['teacher'])

for subject in school['subjects']:
    print("\t" + subject)
#输出结果:
wang
	shuxu
	yuwen

そして、ユーザ入力whileループ

関数の入力

関数入力()いくつかのテキストを入力するユーザーを待って、プログラムの一時停止をしましょう。ユーザ入力を取得した後、Pythonは変数に格納します。
関数の入力()を使用する場合は、入力として読み込みます字符串

message = input("please input message:\n")
print("message is:"+message)
#输出结果:
please input message:
22222
message is:22222

Int関数

文字列、整数などの入力値、入力()解釈を得るために、関数int()を使用して、直接比較することはできません

ここに画像を挿入説明
数値表現に変換する必要がある計算と比較するための入力値、前。

剰余演算子

%2つの数値を除算し、剰余を返します

print(4 % 3)
print(5 % 3)
print(6 % 3)
#输出结果:
1
2
0

利用サイクリングしながら、
構文ながら理解するために、例を

number = 1
while number <= 5:
    print(number)
    number +=1
#输出结果:
1
2
3
4
5

使用して、ループ内で継続します

サイクルの始めに戻り、テスト結果に応じてサイクリング条件を継続するかどうかを決定

number = 0
while number <10:
    number += 1
    if number % 2 ==0:
        continue

    print(number)
 #输出结果:
 1
3
5
7
9

リスト間の可動要素で

numbers = ['a','b','c']
confirmed_numbers = []

while numbers:
    middle_number = numbers.pop()#删除末尾赋给新的变量
    confirmed_numbers.append(middle_number)

for confirmed_number in confirmed_numbers:
    print(confirmed_number.title())
#输出结果:
C
B
A

すべて削除リストの要素は、特定の値が含まれています

messages = ['a','b','c','d','a','a']
print(messages)

while 'a' in messages:
    messages.remove('a')

print(messages)
#输出结果:
['a', 'b', 'c', 'd', 'a', 'a']
['b', 'c', 'd']

移入辞書にユーザー入力を使用します

#创建一个空字典
responses = {}
#设置一个标志
active = True

while active:
    name = input("\nWhat is your name?")
    like_food = input("your like food is ?")
    
    #将答案存储在字典中
    responses[name] = like_food

    repeat = input("Would you like to let another person respond? (yes/ no) ")
    if repeat =='no':
        active = False
for name,like_food in responses.items():
    print(name+":"+like_food)
#输出结果:
What is your name?222
your like food is ?222
Would you like to let another person respond? (yes/ no) no
222:222

この時間は、それが本に最初の研究、学習継続する次の時間です。

公開された71元の記事 ウォン称賛80 ビュー10000 +

おすすめ

転載: blog.csdn.net/qq_43431158/article/details/97665838