jisho = defaultdict(intかlist等の型)
#これで変数jishoが辞書になる
例
from collections import defaultdict
jisho = defaultdict(int)
lst = [“a”, “b”, “c”, “b”, “a”, “e”]
for key in lst:
jisho[key] += 1
print (jisho)
例:2
1 |
<span role="presentation"><span class="cm-keyword">from</span> <span class="cm-variable">collections</span> <span class="cm-keyword">import</span> <span class="cm-variable">defaultdict</span> </span> |
1 |
<span role="presentation"><span class="cm-comment"># 文字列description</span></span> |
1 |
<span role="presentation"><span class="cm-variable">description</span> = <span class="cm-error">\</span></span> |
1 |
<span role="presentation"><span class="cm-string">"AFL intends to import PVC-insulated and XLPE-insulated bunched copper wire from Honduras. "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span> |
1 |
<span role="presentation"><span class="cm-string">"The insulated wire will be used in the U.S. in the production of automotive wire harnesses. "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span> |
1 |
<span role="presentation"><span class="cm-string">"Copper strands, classifiable under subheading 7413.00, Harmonized Tariff System of the United States "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span> |
1 |
<span role="presentation"><span class="cm-string">"(HTSUS); polyvinyl chloride (PVC) pellets, classifiable under subheading 3904.21, HTSUS; and "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span> |
1 |
<span role="presentation"><span class="cm-string">"cross-link polyethylene (XLPE) pellets, classifiable under subheading 3901.30, HTSUS, all of U.S. origin will be exported to Honduras. "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span> |
1 |
<span role="presentation"><span class="cm-string">"The copper strands will be annealed in the U.S. and will be wound onto reels, "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span> |
1 |
<span role="presentation"><span class="cm-string">"containing between six and 14 untwisted and non-bunched copper strands. In Honduras, "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span> |
1 |
<span role="presentation"><span class="cm-string">"the strands are fed into a buncher, which arranges the strands in specific geometric orientations, "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span> |
1 |
<span role="presentation"><span class="cm-string">"with specific twist lengths ( lay lengths ) based on the specifications of the end use application "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span> |
1 |
<span role="presentation"><span class="cm-string">"( lay plate ). The strands then enter the compaction die which sets the diameter and length of "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span> |
1 |
<span role="presentation"><span class="cm-string">"the specific products, resulting in the creation of bunched wire. By using strands of various "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span> |
1 |
<span role="presentation"><span class="cm-string">"sizes and numbers of strands, different sizes of bunched wire are produced. It is "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span> |
1 |
<span role="presentation"><span class="cm-string">"stated that the size of the bunched wire determines its electrical current carrying capacity "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span> |
1 |
<span role="presentation"><span class="cm-string">"and physical strength. The bunched wire will contain between seven and 104 strands, with American "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span> |
1 |
<span role="presentation"><span class="cm-string">"Wire Gauge (AWG) measurements varying between 10 and 22. It is stated that the AWG level identifies the "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span> |
1 |
<span role="presentation"><span class="cm-string">"diameter, electrical current capacity and temperature rating, or maximum temperature levels within "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span> |
1 |
<span role="presentation"><span class="cm-string">"which the wire can function. From the compaction die, the bunched wire enters the bow , which rotates "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span> |
1 |
<span role="presentation"><span class="cm-string">"and twists the bunched wire, based on the needs of the given end use of the wire. "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span> |
1 |
<span role="presentation"><span class="cm-string">"Next, the bunched wire is wound onto a reel and prepared for insulation with either PVC or XLPE. "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span> |
1 |
<span role="presentation"><span class="cm-string">"The compound is melted and pumped through the extruder line. In the extruder line, a "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span> |
1 |
<span role="presentation"><span class="cm-string">"rotating wheel pulls the copper wire, and the compound is extruded over the wire. "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span> |
1 |
<span role="presentation"><span class="cm-string">"If PVC is used, the material is cooled following a controlled cooling with water, "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span> |
1 |
<span role="presentation"><span class="cm-string">"which solidifies the compound around the bunched wire. It is stated that PVC-insulated "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span> |
1 |
<span role="presentation"><span class="cm-string">"wire has a lower temperature rating and is more likely to be used in applications "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span> |
1 |
<span role="presentation"><span class="cm-string">"where it is subject to less intense heat. If XLPE is used, a steam vulcanization process "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span> |
1 |
<span role="presentation"><span class="cm-string">"is required which causes a chemical reaction in the extruded XLPE, causing "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span> |
1 |
<span role="presentation"><span class="cm-string">"crosslinking in the underlying polymer chain. It is stated that crosslinking"</span></span> |
1 |
<span role="presentation"></span> |
1 |
<span role="presentation"><span class="cm-comment"># defaultdictを定義</span></span> |
1 |
<span role="presentation"><span class="cm-variable">moji</span> = <span class="cm-variable">defaultdict</span>(<span class="cm-builtin">int</span>)<span class="cm-comment">#これが辞書になる</span></span> |
1 |
<span role="presentation"></span> |
1 |
<span role="presentation"><span class="cm-comment"># 文字の出現回数を記録</span></span> |
1 |
<span role="presentation"><span class="cm-keyword">for</span> <span class="cm-variable">key</span> <span class="cm-keyword">in</span> <span class="cm-variable">description</span>:</span> |
1 |
<span role="presentation"> <span class="cm-variable">moji</span>[<span class="cm-variable">key</span>] += <span class="cm-number">1</span></span> |
1 |
<span role="presentation"></span> |
1 |
<span role="presentation"><span class="cm-comment"># ソートし、上位10要素を出力して下さい</span></span> |
1 |
<span role="presentation"><span class="cm-builtin">print</span>(<span class="cm-builtin">sorted</span>(<span class="cm-variable">moji</span>.<span class="cm-property">items</span>(), <span class="cm-variable">key</span>=<span class="cm-keyword">lambda</span> <span class="cm-variable">x</span>: <span class="cm-variable">x</span>[<span class="cm-number">1</span>], <span class="cm-variable">reverse</span>=<span class="cm-keyword">True</span>)[:<span class="cm-number">10</span>])</span> |
1 |
<span role="presentation"></span> |
結果
コメントを残す