Python tuple list Dictionary base analysis:

Source: Internet
Author: User

One, the meta-group

1. Definition of tuples, creating

tuple = ('apple','banana','Grange' ,'orange','anguo','C + +  ')

Note: The tuple is comprised of 6 elements, separated by commas.

tuple = ('apple',)

Note: One element followed by a comma

2. Meta-group access

(1) The index is a number in each other's brackets, and the index is also called subscript.

 tuple = ( " apple  , "  banana  , "  grange  , "  orange  , "  anguo  , "  c++   " )  print  (tuple[1 ])  print  (tuple[2 ])  print  (tuple[3]) 

Note: Tuples cannot add or remove any elements.

(2) Gets the value of the tuple (negative index or tile index)

Second, List

1, the definition of the list, create,

list = ['apple','banana','Grange' ,'orange','anguo','C + +  ']

Note: addition and deletion of the change

List = ['Apple','Banana','Grange','Orange','Anguo','C + +']list.append ("C") at the end of the add element List.insert (1,"C") to insert an element into the list list.remove ("Anguo") Delete this element List.pop (1) to delete the specified element list[1]="Php"Add an ordinal equal to the modified value

2, the use of the list

(1) List and tuple are very similar, support negative index, Shard, multivariate list

names=['Michale','Bob','TRACY'] forIinchnames:Print(i) Note: Circular output List Names=('Michale','Bob','TRACY') forIinchnames:Print(i) Note: cyclic output tuples

(2) Find, sort, reverse

Find (2 ways). The index method returns the position of the element in the list, and "in" to determine whether the element is in the list.

List = ['Apple','Banana','Grange','Orange','Anguo','C + +']Print(List.index ('Apple'))Print(List.index ('C + +') Note: The index of the printed element is also:Print("Grange"inchlist) Note: Judging'Grange'is in the list

Sort, invert

List = ['Apple','Banana','Grange','Orange','Anguo','C + +']list.sort ()Print("Print this result:", List) Note: Sort sortlist.reverse ()Print("Print this result:", List) Note: Invert reverse

Third, the dictionary

Create, Access

dict={"a":"Python","b":"Php","C":"Jave","D":"C","e":"C + +"}Print(dict["a"]) Note: Get the corresponding value "Python" by Index "a": Tuple and list index are numbers. The dictionary is the "key" index, and the "key" index can be written as a number

Dictionary loop output:

dict={"a":"Python","b":"Php","C":"Jave","D":"C","e":"C + +"}Print(dict["a"]) forKvinchDict.items ():Print("the dictionary output is"K'-', v) Note the answer: The dictionary output is a-the python dictionary output is e-C + +the dictionary output is C-Jave Dictionary output to D-c dictionary output to B-PHP

Python tuple list Dictionary base analysis:

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.