Find the location of a value in the list (Python)

Source: Internet
Author: User
Tags python list

P=list.index (value)

List is the name of the listing

Value is a lookup

P is value in the location of the list

The following is quoted from: http://www.linuxidc.com/Linux/2012-01/51638.htm

Summary of Python3.2.2 list operations

List operation: Quickly create list, add Item, delete item, reassign item, reverse item order, retrieve item

A quick list is created in two ways: The Split method, the list function, and the range function work together.

Split method. Writes a string that separates the characters by a space, and then uses the Split method on the string.
A_list = ' A b c d e F g '. Spit ()//Create a list [' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G '], ' o ', ' GS ', but this is a much simpler style.

The list function is used in conjunction with the range function. You can quickly create a very large list.
A_list = List (range (100))//easy to create a 0 to 99 listing

New item, Four ways: concatenation, append, Extend, insert, the latter three ways are list of methods.
Example list a_list = [' A ']:
Concatenation added. It adds another list, and two lists are grouped together into a new list:
A_list = A_list + [2.0,3]//long list may consume a lot of memory

The Append method is added. It adds a Item,item type to the end of the original list that can be arbitrary:
A_list.append (' hello ')///Add an item of type string to the end of the original list
A_list.append ([' Hello '])//Add an item of type list at the end of the original list

The Extend method is added. It is similar to concatenation, accepts only list parameters, decomposes the item in the list, and then adds it to the original list:
A_list.extend (' hello ')//Add 5 character item to the end of the original list because it treats hello as a list
A_list.extend ([' Hello '])//Add 1 item to the end of the original list

Insert method Add. Insert item in the original list:
A_list.insert (0, ' C ')//Add a character to the 0-bit of the original list
A_list.insert (0.[' C ']////Add a list to the 0-bit of the original list


Delete Item, three ways: Del, remove, pop, the latter two ways are list of methods.
Example list: A_list = [' A ', ' B ', ' C ', ' Hello ']:
del Delete . It is deleted by index value or slice of item:
Del a_list[0]//delete the first value of the list
Del A_list[:2]//Deletes the first two values of the list. (Why not the top three?) Because the Python list slice contains the previous index but not the latter index

Remove method . It is not indexed by item, but is deleted according to the value of item:
A_list.remove (' a ')//Remove a from list

Pop method deletion . It is deleted by item index value, and the item value is deleted, and the last item is deleted by default if no index is specified:
A_list.pop (1)//deletes the second value of the list and returns the deleted value
A_list.pop ()//deletes the last value of the list and returns the value that was deleted


Re-assign the item and assign the value using the assignment symbol for the specified index:
Example list: A_list = [' A ', ' B ', ' C ', ' Hello ']:
A_LIST[1] = ' BBB '//second value B of the list, to be replaced by BBB


Reverse the item order of the list, reverse method:
Example list: A_list = [' A ', ' B ', ' C ', ' Hello ']:
The item order of the A_list.reverse ()//list will be rearranged from the back to the front, and changed to [' Hello ', ' C ', ' B ', ' a ']


Retrieves the value of the list in four ways: in, not in, count, index, and the latter two ways are the methods of the list.
Example list: A_list = [' A ', ' B ', ' C ', ' Hello ']:
Determines whether the value is in the list, in operator:
' A ' in a_list//Determine if value A is in the list and returns TRUE or False

Determines whether the value is not in the list, not in operator:
' A ' not in a_list//determining if A is not on the list and returns TRUE or False

Counts the number of times the specified value appears in the list, Count method:
A_list.count (' a ')//Returns the number of occurrences of a in the list

to view the location of the specified value in the list, index method:
a_list.index (' a ')   //Returns the location where a appears each time in the list, the default search entire list
A_ List.index (' A ', 0,3)  //Returns the first occurrence of a in the specified slice

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.