Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of lists, tuples, collections and dictionaries in python

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail what is the use of lists, tuples, collections and dictionaries in python. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

List list

A combination of sequential data that can be added, deleted, altered and checked

Assignment operation

Common function del ls [2]: delete

You can multiply a list by an integer to indicate that multiple lists are linked together

B = 2c = a * bprint (c) # the input result is as follows: ['axiaozhongjie 2jingmei 3jingmei 4Jing 5,' axiangjingdagomo 2jingjie3jingmei 4jingy5]

The double-layer list loop is used in the same way as dict, requiring that each list have only two values: for kline v in ls:

List connotation: quickly create a list

General function: len,min,max, other types of functions are also common

Append: add at the end, insert (index,data): insert before index

Pop () pops up the last one, returning the pop-up data of the value.

Remove: deletes the first element of the specified value in the list

Clear: clear the list and keep the id unchanged

Reverse (): reverse

Extend extended list a = [1] b = [2] a.extend (b) asides = [1pc2]

Count (): count the number of times a data appears

Copy is a shallow copy, that is, it is only a copy.

Ls = list (), ls = []

The subscript starts at 0, and the last number can be accessed using-1, and so on.

You can use its slicing operation [:] and arrange it in reverse order [::-1]

The original ID of the new list after slicing and copy () is inconsistent with the original id. Other methods:

Assign\ append\ del\ and other id is consistent with the original list

The assignment operation is to pass address, and copy is to pass value.

A = [x for x in range (1pime35)] # generate a list from 1 to 34 # generate a new list of all the even numbers in a bb = [m for m in an if m% 2 = = 0] print (b) #.. c = [for m in a for n in b if mechn250] print (c) # the difference between deep copy and shallow copy # the reason for the following problems is that the copy function is a shallow copy function That is, only one layer of content needs to be copied # A specific tool is required for deep copying: a = [1dje 2je 3, [10,20,30]] b = a.copy () print (id (a)) print (id (b)) print (id (a [3])) print (id (b [3])) a [3] [2] = 666print (a) print (b) # the output is as follows: 1402494083668140249236040 It also points to the same address 140249409236232149409236232 [1, 2, 3, [10, 20, 666]] [1, 2, 3, [10, 20, 666]] tuple-tuple

Tuples can be thought of as immutable list

Properties:

It's a sequence table, ordered.

Tuple data values can be accessed and cannot be modified or deleted, which means that the content cannot be modified. Refer to the following example

Tuple data can be of any type

All the features of list, except for modifiable tuples, have

In other words, list has exactly the same operations, such as indexing, slicing, sequence addition, multiplication, membership operations, etc.

# tuple addition T1 = (1meme 2mage3) T2 = (5jingo 6mag7) # addressing operation print (T1) print (id (T1)) T1 = T1 + t2print (T1) print (id (T1)) # tuple is unmodifiable, which refers to the immutability of content # modifying and deleting tuple content will result in an error T1 [1] = 100del (T1 [2]) # tuple multiplied T2 = T2 * 2 collection-set

Set is a concept in high school mathematics.

A pile of determined, unordered and unique data, each of which becomes an element in the collection

Properties:

The data in the collection is out of order, that is, indexes and fragments cannot be used.

The data elements within the collection are unique and can be used to eliminate duplicate data.

The data in the collection, such as str, int, float, tuple, frozen set, etc., that is, only hashable data can be placed internally.

The connotation of set

If there is duplicate data in the definition, the duplicate data is automatically filtered.

S = {3magin 1, 2, 3, 3, 4, 3, 2, 3, 3, 3, 4, 4, 3)

S = = {1pm 2pm 3pm 4}

# the connotation of the set of multiple cycles S1 = {1 for m in 2, 3 for n in 4} S2 = {"I", "am", "stu"} s = {munin for m in S2 for n in S1} print (s) s = {munin for m in S2 for n in S1 if n = 2} print (s) set

Add (), add data

Copy (), copy

Remove: remove the specified value and change the original value directly. If the value to be deleted does not exist, error: s.remove (1)

Discard: removes the specified value from the collection, which is the same as remove, but does not report an error if you want to delete shares: s.discard (1)

Pop pops up an element at random

Set function

# intersection: intersection # difference: difference is equal to S1-slotted union: Union # issubset: check whether a collection is another subset # issuperset: check whether a collection is another superset S1 = {1Yue2Jing 3Jing 4Jing 6} S2 = {5JEI 6pm 7pm 8pm 9} sq1 = s1.intersection (S2) print (swatches 1) scuttle 2 = s1.difference (S2) print (swatches 2) swatches 3 = s1.issubset (S2) print (swatches 3)

Fronzenset: frozen collection

A special set: a collection that cannot be modified.

S = frozenset ()

Dictionary-dict

A dictionary is an unordered combination of data that appears in key-value pairs.

Create:

# create a dictionary with values, with each set of data separated by colons Each key-value pair is separated by a comma d = {"one": 1, "two": 2, "three": 3} print (d) d = dict ({"one": 1, "two": 2, "three": 3}) print (d) # using the keyword parameter d = dict (one=1, two=2, three=3) print (d) # d = dict ([("one", 1), ("two", 2), ("three") 3)]) print (d ['one'])

Properties:

The dictionary is a sequence type, but it is an unordered sequence, so there are no fragments and indexes

Each of the data in the dictionary consists of key-value pairs, that is, kv pairs.

Key: must be a hashable value, such as int,string,float,tuple, but not list,set,dict

Value: any valu

Dictionary commonly used access d = {"one": 1, "two": 2, "three": 3} # use for loop Access for k in d directly according to the key value: print (k, d [k]) # the above code can be rewritten as follows: for k in d.keys (): print (k, d [k]) # access only the dictionary value for v in d.values (): print (v) # Note the following special usage: for klore v in d.items (): print (kmaehashi, Mustang) dictionary common functions

Keys (): a structure that returns the keys of the dictionary

Values (): a structure that returns the values of a dictionary

Items (): returns the tuple format of the dictionary's key-value pairs

Get (): returns the corresponding value according to the specified key, and the default value can be set.

D.get ('one',100)

Fromkeys uses the specified sequence as the key and a value as the value of all the keys in the dictionary

L = ["eins", "zwei", "drei"] # Note the type of two parameters fromkeys # Note the calling body of fromkeys d = dict.fromkeys (l, "h") print (d) this is the end of the article on "what is the use of lists, tuples, collections and dictionaries in python". I hope the above can be helpful to you, so that you can learn more knowledge, if you think the article is good. Please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report