擅长:python、mysql、java
<p><strong>修复:</strong>要合并<code>[['Do sth', 'Shine'], ['Swag'], ['Lag']]</code>您可以使用简单的列表理解</p>
<pre><code>a = [['Do sth', 'Shine'], ['Swag'], ['Lag']]
a = [word for sublist in a for word in sublist]
</code></pre>
<hr/>
<p><strong>最好:</strong>修改<code>add_duty</code>只将单词添加到<code>self.duty</code>列表中,而不是列表中</p>
<pre><code>def add_duty(self, new_duty):
if isinstance(new_duty, list):
self.duty.extend(new_duty)
else:
self.duty.append(new_duty)
# call like
manager_employee[2].add_duty(['Do sth', 'Shine']) # with a list
manager_employee[2].add_duty('Swag') # with a string
</code></pre>
<hr/>
<p><strong>更好:</strong>使用<code>*args</code>语法,它接受所有给定参数并将它们存储在列表中</p>
<pre><code>def add_duty(self, *new_duty): # new_duty contains all parameters, as a list
self.duty.extend(new_duty)
# call with any amount of string
manager_employee[2].add_duty('Do sth', 'Shine', 'other')
manager_employee[2].add_duty('Do sth', 'Shine')
manager_employee[2].add_duty('Swag')
</code></pre>