2016-04-03 12 views
-3

自分のプログラムが居住者を検索することを希望し、特定の値であれば他のリストから同じ配置データを印刷します。例えば、このような状況では、私はそれを印刷したいと思うでしょう。リストを検索して他のリストから同じ場所を呼び出す

、「名前は年齢が7であり、彼らは場所に住んでいない、アランである」

と同様に、

は「名前はマーガレットで、年齢は66であり、彼らは場所に住んでいない」

name = ["Alan", "Susan", "Margaret"] 
age = [7, 34, 66] 
resident = [0, 1, 0] 

if resident = 0: 
    print ("name is {}, age is {} and they do not live in place".format(name[], age[])) 
+0

[Python的反復を超えます並列に複数のリスト](http://stackoverflow.com/q/21911483/2301450) – vaultah

答えて

0

zip複数のリストを取り、タプルのリストを返します。ここでは例です:各タプルを超える

zip(name, age, resident) 
>>>> [('Alan', 7, 0), ('Susan', 34, 1), ('Margaret', 66, 0)] 

反復処理は、非常に簡単です。

-1

あなたは一般的に使用される%s文字列置換を使用することができます。

name = ["Alan", "Susan", "Margaret"] 
age = [7, 34, 66] 
resident = [0, 1, 0] 

if resident[0] == 0:print ("name is %s, age is %s and they do not live in place"%(name[0],age[0])) 
0

を繰り返すリストからインデックスやアイテムを提供しており、またenumerateの使用を考えてみましょう:

>>> for i,x in enumerate(resident): 
     if x == 0: 
      print("name is {0}, age is {1} and they do not live in place".format(name[i], age[i])) 


name is Alan, age is 7 and they do not live in place 
name is Margaret, age is 66 and they do not live in place 
関連する問題