Python Learning notes--when indexing doesn't work

Source: Internet
Author: User
Tags stdin string format

Dictionary (dict)
phonebook={' cc ': ' 12334 ', ' dd ': ' 123443 '}

>> phonebook (' cc ')
Traceback (most recent call last):
File "<stdin>", line 1, in <mo Dule>
TypeError: ' Dict ' object is not callable
>> phonebook (CC)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Nameerror:name ' CC ' was not defined
>> s=dict (phonebook)
& gt;> s
{' cc ': ' 12334 ', ' dd ': ' 123443 '}
>> s (cc)
Traceback (most recent call last):
File "<stdin& gt; ", line 1, in <module>
Nameerror:name ' CC ' isn't defined
>> s (' cc ')
Traceback (most recent call Last):
File "<stdin>", line 1, in <module>
TypeError: ' Dict ' object was not callable
>> Phonebo OK
{' cc ': ' 12334 ', ' dd ': ' 123443 '}
>> phonebook (' cc ')
Traceback (most recent call last):
File "< Stdin> ", line 1, in <module>
TypeError: ' Dict ' object was not callable

phonebook[' CC ']
' 12334 '
dict function: Create a dictionary from other mappings or key-value pairs of sequences
items=[(' name ', LX), (' Age ', 26)]
Traceback (most recent):
File "<stdin>", line 1, in <module>
Nameerror:name ' LX ' is not defined

>> items=[(' name ', ' LX '), (' Age ', 26)]
>> c=dict (items)
>> C
{' name ': ' LX ', ' Age ': 26}

D=dict ()

>> D
{}
If no dictionary is defined, an empty dictionary is returned

Dict Basic Operation
Len (d): Returns the number of items contained in D
D[K]: Returns the K value in D
D[K]=V: Assigning value to K in D
Del D[k]: Delete
K in D: query for K keys in D
Elements that are not in dict can also be added by external direct assignment, unlike list needs append

>> d[10]= ' KK '
>> D
{ten: ' KK '}

>> x=[]
>> x[30]= ' KK '
Traceback (most recent):
File "<stdin>", line 1, in <module>
Indexerror:list Assignment index out of range
>> x={}
>> x[30]= ' KK '
>> x
{: ' KK '}
A little piece of practice in the book
people={
' LX ': {
' Phone ': ' 123 ',
' Addr ': ' FDFSF '
},
' Sdy ': {
' Phone ': ' 456 ',
' Addr ': ' Test '
},
' SB ': {
' Phone ': ' 789 ',
' Addr ': ' Test1 '
}

}
labels={' phone ': ' PhoneNumber ',
' Addr ': ' Address '
}
Name=input (' Name: ')
R=input (' Please input number (p) or address (a) ')
If r== ' P ': key= ' phone '
If r== ' a ': key= ' addr '
If name in People:print ("{} ' s {} is {}". Format (Name,labels[key],people[name][key]))
String format feature for Dict:format_map ()
Query keyword corresponding to replace

Dictionary method:
Clear: Clears all items in Dict

>> d={}
>> D
{}
>> d{' name '}= ' LX '
File "<stdin>", line 1
d{' name '}= ' LX '
^
Syntaxerror:invalid syntax
>> d[' name ']= ' LX '
>> D
{' name ': ' LX '}
>> C{}=d.clear ()
File "<stdin>", line 1
C{}=d.clear ()
^
Syntaxerror:invalid syntax
>> C{}.clear ()
File "<stdin>", line 1
C{}.clear ()
^
Syntaxerror:invalid syntax
>> C=d.clear ()
>> C
>> D
{}
Copy: Copying from a dict to B dict
>> x={' name ': ' LX ', ' age ': ' + ', ' height ': ' 170 '}
>> y=x.copy ()
>> y
{' name ': ' LX ', ' age ': ' + ', ' height ': '}### ' #浅复制
The original will not be affected if the value in the replacement copy is taken, and the original value in the in-place modified copy will also be modified
>> x={' name ': ' LX ', ' age ': ' + ', ' height ': [' 170 ', ' 180 ', ' 190 ']}
>> x
{' name ': ' LX ', ' age ': ' + ', ' height ': [' 170 ', ' 180 ', ' 190 ']}
>> y
{' name ': ' xx ', ' age ': ' + ', ' height ': ' 170 '}
>> y=x.copy ()
>> y
{' name ': ' LX ', ' age ': ' + ', ' height ': [' 170 ', ' 180 ', ' 190 ']}
>> y[' height '].remove (' 190 ')
>> y
{' name ': ' LX ', ' age ': ' + ', ' height ': [' 170 ', ' 180 ']}
>> x
{' name ': ' LX ', ' age ': ' + ', ' height ': [' 170 ', ' 180 ']}
>> y[' name ']= ' CC '
>> y
{' Name ': ' CC ', ' age ': ' + ', ' height ': [' 170 ', ' 180 ']}
>> x
{' name ': ' LX ', ' age ': ' + ', ' height ': [' 170 ', ' 180 ']}

Deepcopy: Deep copy, changes in copy do not affect originals

>> from copy import deepcopy
>> d={}
>> D
{}
>> d[' name ']=[' lx ', ' CC '
>> D
{' name ': [' lx ', ' CC ']}
>> c=d.copy ()
>> C
{' name ': [' lx ', ' CC ']}
>> dc=d.deepcopy ()
Traceback (most recent):
File "<stdin>", line 1, in <module>
Attributeerror: ' Dict ' object has no attribute ' deepcopy '
>> Dc=deepcopy (d)
>> d[' name '].append (' DD ')
>> C
{' name ': [' lx ', ' cc ', ' DD ']}
>> DC
{' name ': [' lx ', ' CC ']}
>>

Fromkey: Create a new dict with none

>> Dict.fromkeys ([' Name ', ' age ', ' height '])
{' name ': None, ' age ': none, ' Height ': none}

Get: Provides a relaxed environment

>> c={}
>> print (c[' name ')
Traceback (most recent):
File "<stdin>", line 1, in <module>
Keyerror: ' Name '
>> Print (c.get (' name '))
>> c.get (' name ', N/a)
Traceback (most recent):
File "<stdin>", line 1, in <module>
Nameerror:name ' N ' is not defined
>> c.get (' name ', ' N/A ')
' N/A '

Python Learning notes--when indexing doesn't work

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.