回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>在使用Python的<code>max()</code>内置方法时,我发现了一件有趣的事情。。。。你知道吗</p>
<pre><code>input_one = u'A測試測試;B測試;測試;D測試測試測試;E測試測試測試測試測試測試測試測試測試'
input_two = u'測試測試;測試;測試;測試測試測試;測試測試測試測試測試測試測試測試測試'
input_en = u'test;test,test,test;testtesttest;testtesttesttest'
input_ja = u'ああああああ;あああ;あああああああ;ああああああああああああ'
input_ja_mixed = u'aああああああ;bあああ;cあああああああ;dああああああああああああ'
input_ascii = u'egfwergreger;@#@$fgdfdfdfdsfsdfsdf;sdfsdfsfsdfs233'
def test_length(input):
lengths = []
for i in input:
lengths.append(len(i))
index = find_index(input, max(lengths))
return input[index]
def find_index(input, to_find):
for index, value in enumerate(input):
print('index: %s, length: %s, value: %s' % (index, len(value), value))
if len(value) == to_find:
return index
def test_one(input):
input = input.split(';')
print('input:', input)
print('using test_length: ', test_length(input))
print('using max():', max(input))
</code></pre>
<p>如果使用<code>max()</code>在只包含英文字母的列表中查找max元素,效果会很好。你知道吗</p>
<p>但是,如果元素与符号混合(比如<code>@</code><code>#</code><code>$</code>),它的行为就不同了。你知道吗</p>
<p>例如</p>
<pre><code>In [80]: test_one(input_ascii)
input: ['egfwergreger', '@#@$fgdfdfdfdsfsdfsdf', 'sdfsdfsfsdfs233']
index: 0, length: 12, value: egfwergreger
index: 1, length: 21, value: @#@$fgdfdfdfdsfsdfsdf
using test_length: @#@$fgdfdfdfdsfsdfsdf
using max(): sdfsdfsfsdfs233
</code></pre>
<p>特殊情况是,中文和英文字母混在一起:</p>
<pre><code>In [82]: test_one(input_one)
input: ['A測試測試', 'B測試', '測試', 'D測試測試測試', 'E測試測試測試測試測試測試測試測試測試']
index: 0, length: 5, value: A測試測試
index: 1, length: 3, value: B測試
index: 2, length: 2, value: 測試
index: 3, length: 7, value: D測試測試測試
index: 4, length: 19, value: E測試測試測試測試測試測試測試測試測試
using test_length: E測試測試測試測試測試測試測試測試測試
using max(): 測試
</code></pre>
<p>文档没有指定<code>max()</code>方法具有的任何特殊行为。你知道吗</p>
<p>Python版本是python3.4。你知道吗</p>
<p>这是我的问题还是我不知道的?你知道吗</p>