Python Learning notes list, tuples, dictionaries (1)

Source: Internet
Author: User

1, the most basic data structure: sequence

A, any sequence begins with a 0 serial number (also an index);

As follows: The first element is ' H '

>>> a = ' Hello '
>>> A[0]
H

b, sequence of operations:

Shard: A[1:5] #表示从第2个元素到第6个元素, but not including the 6th element

A[-3:-1] #表示从倒数第3个元素到倒数第一个元素, but excluding the first element of the countdown

A[-3:] #表示从倒数第三个元素到最后一个元素, you can take the last element

a[:] #表示a的全部元素

A[1:9:2] #表示从第2个元素到第10个元素, step is 2

As follows:

>>> a = [1,2,3,4,5,6,7,8,9,10]
>>> A[1:5]
[2, 3, 4, 5]
>>> A[-3:-1]
[8, 9]
>>> a[-3:]
[8, 9, 10]
>>> a[:]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> A[1:9:2]
[2, 4, 6, 8]

Addition:

>>> ' python ' + ' + ' selenium '
' Python selenium '

Multiplication:

>>> ' python ' * 3
' Pythonpythonpython '

Membership: In

>>> ' i ' in ' Python '
False
>>> ' py ' in ' python '
True

2. List

The list of individuals feels like a sequence: the main things to keep in mind are list operations and some methods

List function, you can convert the sequence into lists

>>> a = list (' Hello ')
>>> A
[' H ', ' e ', ' l ', ' l ', ' O ']

Del #删除列表里的元素

>>> A
[' H ', ' e ', ' l ', ' l ', ' O ']
>>> del A[0]
>>> A
[' E ', ' l ', ' l ', ' O ']

Append #在列表末尾追加对象

>>> a.append (' H ')

>>> A
[' E ', ' l ', ' l ', ' o ', ' H ']

Count #统计某个元素在列表中出现的次数

>>> a.count (' l ')
2

Extend #在列表末尾一次性追加另一个列表的多个值

>>> b = List (' World ')
>>> a.extend (B[1:3])
>>> A
[' E ', ' l ', ' l ', ' o ', ' H ', ' o ', ' R ']

Index #从列表中找出某个值的第一个匹配项的索引位置

>>> a.index (' O ')
3

Insert #将对象插入列表中

>>> A.insert (4, ' W ')
>>> A
[' E ', ' l ', ' l ', ' o ', ' W ', ' H ', ' o ', ' R ']

Pop #从移除列表中的一个元素 (the last one by default)

>>> A.pop ()
' R '
>>> A
[' E ', ' l ', ' l ', ' o ', ' W ', ' H ', ' O ']
>>> A.pop (4)
W
>>> A
[' E ', ' l ', ' l ', ' o ', ' H ', ' O ']

Remove #移除列表中某个值的第一个匹配项

>>> a.remove (' O ')
>>> A
[' E ', ' l ', ' l ', ' H ', ' O ']

Reverse #将列表中的元素反向存放

>>> A.reverse ()
>>> A
[' O ', ' H ', ' l ', ' l ', ' e ']

Sort #用于在原位置对列表进行排序 (modifies the list) and can be sorted according to the key function

>>> A.sort ()
>>> A
[' H ', ' e ', ' l ', ' l ', ' O ']

>>> b = [' Hello ', ' Hi ', ' See You ']
>>> b.sort (key = len)
>>> b
[' Hi ', ' hello ', ' See You ']

Shard Assignment:

>>> A
[' H ', ' e ', ' l ', ' l ', ' O ']
>>> A[1:3] = [3,4]
>>> A
[' H ', 3, 4, ' l ', ' O ']

3, meta-group

Tuples are immutable sequences, tuples cannot be modified; creating tuples is simple, and the value is a comma.

>>> A = 3,
>>> A
(3,)
>>> A =
>>> A
(1, 2, 3)

The main factors to remember are:

The tuple function converts a sequence to a tuple

>>> a = ' Hello '
>>> Tuple (a)
(' H ', ' e ', ' l ', ' l ', ' o ')

Python Learning notes list, tuples, dictionaries (1)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.