ホームページ >バックエンド開発 >Python チュートリアル >Tips | 使いやすくてとっつきやすい「Python辞典」の知識ポイント11選!
dictionary
の概念がその後の学習や仕事に役立ちます。 >>> # Python 2.7 >>> a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'} >>> a_dict {'color': 'blue', 'pet': 'dog', 'fruit': 'apple'} >>> a_dict {'color': 'blue', 'pet': 'dog', 'fruit': 'apple'}
>>> # Python 3.5 >>> a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'} >>> a_dict {'color': 'blue', 'pet': 'dog', 'fruit': 'apple'} >>> a_dict {'color': 'blue', 'pet': 'dog', 'fruit': 'apple'}
>>> a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'} >>> a_dict {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'} >>> a_dict {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}
>>> a_dict = {'one': 1, 'two': 2, 'thee': 3, 'four': 4} >>> new_dict = {} >>> for key, value in a_dict.items(): ... new_dict[value] = key ... >>> new_dict {1: 'one', 2: 'two', 3: 'thee', 4: 'four'}
>>> a_dict = {'one': 1, 'two': 2, 'thee': 3, 'four': 4} >>> new_dict = {} # Create a new empty dictionary >>> for key, value in a_dict.items(): ... if value <= 2: ... new_dict[key] = value ... >>> new_dict {'one': 1, 'two': 2}
>>> incomes = {'apple': 5600.00, 'orange': 3500.00, 'banana': 5000.00} >>> total_income = 0.00 >>> for value in incomes.values(): ... total_income += value # Accumulate the values in total_income ... >>> total_income 14100.0
>>> objects = ['blue', 'apple', 'dog'] >>> categories = ['color', 'fruit', 'pet'] >>> a_dict = {key: value for key, value in zip(categories, objects)} >>> a_dict {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}
>>> a_dict = {'one': 1, 'two': 2, 'thee': 3, 'four': 4} >>> new_dict = {value: key for key, value in a_dict.items()} >>> new_dict {1: 'one', 2: 'two', 3: 'thee', 4: 'four'}
>>> a_dict = {'one': 1, 'two': 2, 'thee': 3, 'four': 4} >>> new_dict = {k: v for k, v in a_dict.items() if v <= 2} >>> new_dict {'one': 1, 'two': 2}
>>> incomes = {'apple': 5600.00, 'orange': 3500.00, 'banana': 5000.00} >>> total_income = sum([value for value in incomes.values()]) >>> total_income 14100.0
>> incomes = {'apple': 5600.00, 'orange': 3500.00, 'banana': 5000.00} >>> sorted_income = {k: incomes[k] for k in sorted(incomes)} >>> sorted_income {'apple': 5600.0, 'banana': 5000.0, 'orange': 3500.0}
>>> prices = {'apple': 0.40, 'orange': 0.35, 'banana': 0.25} >>> def discount(current_price): ... return (current_price[0], round(current_price[1] * 0.95, 2)) ... >>> new_prices = dict(map(discount, prices.items())) >>> new_prices {'apple': 0.38, 'orange': 0.33, 'banana': 0.24}
>>> prices = {'apple': 0.40, 'orange': 0.35, 'banana': 0.25} >>> def has_low_price(price): ... return prices[price] < 0.4 ... >>> low_price = list(filter(has_low_price, prices.keys())) >>> low_price ['orange', 'banana']
>>> vegetable_prices = {'pepper': 0.20, 'onion': 0.55} >>> fruit_prices = {'apple': 0.40, 'orange': 0.35, 'pepper': .25} >>> {**vegetable_prices, **fruit_prices} {'pepper': 0.25, 'onion': 0.55, 'apple': 0.4, 'orange': 0.35}
以上がTips | 使いやすくてとっつきやすい「Python辞典」の知識ポイント11選!の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。