Pythonのシーケンスで2番目に繰り返される単語?
文字列が与えられ、私たちのタスクは2番目に繰り返される単語を見つけることです。ここでは、単語をキーとして、その頻度を値として含む辞書を作成するためのCounter(iterator)を使用します。
アルゴリズム
Step 1: Create user define list. Step 2: Then convert list into a dictionary. Step 2: Next get the values and sort them in descending order. Step 3: Then the second element is the second largest value. Step 4: Next again traverse whole dictionary and display key whose value is equal to second largest element.
サンプルコード
# To print Second most repeated word in a sequence in Python from collections
import Counter
defsecondrepeatation(A):
# Convert list into dictionary
con = Counter(A)
res = sorted(con.values(), reverse=True)
maxi = res[1]
for (key, val) in con.items():
if val == maxi:
print("Second most repeated word ::>",key)
return
# Driver program
if __name__ == "__main__":
A=list() #create user defined list
n=int(input("Enter the size of the List ::"))
print("Enter the word ::")
for i in range(int(n)):
k=input("")
A.append(k)
secondrepeatation(A) # call function
出力
Enter the size of the List ::4 Enter the word :: aa bb aa cc Second most repeated word ::> bb
-
Androidでシーケンス内で2番目に繰り返される文字列を見つける方法は?
この例は、Androidでシーケンス内で2番目に繰り返される文字列を見つける方法を示しています。 ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。 ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="h
-
Pythonでリストを印刷する
リストは一連の要素です。シーケンス内のすべての要素は、シーケンス内の位置によってアクセスできます。インデックスは0で始まります。したがって、list [2]は、リストの3番目のインデックス2、つまり50の要素を返します。