2017-04-02 11 views
-2

私は優先順位を管理する簡単なGUIプログラムを作成しています。C#の汎用リストのPythonと同等です<T>

Priorities

私は正常にリストボックスに項目を追加するための機能を追加するために管理しています。今度は、C#のList <と呼ばれるアイテムにアイテムを追加します。そのようなことはPythonに存在しますか?

List<Priority> priorities = new List<Priority>(); 

を...そして次のメソッドを作成します:

は例えば、C#で、私が最初に作成し、リストビューにアイテムを追加するには

void Add() 
{ 
    if (listView1.SelectedItems.Count > 0) 
    { 
     MessageBox.Show("Please make sure you have no priorities selected!", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information); 
    } 
    else if (txt_Priority.ReadOnly == true) { MessageBox.Show("Please make sure you refresh fields first!", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information); } 
    else 
    { 
     if ((txt_Priority.Text.Trim().Length == 0)) { MessageBox.Show("Please enter the word!", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information); } 
     else 
     { 
      Priority p = new Priority(); 
      p.Subject = txt_Priority.Text; 

      if (priorities.Find(x => x.Subject == p.Subject) == null) 
      { 
       priorities.Add(p); 
       listView1.Items.Add(p.Subject); 
      } 
      else 
      { 
       MessageBox.Show("That priority already exists in your program!"); 
      } 
      ClearAll(); 
      Sync(); 
      Count(); 
     } 
    } 
    SaveAll(); 

} 
+3

"Python list"を検索するための情報が見つかりませんでしたか? – jonrsharpe

答えて

1

Pythonはdynamic次のとおりです。

>>> my_generic_list = [] 
>>> my_generic_list.append(3) 
>>> my_generic_list.append("string") 
>>> my_generic_list.append(['another list']) 
>>> my_generic_list 
[3, 'string', ['another list']] 

追加する前に何も定義する必要はありません。nyオブジェクトを既存のlistに追加します。

Pythonはduck-typingを使用します。リストを反復して各要素のメソッドを呼び出す場合は、その要素がそのメソッドを理解していることを確認する必要があります。

ですから、相当したい場合:

List<Priority> priorities 

をあなただけのリストを初期化し、あなただけのそれにPriorityインスタンスを追加を確認する必要があります。それでおしまい!

関連する問題