Python<1> List

Source: Internet
Author: User

The elements in the list are separated by commas, surrounded by [], and the types of the elements are arbitrarily

The official point is thatthe list is an ordered collection of positions related to a random type of object.

It does not have a fixed size (1). By the offset

(2) make Assignments and other species List of methods , you can change the size of the list .

(1) Although the list does not have a fixed size. Python still does not agree to referencing an element that does not exist, and an index outside the end of the list causes an error. The assignment is also.

(2) We can index the list by an offset. Slice, and the index value can be negative.

Main properties of List

<1> ordered collection of random objects (left to right order)

<2> read by offset

<3> variable-length, heterogeneous, and discretionary nesting (a child list that can create a list of sub-lists ...) )

<4> is a classification of mutable sequences (which can be changed in place, and sequence operations work in lists and strings in the same way.) The difference is that the list is mutable. To run

Operations such as delete, index assignment, and so on. String immutable, cannot run these)

<5> Object Reference Array

Use list operations frequently

<1>l=[] An empty list l=list ()

<2>l=[0,1,2,3] Four items: Index 0-3

<3>l=[1,[2,3]] Nested a list of subforms

<4>l[i] l[i:j] Len (L) index, shard, seek length

<5>l1+l2 Merging

<6>l * 3 repeated 3 times

<7>for x in L:print x iterative printing

<8>l.append (x) insertion

<9>l.extend ([x, Y, z]) growth

<10>l.insert (i,x) Insert X at I

<11>l.count (x) returns the number of occurrences of X

<12>l.sort () sort

<13>l.reverse Anti-

<14>del L[k] Delete

<15>del L[m,n]

<16>l.pop ()

<17>l.remove (x)

<18>l[i:j]=[]

<19>x in L

The range () and split () functions all get a list

L=range ( -5,5), l=[-5,-4,-3,-2,-1,0,1,2,3,4]

L= ' This is a test '. Split ()--l=[' This ', ' is ', ' a ', ' test ']

Note:

The index of {1}, the assignment of the Shard is directly changed directly on the original list.

such as the assignment of the <18> Shard can be seen as (3) deleting the original slice. Then insert the data.

Note: When the value of the assignment and the Shard overlap, a specific analysis is required

For example L[2:5] = L[3:6] is feasible, before the deletion of 2:5 has put 3:6 of the data, delete and then insert 2:5.

(3) This is not the case, but it helps you understand why the number of deletions does not have to be equal to the number of insertions.

For example l=[1,2,3,4] l[1:2]=[4,5] At this time l=[1,4,5,3,4] l[1:2]=[] is to remove the second item

The result of {2}l.append (x) and l+[x] is similar, but the former is changed in the same place, and the latter generates a new list.

{3} for sort, we can change the sort behavior by passing in the keyword parameter. In the 2.6 and previous version numbers, different types of data can also be

Sort of. This order is determined by the type name. Like what. All integers are smaller than the entire string: Wait a minute. It's not the same in 3.0. Different types

The sort exception occurs.

{4} Be careful that both append and sort are directly changing the list in situ, returning to none. Suppose you edit a statement like L=l.append (x). Will not get

L changed the value (actually lose the entire list of references), so there is no reason to assign the value again.

The value of the {5} shard [I:j]. Contains the left boundary and does not contain the right boundary.

l=[0,1,2] l[1:]=[], l=[0] l[0]=[]->l=[[]

Python also contains a more advanced operation called a list resolution expression because it is more complex. Write a separate note later.

The implementation command line specifies a numeric file. Output maximum and minimum values

Import sys   #为了获得命令行參数if len (sys.argv)! = 2    print "Please supply a filename"    raise Systemexit (1) f = open ( SYS.ARGV[1])  lines = F.readlines ()       #打开文件 all input rows exist in a string list f.closefvalues=[float (line) for lines in  lines]     #全部字符串循环, and a new list of each element, float, print "The minimum value is", min (fvalues)               print "The maximum value is", Max (Fvalu ES)       #通过内置函数得到最大值和最小值


References <<learning python>>

Python&lt;1&gt; List

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.