2016-12-23 10 views
0

詳細を説明すると、複数のワークステーションに関する情報があります。たとえば、名前、インストールされているアプリケーション、ユーザー、部門が異なる100台のワークステーションがあります。複数のインスタンスでいくつかの属性値をチェックし、同じインスタンスの別の属性をPythonで表示する

複数インスタンスで特定の属性をチェックし、同じインスタンスから別の属性を出力したいとしました。たとえば、

"xxxx"というユーザーを検索しています。存在する場合、 "xxxx"ユーザーが属するマシン名または部門と同じような別の属性を表示します。

私はこれをやろうとしましたが、1つのインスタンスに対して行うことができました。オブジェクトをチェックする必要があるクラスのように、class.object条件を使用してこの操作を行うことができます。

しかし、複数のインスタンスがあり、特定の属性をランダムにチェックするのは難しいことです。私はいくつかのヒントを見つけようとしましたが、失敗しました。私はPythonの初心者であり、誰かが答えることができれば助けになるでしょう。

from itertools import count 

class Workstation(object): 
"""Creates an workstation list-object""" 
count = 0 

    def __init__(self, machine_name, graphics, cad, user, dept): 
     self.machine_name = machine_name 
     self.graphics = graphics 
     self.cad = cad 
     self.user = user 
     self.dept = dept 
     Workstation.count += 1 

    def ws_list(self): 
     """showing the machine name with department""" 
     print ('Name:', self.machine_name, 'Department:', self.dept) 


    def total(self): 
     print ('the total count is %d' %Workstation.count) 


    def info(self): 
     print("enter the machine name") 
     dept = raw_input(" ") 
     if dept == self.dept: 
      print ("the department is", self.machine_name) 
     else: 
      print ("the machine is not in the list") 


ws1 = Workstation("xxx", "nVidia Quadro FX 880M", "cad", 123, "IN") 
ws2 = Workstation("yyy", "nVidia Quadro FX 880M", "cae", 456, "US") 
ws3 = Workstation("zzz", "nVidia Quadro FX 880M", "IT", 789, "GE") 

print ws1.machine_name 
print ws1.ws_list() 
print total 

よろしく、

ジェイ

+0

上記のコードにはどのような問題がありますか?なぜ失敗したのかをもっと説明できますか? –

+0

ありがとうPulkit Goyal、実際にコードの最後の3行を見て、特定の属性を取得するには、ws1、ws2などのインスタンスを言及する必要があります。 – jay

答えて

0

Workstationクラスが複数のインスタンスがあることを知っているべきではありません。これにはlistを使用してください。ここでの例では、単純なリストおよびフィルタを使用して、次のとおりです。この例では

class Workstation(object): 
    """Creates an workstation list-object""" 
    def __init__(self, machine_name, graphics, cad, user, dept): 
     self.machine_name = machine_name 
     self.graphics = graphics 
     self.cad = cad 
     self.user = user 
     self.dept = dept 

# this is your database, containing all workstations 
all_ws = [ 
    Workstation("xxx", "nVidia Quadro FX 880M", "cad", 123, "IN"), 
    Workstation("yyy", "nVidia Quadro FX 880M", "cae", 456, "US"), 
    Workstation("zzz", "nVidia Quadro FX 880M", "IT", 789, "GE") 
] 

def filter_by_dept(workstations, dept): 
    """Filters workstations by Department""" 
    # the filtered list is created using a list comprehension 
    return [x for x in workstations if x.dept == dept] 

print("enter the department name") 
dept = raw_input(" ") 

matching_ws = filter_by_dept(all_ws, dept) 

for ws in matching_ws: 
    # a more portable syntax for string formating 
    print('Name: {} Department: {}'.format(self.machine_name, self.dept)) 

# the len() fonction returns the number of items in the list 
print(len(matching_ws)) 

Workstationクラスはプロパティのみ(なしメソッド)を有しています。カスタムクラスを使用する代わりに、これらのプロパティをdictに保存することもできます。

+0

提案いただきありがとうございます。このコードを実行中にこのエラーが発生しました .....(AttributeError: 'リスト'オブジェクトには属性 'len'がありません)..... おそらく私はlen属性をインポートする必要があります...?助けてください。 – jay

+0

私は答えを編集しましたが、私のデバイスで今テストすることができませんでした – abstrus

+0

pythonのデータ構造について読む:https://docs.python.org/3.6/tutorial/datastructures.html – abstrus

関連する問題