Python元组搜索

Python元组搜索,python,for-loop,python-2.7,if-statement,Python,For Loop,Python 2.7,If Statement,我正试图写一个简单的程序,让我输入一个名字,然后它会搜索员工名单,看看这个人是否是经理。如果员工人数大于等于500,则该人员将成为经理 例如,对于[“David”,501],如何在条件语句中只指定数字部分?我使用了item作为名称,但不确定如何指定数字。谢谢 #!/usr/bin/python input = raw_input("Please enter name to determine if employee is a manager: ") employeeList = [["Sha

我正试图写一个简单的程序,让我输入一个名字,然后它会搜索员工名单,看看这个人是否是经理。如果员工人数大于等于500,则该人员将成为经理

例如,对于
[“David”,501]
,如何在条件语句中只指定数字部分?我使用了item作为名称,但不确定如何指定数字。谢谢

#!/usr/bin/python

input = raw_input("Please enter name to determine if employee is a manager: ")

employeeList = [["Shawn", 500], ["David", 501], ["Ted", 14], ["Jerry", 22]]

for item in employeeList :
    print "Employee Name is: ", item
    if item == input and item >= 500:
        print "This person is a manager."
    else:
        print "This person is not a manager."

项目[0]
将是姓名,
项目[1]
他们的员工编号。

项目[0]
将是姓名,
项目[1]
他们的员工编号。

您可以使用数字索引引用列表中的特定元素。因为您使用的是列表列表,所以我们可以引用
循环变量的索引

下面是相应调整的for循环:

for item in employeeList:
  print "Employee name is: %s" % (item[0])
  if item[0] == input and item[1] >= 500:
    print "This person is a manager."
  else:
    print "This person is not a manager."

您可以使用数字索引引用列表中的特定元素。因为您使用的是列表列表,所以我们可以引用
循环变量的索引

下面是相应调整的for循环:

for item in employeeList:
  print "Employee name is: %s" % (item[0])
  if item[0] == input and item[1] >= 500:
    print "This person is a manager."
  else:
    print "This person is not a manager."

把清单上的每一项都打印出来有什么意义

您只需找到被查询的人并检查他/她是否是经理

#!/usr/bin/python

input = raw_input("Please enter name to determine if employee is a manager: ")

employeeList = [["Shawn", 500], ["David", 501], ["Ted", 14], ["Jerry", 22]]
employeeDict = dict(employeeList)

if input in employeeDict:
    if employeeDict[input] > 500:
        print "This person is a manager."
    else:
        print "This person is not a manager."
else:
    print "This person is not an employee."

把清单上的每一项都打印出来有什么意义

您只需找到被查询的人并检查他/她是否是经理

#!/usr/bin/python

input = raw_input("Please enter name to determine if employee is a manager: ")

employeeList = [["Shawn", 500], ["David", 501], ["Ted", 14], ["Jerry", 22]]
employeeDict = dict(employeeList)

if input in employeeDict:
    if employeeDict[input] > 500:
        print "This person is a manager."
    else:
        print "This person is not a manager."
else:
    print "This person is not an employee."

这应该是:

lookup = dict( (name, ' is a manager' if num >= 500 else ' is not a manager') for name, num in employeeList)
print '{}{}'.format(some_name_from_somewhere, lookup.get(some_name_from_somewhere, ' is not known'))

这应该是:

lookup = dict( (name, ' is a manager' if num >= 500 else ' is not a manager') for name, num in employeeList)
print '{}{}'.format(some_name_from_somewhere, lookup.get(some_name_from_somewhere, ' is not known'))

首先,请注意,在您的示例中,您使用的不是元组列表,而是列表列表。元组是用普通括号定义的

("I", "am", "a", "tuple")
序列存取 在Python中,
list
tuple
是可访问的,因此可以使用数字索引访问它们

有关列表:

item=[“Shawn”,501]
,然后您可以执行
item[0]
item[1]

对于元组,它是完全相同的:

item=(“Shawn”,501)
,然后您可以执行
item[0]
item[1]

拆包 当您的元组很小时,“打开”它们--
unpacking
作为Python术语,也是非常方便的

item = ("Shawn", 501)
name, number = item # We unpack the content of the tuple into some variables
命名元组 Python2.6引入了一种您可能会发现很有用的类型。以您的示例为例,您可以执行以下操作:

Employee = namedtuple("Employee", ['name', 'number'])
employeeList = [
    Employee("Shawn", 500),
    Employee("David", 501),
    Employee("Ted", 14],
    Employee("Jerry", 22)
]
for item in employeeList:
    if item.name == name and item.number >= 500:
        print "This person is a manager."
    else:
        print "This person is not a manager."

首先,请注意,在您的示例中,您使用的不是元组列表,而是列表列表。元组是用普通括号定义的

("I", "am", "a", "tuple")
序列存取 在Python中,
list
tuple
是可访问的,因此可以使用数字索引访问它们

有关列表:

item=[“Shawn”,501]
,然后您可以执行
item[0]
item[1]

对于元组,它是完全相同的:

item=(“Shawn”,501)
,然后您可以执行
item[0]
item[1]

拆包 当您的元组很小时,“打开”它们--
unpacking
作为Python术语,也是非常方便的

item = ("Shawn", 501)
name, number = item # We unpack the content of the tuple into some variables
命名元组 Python2.6引入了一种您可能会发现很有用的类型。以您的示例为例,您可以执行以下操作:

Employee = namedtuple("Employee", ['name', 'number'])
employeeList = [
    Employee("Shawn", 500),
    Employee("David", 501),
    Employee("Ted", 14],
    Employee("Jerry", 22)
]
for item in employeeList:
    if item.name == name and item.number >= 500:
        print "This person is a manager."
    else:
        print "This person is not a manager."

使用
input
作为变量名不是一个好主意,它已经是函数名了。另外,if子句是错误的,但您很快就会注意到;)谢谢大家,是的,我现在确实意识到我的if子句是错误的:)我将来也会记住输入名称。此外,
dict
是解决这个问题的正确数据结构,而不是
list
s的
list
。@ChinmayKanchi-yup-同意-我的答案使用了.get()默认为无效查找…使用
input
作为变量名不是一个好主意,因为它已经是函数名。另外,if子句是错误的,但您很快就会注意到;)谢谢大家,是的,我现在确实意识到我的if子句是错误的:)我将来也会记住输入名称。此外,
dict
是解决这个问题的正确数据结构,而不是
list
s的
list
。@ChinmayKanchi-yup-同意-我的答案使用了.get()无效查找的默认值。。。