Python list, tuple, dict, set comparison

Source: Internet
Author: User
Tags python list


List Tuple Dict Set
Characteristics Order, find speed gradually as the element increases Ordered \ cannot be modified Unordered, Find Fast, key cannot be duplicated Elements are not duplicated, unordered, and determine whether an element is fast in set
Create L =[' Michael ', ' Bob ', ' Tracy ' t = (' Adam ', ' Lisa ', ' Bart ')
t = (1,)
t = (' A ', ' B ', [' A ', ' B '])
D = {
' Adam ': 95,
' Lisa ': 85,
' Bart ': 59
}
s = set ([' A ', ' B ', ' C '])
Access L[0], l[-1] T[0], t[-1] Access via key
If ' Paul ' in D:
Print d[' Paul '
Or
D.get (' Paul ')
x = '??? ' # user-entered string
If x in weekdays:
print ' Input ok '
Else
print ' input error '
adding elements Tail add l.append (' Paul ')
Inserts the specified index position l.insert (0, ' Paul ')
No d[' Paul '] = 72 S.add (4)
Delete Element Delete last element and return L.pop ()
Deletes an element of the specified index L.pop (2)
No
S.remove (4)
Replace element L[2] = ' Paul '
L[-1] = ' Paul '
No

The empty
t = ()

Number of elements

Len (d)
Traverse

For key in D:
... print Key
For name in S:
... print Name
Slice Take the first 3 elements L[0:3] starting at index 0, until index 3, but not including index 3;
If the first index is 0, you can omit to write l[:3];
The third parameter indicates that each n takes one, on L[::2]



Iteration L = [' Adam ', ' Lisa ', ' Bart ', ' Paul ']
>>> for index, name in enumerate (L):
... print index, '-', name

D = {' Adam ': +, ' Lisa ': $, ' Bart ': 59}
Print d.values ()
# [85, 95, 59]
For V in D.values ():
Print V
# 85
# 95
# 59
Or
D = {' Adam ': +, ' Lisa ': $, ' Bart ': 59}
Print d.itervalues ()
# <dictionary-valueiterator Object at 0x106adbb50>
For V in D.itervalues ():
Print V
# 85
# 95
# 59
Or for key, value in D.items ():
... print key, ': ', value
...
Lisa:85
Adam:95
bart:59


This article is from the "No Progress is Backward" blog, please be sure to keep this source http://janetria.blog.51cto.com/10509492/1913237

Python list, tuple, dict, set comparison

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.