程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python3 learning notes -- set, tuple, dictionary, list comparison

編輯:Python

data structure

Python The following data structures are supported : list , Dictionaries , Tuples , aggregate .

When to use a dictionary :

  • When you need keys : When the logical association between value pairs .

  • When you need to quickly find data based on a custom key .

  • When your data is constantly modified . please remember , The dictionary is changeable .

When to use other types :

  • If you have some data sets that don't need random access , Please use the list . When you need a simple , Iteratively, frequently modified sets can use lists .

  • If you need element uniqueness , Use set .

  • Use tuples when data cannot be changed .

A lot of times , Tuples are used in conjunction with dictionaries , For example, tuples may represent a keyword , Because it's immutable .

1、 list

Use square brackets establish

words = ["Hello", "world", "!"]

Create an empty list with empty square brackets

It can be accessed by index

Most of the time , The last item in the list does not have a comma . However , Placing a comma on the last item is completely valid , In some cases, it is encouraged .

The index of the list is from 0 At the beginning , Not from 1 At the beginning

2、 aggregate

Use Curly braces or  set  Function creation

num_set = {
1, 2, 3, 4, 5}
word_set = set(["spam", "eggs", "sausage"])

To create an empty set , You have to use set(), Such as {}  Is to create an empty dictionary .

A set is disorder Of , This means they cannot be indexed .

Collection cannot contain duplicate elements .

Due to the way of storage , Checking whether an item is part of a set is better than checking whether it is part of a list faster .

Use together  add  Additive elements  .

remove  Method to delete a specific element from the collection ; pop  Delete random elements .

3、 Tuples

Tuples Use parentheses establish , Can also be in the absence of parentheses Created in case of

words = ("spam", "eggs", "sausages",)
my_tuple = "one", "two", "three"

Create an empty tuple with empty bracket pairs .

Tuples are faster than lists , But tuples cannot be changed .

You can use indexes to access values in tuples .

4、 Dictionaries

A dictionary is a data structure used to map any key to a value

ages = {
"Dave": 24, "Mary": 42, "John": 58}

An empty dictionary is defined as {}.

Dictionaries Each element in the consists of a key : value Yes, to show .

Use Dictionaries [“ Key name ”] You can get the corresponding value .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved