搜索列表然后从其他列表中调用相同的位置

时间:2016-04-03 08:18:42

标签: python

我希望我的程序搜索常驻,如果它是特定值,它将从其他列表打印相同的放置数据。例如,在这种情况下,我希望它打印;

"名字是艾伦,年龄是7岁,他们不住在原地",

以及

"名字是玛格丽特,年龄是66,他们不住在原地"

@Override
public void onClick(View v) {
    String tag = v.getTag();
    if (tag.equals("btna1")) {
        Log.i("Test", "Clicked on button A 1");
    } else if (tag.equals("btnb1")) {
        Log.i("Test", "Clicked on button B 1");
    }
}

3 个答案:

答案 0 :(得分:0)

for person, ages, residency in zip(name,age,resident):
    if residency == 0:
        print("name is {0}, age is {1} and they do not live in place".format(person, ages))

zip获取多个列表并返回元组列表。这是一个例子:

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

迭代每个元组非常容易。

答案 1 :(得分: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

答案 2 :(得分:-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]))