SoFunction
Updated on 2024-11-16

Comprehensive understanding of python strings and dictionaries

Many sequences of method strings apply equally well, the
However, strings are immutable, so some methods that try to change them are not available

1 String formatting

1) Formatting strings with tuples or dictionaries

format = "hello,%% enough for you?"
values = ('world','Hot')
format % values

Similar to C formatting

2) Template String

The string module provides template strings to format strings.
from string import Template
s = Template(x,gloriousx,gloriousx!)
(x = 'slurm')
Replace x with slurm
A few detailed tips to use


Formatting conversion types, field width precision, sign bit, alignment, padding, etc. Attend some manuals!


2 String method

1)find

Find substring, return leftmost index
(subs)

2)join
connection string

3)lower

4)replace
All matches are replaced

5)split
Split String Separator Not Contained
.........
..........

--------------------------------------------------

The values in the dictionary are in no particular order

key can be a number, string, tuple (must be immutable, not a list)

phonebook = {'jmz':'5153','usr1':'1234','usr2':'4321'}

1) The dict function

Build dictionaries by other mappings or sequences of keys and values:
Create a dictionary with a list containing 2 tuples:

items = [('key1','value1'),('key2','value2'),('key3','value3')]
d = dict(items)
{'key1':'value1','key2':'value2','key3':'value3'} Probably not in that order.

Create dictionaries by keyword arguments:
d = dict(key1 = 'value1',key2 = 'value2',key3 = 'value3')
The above result will also be obtained

The dict function is not really a function, it is a type, similar to list, tuple, str.

2) Basic dictionary operations

Suppose d is a dictionary:
len(d) dictionary length
d[key] the value of key key in the dictionary
d[key] = value Assign value (add automatically if key does not exist)
del d[key] Delete the item whose key is key
key in d Check if it is in the dictionary

3) Dictionaries can also be used to format

Add the key (enclosed in parentheses) to each conversion descriptor (%) followed by the other descriptors:
Example: %(value)s
 phonebook = {'jmz':'5153','usr1':'10086'}
 "jmz's phone number is %(jmz)s." % phonebook
In this way any number of conversion descriptors can be obtained as long as the given key can be found in the dictionary.

4) Some dictionary methods
clear:
In-place operation (no return value), clears all entries in the dictionary

copy: shallow and deep copy
y = ()
y = deepcopy(x)

fromkeys:
Creates a new dictionary with the given key: the default value is None
 >>>{}.fromkeys(['key1','key2'])
 >>>{'key1':None,'key2':None}
Or:
 >>>(['key1','key2'])
The same effect.

get:
Generally speaking, the dictionary item that the model text is good at will be wrong, for example:
 >>>print d[name]
(indicates contrast)
>>>print ('name') will return None by default.

has_key:
Returns True or False
 >>>d.has_key('jmz')

items and iteritems:
The items method returns the dictionary items as a list method:
d = {'key1':'value1','key2':'value2'}
 >>>()
 >>>[('key1','value1'),('key2','value2')]
iteritems returns an iteration over items.
>>>it = () #it is an iterator object for the above list
>>>list(it)# can convert an iterator to a list
 >>>[('key1','value1'),('key2','value2')]   
 
keys and iterkeys:
Returns the keys as a list, an iterator of the keys

pop:
('key') has a return value and removes the

popitem:
() pops up random items, since the dictionary is unordered

setdefault:
 >>>('key','default_value')
When the key does not exist, return the default value and update the dictionary, if the key already exists, return its value and do not update the dictionary, which is equivalent to this sentence has no effect

update:
Use one dictionary to update another:
 >>>(dd)
Add the items from dd to d and overwrite if duplicated.

values and itervalues:
Returns a list of values, an iterator over the list of values.

This above comprehensive understanding of python strings and dictionaries is all that I have shared with you.