结果python中列表的第二个元素
我有一份清单:结果python中列表的第二个元素,python,list,python-3.x,Python,List,Python 3.x,我有一份清单: list = [['John','Barnes'], ['Bob','Marley'], ['Chris','Brown']] 我想使用x作为第一个元素的变量,结果将是该列表的第二个元素 x = input("Type the first name") 然后打印第二个元素。有人能帮我吗?IIUC您可以使用re模块和列表理解: import re l = [['John', 'Barnes'], ['Bob', 'Marley'], ['Chris', 'Brown']] x
list = [['John','Barnes'], ['Bob','Marley'], ['Chris','Brown']]
我想使用x作为第一个元素的变量,结果将是该列表的第二个元素
x = input("Type the first name")
然后打印第二个元素。有人能帮我吗?IIUC您可以使用
re
模块和列表理解:
import re
l = [['John', 'Barnes'], ['Bob', 'Marley'], ['Chris', 'Brown']]
x = 'Bob'
result = [l1[1] for l1 in l if re.findall(x, l1[0])]
print(result)
['Marley']
或者您可以将其与x进行比较:
result = [l1[1] for l1 in l if l1[0] == x]
['Marley']
IIUC您可以使用re
模块和列表理解
:
import re
l = [['John', 'Barnes'], ['Bob', 'Marley'], ['Chris', 'Brown']]
x = 'Bob'
result = [l1[1] for l1 in l if re.findall(x, l1[0])]
print(result)
['Marley']
或者您可以将其与x进行比较:
result = [l1[1] for l1 in l if l1[0] == x]
['Marley']
该列表很容易转换为。我能想到的最简单的方法是:
In [14]: names = dict(list) # first name -> second name
In [15]: x = input('Type the first name: ')
Type the first name: Bob
In [16]: names[x] # search the `names` dictionary and return the second name
Out[16]: 'Marley'
如果您不想将list
转换为dictionary,一个简单的循环如下
x = input('Type the first name: ')
for first, second in list:
if first == x:
print(second)
break
它也可以写为:
字典查找通常比这更快,如果可以的话,应该使用第一种解决方案
另外,尽量不要使用内置类型的名称(例如,列表
)和函数作为变量名。该列表很容易转换为变量名。我能想到的最简单的方法是:
In [14]: names = dict(list) # first name -> second name
In [15]: x = input('Type the first name: ')
Type the first name: Bob
In [16]: names[x] # search the `names` dictionary and return the second name
Out[16]: 'Marley'
如果您不想将list
转换为dictionary,一个简单的循环如下
x = input('Type the first name: ')
for first, second in list:
if first == x:
print(second)
break
它也可以写为:
字典查找通常比这更快,如果可以的话,应该使用第一种解决方案
此外,尽量不要将内置类型(例如列表
)和函数的名称用作变量名。如果您需要:
x= raw_input("Type the first name\n")
print ''.join([i[1] for i in list if i[0] == x ])
输入:
约翰
输出:
巴恩斯
如果这是你想要的:
x= raw_input("Type the first name\n")
print ''.join([i[1] for i in list if i[0] == x ])
输入:
约翰
输出:
巴恩斯
一个简单的方法是:
# It would not be correct to name the list of names as 'list'
l = [['John','Barnes'], ['Bob','Marley'], ['Chris','Brown']]
#get the input and convert it into lowercase
x = input("Type the first name").lower()
#A function that prints the last name , if there is a match , Note : This would also print the last names for all first name matches.
def get_last_names_for_first_names(x):
for first_name,last_name in l:
if (first_name.lower() == x):
print (last_name)
就是这样。一个简单的方法是:
# It would not be correct to name the list of names as 'list'
l = [['John','Barnes'], ['Bob','Marley'], ['Chris','Brown']]
#get the input and convert it into lowercase
x = input("Type the first name").lower()
#A function that prints the last name , if there is a match , Note : This would also print the last names for all first name matches.
def get_last_names_for_first_names(x):
for first_name,last_name in l:
if (first_name.lower() == x):
print (last_name)
就是这样。我建议使用原始输入
而不是输入
,Python 2.X将根据您的输入结果调用eval
,您可能会得到意外的命名错误。@jacob:不是Python 3。@vaultah感谢您明确指出,它根据Python版本的不同而有所不同。请不要命名变量list
,因为它将隐藏内置列表对象
,我建议使用它原始输入
而不是输入
,Python 2.X将根据您的输入结果调用eval
,您可能会得到意外的命名错误。@jacob:不是Python 3。@vaultah感谢您明确指出,它根据Python版本的不同而有所不同。请不要命名变量list
,因为它将隐藏内置列表对象
,非常感谢。要记录的列表非常有效!谢谢。要记录的列表非常有效!