<p>我想了一个方法来操纵第一次尝试,让它看起来更好。。。你知道吗</p>
<p>够笨拙的,不是真正的技术。你知道吗</p>
<p>即使它看起来不错,但实际上它是错误的-它计数(伊利诺伊州得克萨斯州)作为额外的,无论它出现在哪里(然而,它应该只考虑何时(伊利诺伊州得克萨斯州)是一个错过了第一次尝试)。你知道吗</p>
<pre><code>list1 = ["New York", "California", "Illinois", "Texas", "Illinois", "Texas", "Illinois", "Texas", "Illinois", "Texas", "Illinois", "Texas", "Indiana"]
repetitives = []
for num, element in enumerate(list1):
if element == list1[num - 2]:
repetitives.append(element)
core_repetitives = repetitives[0:2]
string_repetitives = ",".join(repetitives)
string_core_repetitives = ",".join(core_repetitives)
repetitives_times = string_repetitives.count(string_core_repetitives)
string_list1 = ",".join(list1)
first_try = string_list1.replace(string_repetitives, "(" + "-".join(core_repetitives) + ") " + str(repetitives_times) + " times")
extra_count = first_try.count(string_core_repetitives)
actual_times = repetitives_times + extra_count
second_try = string_list1.replace(string_repetitives, "(" + "-".join(core_repetitives) + ") " + str(actual_times) + " times")
print second_try.replace(string_core_repetitives, "").replace(",,", ",")
</code></pre>
<p>输出为:</p>
<pre><code>New York,California,(Illinois-Texas) 5 times,Indiana
</code></pre>