Flashield's Blog

Just For My Daily Diary

Flashield's Blog

Just For My Daily Diary

06.course-strings-and-dictionaries【字符串及字典】

This lesson will cover two essential Python types: strings and dictionaries.

本课程将介绍两种基本的 Python 类型:字符串字典

Strings

字符串

One place where the Python language really shines is in the manipulation of strings.
This section will cover some of Python's built-in string methods and formatting operations.

Such string manipulation patterns come up often in the context of data science work.

String syntax

You've already seen plenty of strings in examples during the previous lessons, but just to recap, strings in Python can be defined using either single or double quotations. They are functionally equivalent.

Python 语言真正闪光的地方之一是字符串的操作。
本节将介绍Python的一些内置字符串方法和格式化操作。

这种字符串操作模式经常出现在数据科学工作的背景下。

字符串语法

在前面的课程中,您已经在示例中看到了大量字符串,但回顾一下,Python 中的字符串可以使用单引号或双引号来定义。 它们在功能上是等效的。

x = 'Pluto is a planet'
y = "Pluto is a planet"
x == y
True

Double quotes are convenient if your string contains a single quote character (e.g. representing an apostrophe).

Similarly, it's easy to create a string that contains double-quotes if you wrap it in single quotes:

如果您的字符串包含单引号字符(例如表示撇号),则双引号很方便。

同样,如果将其用单引号括起来,则可以轻松创建包含双引号的字符串:

print("Pluto's a planet!")
print('My dog is named "Pluto"')
Pluto's a planet!
My dog is named "Pluto"

If we try to put a single quote character inside a single-quoted string, Python gets confused:

如果我们尝试在单引号字符串中放入单引号字符,Python 会感到困惑:

'Pluto's a planet!'
  Cell In[3], line 1
    'Pluto's a planet!'
                      ^
SyntaxError: unterminated string literal (detected at line 1)

We can fix this by "escaping" the single quote with a backslash.

我们可以通过用反斜杠“转义”单引号来解决这个问题。

'Pluto\'s a planet!'
"Pluto's a planet!"

The table below summarizes some important uses of the backslash character.

下表总结了反斜杠字符的一些重要用途。

What you type... What you get example print(example)
\' ' 'What\'s up?' What's up?
\" " "That's \"cool\"" That's "cool"
\\ \ "Look, a mountain: /\\" Look, a mountain: /\
\n "1\n2 3" 1
2 3

The last sequence, \n, represents the newline character. It causes Python to start a new line.

最后一个序列\n表示换行符。 它使 Python 开始一个新行。

hello = "hello\nworld"
print(hello)
hello
world

In addition, Python's triple quote syntax for strings lets us include newlines literally (i.e. by just hitting 'Enter' on our keyboard, rather than using the special '\n' sequence). We've already seen this in the docstrings we use to document our functions, but we can use them anywhere we want to define a string.

此外,Python 的字符串三引号语法允许我们按字面意思包含换行符(即只需按键盘上的Enter,而不是使用特殊的\n序列)。 我们已经在用于记录函数的文档字符串中看到了这一点,但是我们可以在任何想要定义字符串的地方使用它们。

triplequoted_hello = """hello
world"""
print(triplequoted_hello)
triplequoted_hello == hello
hello
world

True

The print() function automatically adds a newline character unless we specify a value for the keyword argument end other than the default value of '\n':

print() 函数会自动添加换行符,除非我们为关键字参数 end 指定一个值,而不是默认值 '\n'

print("hello")
print("world")
print("hello", end='')
print("pluto", end='')
hello
world
hellopluto

Strings are sequences

Strings can be thought of as sequences of characters. Almost everything we've seen that we can do to a list, we can also do to a string.

字符串是序列

字符串可以被认为是字符的序列。 几乎我们所看到的可以对列表执行的所有操作也可以对字符串执行。

