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 are the similarities and differences between Python lists, tuples, and dictionaries

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

Share

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

This article focuses on "what are the similarities and differences between Python lists, tuples, and dictionaries". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the similarities and differences between Python lists, tuples, and dictionaries?"

List: []

Features: modifiable

Basic operations:

Index []

Slice []

Append append

Delete del

Length len

Include in

Eg:

> name_list = ('hello','jacky','dick')

> delname_list [0]

Traceback (mostrecent call last):

File "", line 1, in

TypeError:'tuple' object doesn't support item deletion

> type (name_list)

> name_list = ['jacky','dick','hello']

> id (name_list)

35733128L

> name_list.append ('hu')

> name_list

['jacky',' dick','hello', 'hu']

> id (name_list)

35733128L

> delname_list [0]

> name_list

['dick',' hello','hu']

How to turn a list into a string:

> "_" .join (name_list)

'dick_hello_hu'

> len (name_list)

three

> name_list

['dick',' hello','hu']

> > "jacky" in name_list

False

Tuple: ()

Features: unmodifiable

Basic operations:

Index []

Slice []

Length len

Include in

['dick','hello',' hu'] list

('dick',' hello','hu') tuple

[

'dick', ('jacky','kirk')

]

The list contains two elements:

First: string

Second: tuple

Add, delete and modify: minimum unit

# slice, index (- 1), length, contain, loop

Str: re-open up memory space

List: use the original memory space after modification

Tuple: modification is not allowed

Name_list = ['liu','zhang','huang']

For ele in name_list

Print ele

# 1 minute ele = liu

# 2 dint ele = zhang

# 3 minute ele = huang

If ele = 'zhang':

Print 'sb'

# this cycle will not continue

Continue

If ele = 'liu':

Print 'find'

Break

# jump out of the whole cycle

# for

# continue

# break

While condition:

Print 'xxx'

Condition = true true: loop

Condition = false false: no loop

While True:

Print 'xxxx'

Flag = True

While flag:

Print 'jjjj'

Flag = False

Print 'vvvv'

Dict, dictionary, key-value pair

Person = {

"name": 'dick'

"age": 18

"gender": 'man'

}

# each element is a key-value pair (key and value)

The dictionary can also be found through the "index", corresponding to the keys of the dictionary:

Person ['name']

Dict_test.py:

Person = {

"name": "dick"

"age": 29

"gerder": 'man'

}

# person.keys () # all key are taken out and saved as a list

# person.values () # all value are taken out and saved as a list

# person.items () # all elements # all key-value pairs

For KJO v in person.items ():

Print k

Print v

Print'='

Execution result:

E:\ > pythondict_test.py

Gender

Man

=

Age

twenty-nine

=

Name

Dick

=

Dictionary disorder, special for loop

Keys ()-- > list

Values ()-- > list

Items ()-- > when only the for loop is used, the element is assigned

At this point, I believe you have a deeper understanding of "what are the similarities and differences between Python lists, tuples and dictionaries". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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