<h2>解决方案</h2>
<pre class="lang-py prettyprint-override"><code>df = pd.DataFrame({'Col1': ['ABS', 'CDC', 'POP'],
'Col2': [45, 23, 15],
})
keys = aa.keys()
df.Col1 = [''.join([aa.get(e) if (e in keys) else e for e in list(ee)]) for ee in df.Col1.tolist()]
df
</code></pre>
<p><strong>输出</strong>:</p>
<p><a href="https://i.stack.imgur.com/jm8aZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jm8aZ.png" alt="enter image description here"/></a></p>
<blockquote>
<h2>Unpacking the Condensed List Comprehension</h2>
</blockquote>
<p>让我们用一种更易读的形式写下清单。我们创建一个函数<code>do_something</code>来理解列表理解的第一部分中发生了什么。第二部分(<code>for ee in df.Col1.tolist()</code>)基本上迭代数据帧<code>df</code>的列<code>'Col1'</code>中的每一行。你知道吗</p>
<pre class="lang-py prettyprint-override"><code>def do_something(x):
# here x is like 'ABS'
xx = '.join([aa.get(e) if (e in keys) else e for e in list(x)])
return xx
df.Col1 = [do_something(ee) for ee in df.Col1.tolist()]
</code></pre>
<h3>拆包<code>do_something(x)</code></h3>
<p>函数<code>do_something(x)</code>执行以下操作。如果你用<code>x = 'ABS'</code>试试,会更容易。<code>do_something</code>中的<code>''.join(some_list)</code>加入生成的列表。下面的代码块将说明这一点。你知道吗</p>
<pre class="lang-py prettyprint-override"><code>x = 'ABS'
print(do_something(x))
[aa.get(e) if (e in keys) else e for e in list(x)]
</code></pre>
<p><strong>输出</strong>:</p>
<pre><code>ADBS
['AD', 'B', 'S']
</code></pre>
<blockquote>
<h3>So what is the core logic?</h3>
<p>The following code-block shows you step-by-step how the logic works. Obviously, the <code>list comprehension</code> introduced at the beginning of the solution compresses the <code>nested for loops</code> into a single line, and hence should be preferred over the following. </p>
</blockquote>
<pre><code>keys = aa.keys()
packlist = list()
for ee in df.Col1.tolist():
# Here we iterate over each element of
# the dataframe's column (df.Col1)
# make a temporary list
templist = list()
for e in list(ee):
# here e is a single character of the string ee
# example: list('ABS') = ['A', 'B', 'S']
if e in keys:
# if e is one of the keys in the dict aa
# append the corresponding value to templist
templist.append(aa.get(e))
else:
# if e is not a key in the dict aa
# append e itself to templist
templist.append(e)
# append a copy of templist to packlist
packlist.append(templist.copy())
# Finally assign the list: packlist to df.Col1
# to update the column values
df.Col1 = packlist
</code></pre>
<h2>参考文献</h2>
<p>列表和dict理解是一些非常强大的工具,任何python程序员在编写代码时都会发现它们既方便又漂亮。它们能够将原本复杂的代码块整齐地压缩成一两行。我建议你看一下下面的内容。你知道吗</p>
<ol>
<li><a href="https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions" rel="nofollow noreferrer">List Comprehensions: ^{<cd10>}</a></li>
<li><a href="https://docs.python.org/3/tutorial/datastructures.html#dictionaries" rel="nofollow noreferrer">Dict Comprehensions: ^{<cd10>}</a></li>
<li><a href="https://medium.com/better-programming/list-comprehension-in-python-8895a785550b" rel="nofollow noreferrer">List Comprehension in Python: ^{<cd12>}</a></li>
</ol>