# Indexing
planet = 'Pluto'
planet[0]
'P'
# Slicing
planet[-3:]
'uto'
# How long is this string?
len(planet)
5
# Yes, we can even loop over them
[char+'! ' for char in planet]
['P! ', 'l! ', 'u! ', 't! ', 'o! ']

But a major way in which they differ from lists is that they are immutable. We can't modify them.

但它们与列表的一个主要区别是它们是不可变的。 我们无法修改它们。

planet[0] = 'B'
# planet.append doesn't work either
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

Cell In[12], line 1
----> 1 planet[0] = 'B'
      2 # planet.append doesn't work either

TypeError: 'str' object does not support item assignment

String methods

Like list, the type str has lots of very useful methods. I'll show just a few examples here.

字符串方法

list一样,str类型有许多非常有用的方法。 我在这里仅举几个例子。

# ALL CAPS
claim = "Pluto is a planet!"
claim.upper()
'PLUTO IS A PLANET!'
# all lowercase
claim.lower()
'pluto is a planet!'
# Searching for the first index of a substring
claim.index('plan')
11
claim.startswith(planet)
True
# false because of missing exclamation mark
claim.endswith('planet')
False

Going between strings and lists: .split() and .join()

str.split() turns a string into a list of smaller strings, breaking on whitespace by default. This is super useful for taking you from one big string to a list of words.

在字符串和列表之间切换:.split().join()

str.split() 将字符串转换为较小字符串的列表,默认情况下会在空格处中断。 这对于从一个大字符串到一个单词列表非常有用。

words = claim.split()
words
['Pluto', 'is', 'a', 'planet!']

Occasionally you'll want to split on something other than whitespace:

有时你会想在空格以外的地方进行分割:

datestr = '1956-01-31'
year, month, day = datestr.split('-')

str.join() takes us in the other direction, sewing a list of strings up into one long string, using the string it was called on as a separator.

str.join() 将我们引向另一个方向,将一系列字符串缝合成一个长字符串,并使用调用它的字符串作为分隔符。

'/'.join([month, day, year])
'01/31/1956'
# Yes, we can put unicode characters right in our string literals :)
# 是的,我们可以将 unicode 字符放在字符串文字中:)

' 👏 '.join([word.upper() for word in words])
'PLUTO 👏 IS 👏 A 👏 PLANET!'

Building strings with .format()

Python lets us concatenate strings with the + operator.

使用 .format() 构建字符串

Python 允许我们使用+运算符连接字符串。

planet + ', we miss you.'
'Pluto, we miss you.'

If we want to throw in any non-string objects, we have to be careful to call str() on them first

如果我们想抛出任何非字符串对象,我们必须小心地首先对它们调用str()

position = 9
planet + ", you'll always be the " + position + "th planet to me."
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

Cell In[23], line 2
      1 position = 9
----> 2 planet + ", you'll always be the " + position + "th planet to me."

TypeError: can only concatenate str (not "int") to str
planet + ", you'll always be the " + str(position) + "th planet to me."
"Pluto, you'll always be the 9th planet to me."

This is getting hard to read and annoying to type. str.format() to the rescue.

这变得很难阅读并且打字很烦人。 str.format() 来帮助你。

"{}, you'll always be the {}th planet to me.".format(planet, position)
"Pluto, you'll always be the 9th planet to me."

So much cleaner! We call .format() on a "format string", where the Python values we want to insert are represented with {} placeholders.

Notice how we didn't even have to call str() to convert position from an int. format() takes care of that for us.

If that was all that format() did, it would still be incredibly useful. But as it turns out, it can do a lot more. Here's just a taste:

清晰多了! 我们在格式字符串上调用.format(),其中要插入的 Python 值用{}占位符表示。

请注意,我们甚至不必调用str()来将position从 int 转换。 format() 为我们处理了这个问题。

如果这就是format()所做的一切,它仍然非常有用。 但事实证明,它还可以做很多更多的事情。 这里只是一点点尝试:

