Python [2]-list and metadata, python list

Source: Internet
Author: User
Tags python list

Python [2]-list and metadata, python list
I. Sequence

Python contains six built-in sequences: List, tuples, strings, unicode strings, buffer objects, and xrange objects.

The list can be modified, and the tuples cannot be modified.

Ii. List

The list is a variable-length sequence, and its content can be modified. Sequences are defined by square brackets [] or the list function, and can also be nested with sequences.

>>> a=['wang',15]
>>> print a
['wang', 15]
>>> b=['chen',18]
>>> print b
['chen', 18]
>>> students=[a,b]
>>> print students
[['wang', 15], ['chen', 18]]

Iii. General sequence operations 1. Index

All elements in the sequence are numbered and increase progressively from 0. If the index is negative, the count starts from the last element, and the number of the last element is-1.

>>> str='Hello World'
>>> str[0]
'H'
>>> str[-1]
'd'

In fact, strings can directly use indexes.

>>> 'Hello World'[0]
'H'

2. multipart

Fragments are used to extract a group of data in a sequence. There are three parameters: the first two parameters are index numbers, the first parameter is the number of the first element to be extracted, and the second parameter is the first element number of the remaining part After partitioning; the third parameter is the step size. That is to say, the first index number is included in the shard, and the second index is not included in the shard.

>>> numbers=[1,2,3,4,5,6,7,8,9,10]
>>> numbers[2:5]
[3, 4, 5]
>>> numbers[0:1]
[1]

If the part is included at the end of the sequence, leave the second index empty.

>>> numbers[-3:]
[8, 9, 10]

If the part starts from the beginning of the sequence, the first index is left empty.

>>> numbers[:5]
[1, 2, 3, 4, 5]

When the step is set to a number greater than 1, some elements are skipped.

>>> numbers[::2]
[1, 3, 5, 7, 9]
>>> numbers[::3]
[1, 4, 7, 10]

If the step size is negative, elements are extracted from the right to the left.

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

3. Add

You can use the plus sign to connect the sequence.

>>> [1,2,3]+[4,5,6]
[1, 2, 3, 4, 5, 6]
>>> ['a','b']+['b','c']
['a', 'b', 'b', 'c']
>>> [1,2]+['a','b']
[1, 2, 'a', 'b']
>>> [1,2]+'ab'
Traceback (most recent call last):
  File "<pyshell#30>", line 1, in <module>
    [1,2]+'ab'
TypeError: can only concatenate list (not "str") to list

4. Multiply

Multiply the number n by a sequence to generate a new sequence. The elements in the new sequence repeat the elements in the original sequence n times.

>>> 'hello'*5
'hellohellohellohellohello'
>>> [1,2,3,4,5]*3
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]

Create a sequence containing 10 Element Spaces, but each element in the sequence is empty.

>>> [None]*10
[None, None, None, None, None, None, None, None, None, None]

 

text=raw_input('please input text:')
screen_width=100
text_width=len(text)
box_width=text_width+2
left_margin=(screen_width-box_width)//2
print ' '*left_margin+'+'+'-'*box_width+'+'
print ' '*left_margin+'|'+' '*box_width+'|'
print ' '*left_margin+'| ' +text+''+' |'
print ' '*left_margin+'|'+' '*box_width+'|'
print ' '*left_margin+'+'+'-'*box_width+'+'

Iv. Basic list operations 1. Assignment operations

>>> num=[1,3,5]
>>> num[1]=10
>>> num
[1, 10, 5]

2. delete an element

>>> del num[1]
>>> num
[1, 5]

3. multipart assignment

Replace with sequences of different lengths

>>> num=[1,2,3,4,5]
>>> num[1:3]=[8,8,8,8,]
>>> num
[1, 8, 8, 8, 8, 4, 5]

4. insert parts

>>> num=[1,2,3,4,5]
>>> num[2:2]=[8,8,8]
>>> num
[1, 2, 8, 8, 8, 3, 4, 5]

5. delete part elements

>>> num=[1,2,3,4,5]
>>> num[2:4]=[]
>>> num
[1, 2, 5]

5. List Method

1.Append: Append an element to the end.

>>> num=[1,2,3]
>>> num.append(4)
>>> num.append(5)
>>> num
[1, 2, 3, 4, 5]

2.Count: Counts the number of times an element appears in the list.

>>> data=[1,4,[1,2],3,4,[1,2],[1,2,3]]
>>> data.count(4)
2
>>> data.count([1,2])
2

3.Extend: Append an element of another sequence at the end of the sequence. The extends method directly modifies the extended list.

>>> a=[1,2,3]
>>> b=[4,5,6]
>>> a.extend(b)
>>> a
[1, 2, 3, 4, 5, 6]
>>> c=['a','b','c']
>>> a.extend(c)
>>> a
[1, 2, 3, 4, 5, 6, 'a', 'b', 'c']

4.Index: Locate the first matching position of an element in the list.

>>> data=[1,2,33,5]
>>> data.index(3)
Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    data.index(3)
ValueError: 3 is not in list
>>> data.index(5)
3

5.Insert: Insert an element to a specified position in the list.

>>> data=[1,2,3,4,5]
>>> data.insert(2,88)
>>> data
[1, 2, 88, 3, 4, 5]

6. Pop: Remove the last value from the list and return the value of this element.

>>> data=[1,2,3,4,5]
>>> data.pop()
5

>>> data
[1, 2, 3, 4]

Pop (I): removes the specified element. I is the element index.

>>> data.pop(1)
2
>>> data
[1, 3, 4]

7. Remove: Removes the first match of a value in the list.

>>> data.remove(3)
>>> data
[1, 2, 4, 5]

8.Reverse: Put the elements in the list in reverse order.

>>> data=[2,5,8,10]
>>> data.reverse()
>>> data
[10, 8, 5, 2]

9.Sort: Sorts the elements in the list.

>>> data=[2,55,3,64,43,23]
>>> data.sort()
>>> data;
[2, 3, 23, 43, 55, 64]

Vi. tuples-unchangeable

The turple is a sequence that cannot be modified after initialization. For example:

t=(1,2,3)

If the tuples have only one numeric element, you must add a comma to avoid ambiguity.

t=(1,)

By calling the tuple method, you can convert any list or iterator into tuples. For example:

>>> tuple([1,2,3])

(1, 2, 3)

>>> tuple('hello')

('h', 'e', 'l', 'l', 'o')

Tuples can be connected to longer tuples through the plus sign (+.

>>> (1,2,3)+('a','b')

(1, 2, 3, 'a', 'b')

"Variable" tuples: each element of a tuples cannot be changed once it is initialized, but the object to which the tuples point is variable. For example:

l=[1,2,3]
t=('a',l)
print t

>>('a',[1,2,3])

a[1][0]=5

print t

>>('a',[5,2,3])

Tuples: If you assign values to the values of the tuples, Python will try to package the values of the tuples on the right. For example:

>>> t=(1,2,(3,4))

>>> a,b,c=t

>>> print(a)

1

>>> print(b)

2

>>> print(c)

(3, 4)

With the unpacking, you can easily switch between a and B. For example, you can directly use a, B = B, and.

>>> a=1

>>> b=2

>>> a,b=b,a

>>> print(a)

2

>>> print(b)

VII. range

The range function is used to generate an integer with an average interval. It has three parameters, which are used to specify the start value, end value, and step size. The use of the first two parameters is similar to that of the parts in the list, including the start value, not the end value.

For example:

>>> range(10)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> range(2,10)

[2, 3, 4, 5, 6, 7, 8, 9]

>>> range(1,10,2)

[1, 3, 5, 7, 9]

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.