pluto_mass = 1.303 * 10**22
earth_mass = 5.9722 * 10**24
population = 52910390
#         2 decimal points   3 decimal points, format as percent     separate with commas
# 2 位小数 3 位小数,格式为百分比,以逗号分隔
"{} weighs about {:.2} kilograms ({:.3%} of Earth's mass). It is home to {:,} Plutonians.".format(
    planet, pluto_mass, pluto_mass / earth_mass, population,
)
"Pluto weighs about 1.3e+22 kilograms (0.218% of Earth's mass). It is home to 52,910,390 Plutonians."
# Referring to format() arguments by index, starting from 0
# 通过索引引用 format() 参数,从 0 开始
s = """Pluto's a {0}.
No, it's a {1}.
{0}!
{1}!""".format('planet', 'dwarf planet')
print(s)
Pluto's a planet.
No, it's a dwarf planet.
planet!
dwarf planet!

You could probably write a short book just on str.format, so I'll stop here, and point you to pyformat.info and the official docs for further reading.

您可能可以只写一本关于str.format的小书,所以我会在这里停下来,并指出您pyformat.info和[官方文档](https: //docs.python.org/3/library/string.html#formatstrings)以供进一步阅读。

Dictionaries

Dictionaries are a built-in Python data structure for mapping keys to values.

字典

字典是一种内置的 Python 数据结构,用于将键映射到值。

numbers = {'one':1, 'two':2, 'three':3}

In this case 'one', 'two', and 'three' are the keys, and 1, 2 and 3 are their corresponding values.

Values are accessed via square bracket syntax similar to indexing into lists and strings.

在本例中,'one', 'two''three',1、2 和 3 是它们对应的值。

通过方括号语法访问值,类似于列表和字符串的索引。

numbers['one']
1

We can use the same syntax to add another key, value pair

我们可以使用相同的语法添加另一个键值对

numbers['eleven'] = 11
numbers
{'one': 1, 'two': 2, 'three': 3, 'eleven': 11}

Or to change the value associated with an existing key

或者更改与现有键关联的值

numbers['one'] = 'Pluto'
numbers
{'one': 'Pluto', 'two': 2, 'three': 3, 'eleven': 11}

Python has dictionary comprehensions with a syntax similar to the list comprehensions we saw in the previous tutorial.

Python 具有字典推导式,其语法类似于我们在上一篇教程中看到的列表推导式。

planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']
planet_to_initial = {planet: planet[0] for planet in planets}
planet_to_initial
{'Mercury': 'M',
 'Venus': 'V',
 'Earth': 'E',
 'Mars': 'M',
 'Jupiter': 'J',
 'Saturn': 'S',
 'Uranus': 'U',
 'Neptune': 'N'}

The in operator tells us whether something is a key in the dictionary

in 运算符告诉我们,对象是否是字典中的键

'Saturn' in planet_to_initial
True
'Betelgeuse' in planet_to_initial
False

A for loop over a dictionary will loop over its keys

字典上的 for 对其键进行循环

for k in numbers:
    print("{} = {}".format(k, numbers[k]))
one = Pluto
two = 2
three = 3
eleven = 11

We can access a collection of all the keys or all the values with dict.keys() and dict.values(), respectively.

我们可以分别使用dict.keys()dict.values()访问所有键或所有值的集合。

# Get all the initials, sort them alphabetically, and put them in a space-separated string.
# 获取所有缩写,按字母顺序对它们进行排序,并将它们放入以空格分隔的字符串中。

' '.join(sorted(planet_to_initial.values()))
'E J M M N S U V'

The very useful dict.items() method lets us iterate over the keys and values of a dictionary simultaneously. (In Python jargon, an item refers to a key, value pair)

非常有用的dict.items()方法让我们可以同时迭代字典的键和值。 (在Python术语中,指的是键值对)

for planet, initial in planet_to_initial.items():
    print("{} begins with \"{}\"".format(planet.rjust(10), initial))
   Mercury begins with "M"
     Venus begins with "V"
     Earth begins with "E"
      Mars begins with "M"
   Jupiter begins with "J"
    Saturn begins with "S"
    Uranus begins with "U"
   Neptune begins with "N"

To read a full inventory of dictionaries' methods, click the "output" button below to read the full help page, or check out the official online documentation.

要阅读字典方法的完整清单,请单击下面的“输出”按钮来阅读完整的帮助页面,或查看官方在线文档

help(dict)
Help on class dict in module builtins:

class dict(object)
 |  dict() -> new empty dictionary
 |  dict(mapping) -> new dictionary initialized from a mapping object's
 |      (key, value) pairs
 |  dict(iterable) -> new dictionary initialized as if via:
 |      d = {}
 |      for k, v in iterable:
 |          d[k] = v
 |  dict(**kwargs) -> new dictionary initialized with the name=value pairs
 |      in the keyword argument list.  For example:  dict(one=1, two=2)
 |  
 |  Built-in subclasses:
 |      StgDict
 |  
 |  Methods defined here:
 |  
 |  __contains__(self, key, /)
 |      True if the dictionary has the specified key, else False.
 |  
 |  __delitem__(self, key, /)
 |      Delete self[key].
 |  
 |  __eq__(self, value, /)
 |      Return self==value.
 |  
 |  __ge__(self, value, /)
 |      Return self>=value.
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __getitem__(...)
 |      x.__getitem__(y) <==> x[y]
 |  
 |  __gt__(self, value, /)
 |      Return self>value.
 |  
 |  __init__(self, /, *args, **kwargs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  __ior__(self, value, /)
 |      Return self|=value.
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __le__(self, value, /)
 |      Return self<=value.
 |  
 |  __len__(self, /)
 |      Return len(self).
 |  
 |  __lt__(self, value, /)
 |      Return self size of D in memory, in bytes
 |  
 |  clear(...)
 |      D.clear() -> None.  Remove all items from D.
 |  
 |  copy(...)
 |      D.copy() -> a shallow copy of D
 |  
 |  get(self, key, default=None, /)
 |      Return the value for key if key is in the dictionary, else default.
 |  
 |  items(...)
 |      D.items() -> a set-like object providing a view on D's items
 |  
 |  keys(...)
 |      D.keys() -> a set-like object providing a view on D's keys
 |  
 |  pop(...)
 |      D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
 |      
 |      If the key is not found, return the default if given; otherwise,
 |      raise a KeyError.
 |  
 |  popitem(self, /)
 |      Remove and return a (key, value) pair as a 2-tuple.
 |      
 |      Pairs are returned in LIFO (last-in, first-out) order.
 |      Raises KeyError if the dict is empty.
 |  
 |  setdefault(self, key, default=None, /)
 |      Insert key with a value of default if key is not in the dictionary.
 |      
 |      Return the value for key if key is in the dictionary, else default.
 |  
 |  update(...)
 |      D.update([E, ]**F) -> None.  Update D from dict/iterable E and F.
 |      If E is present and has a .keys() method, then does:  for k in E: D[k] = E[k]
 |      If E is present and lacks a .keys() method, then does:  for k, v in E: D[k] = v
 |      In either case, this is followed by: for k in F:  D[k] = F[k]
 |  
 |  values(...)
 |      D.values() -> an object providing a view on D's values
 |  
 |  ----------------------------------------------------------------------
 |  Class methods defined here:
 |  
 |  __class_getitem__(...) from builtins.type
 |      See PEP 585
 |  
 |  fromkeys(iterable, value=None, /) from builtins.type
 |      Create a new dictionary with keys from iterable and values set to value.
 |  
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __hash__ = None

Your Turn

You've learned a lot of Python... go demonstrate your new skills with some realistic programming applications.

到你了

您已经学习了很多 Python...通过一些实际的编程应用程序展示您的新技能

06.course-strings-and-dictionaries【字符串及字典】

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top