Python:List1的索引等于List2的索引

发布于 2025-02-02 11:42:33 字数 455 浏览 3 评论 0原文

我有2个列表,它们的长度相同且按顺序正确,我需要(我认为)List1的索引等于List2的索引,并在该索引上打印List2内容。

这就是我到目前为止所拥有的:

id = [10610, 12772, 10611, 13434, 13397, 13854]

name = ['sarah', 'john', 'mark', 'james', 'jack']

userid = int(input('enter user ID: '))
ind = id.index(userid)
 
if userid in id:
    print(ind)
else:
    print('wrong ID')

输出:

enter user ID: 13434
3

我想做的就是打印在“名称”列表中相同索引的内容,在这种情况下为“詹姆斯”

谢谢

I have 2 lists , they are the same length and in correct order, what i need(i think) is that index of list1 is equal to index of list2 and print the list2 content at that index.

this is what i have so far:

id = [10610, 12772, 10611, 13434, 13397, 13854]

name = ['sarah', 'john', 'mark', 'james', 'jack']

userid = int(input('enter user ID: '))
ind = id.index(userid)
 
if userid in id:
    print(ind)
else:
    print('wrong ID')

output:

enter user ID: 13434
3

what i want it to do is, to print the content thats at the same index in "name" list, in this case its "james"

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情绪失控 2025-02-09 11:42:33

好吧,评论所说的内容也像以下内容一样更改:

    id = [1, 2, 3, 4, 5, 6]

    name = ['sarah', 'john', 'mark', 'james', 'jack']

    userid = int(input('enter user ID: '))

    if userid in id:
        ind = id.index(userid) #notice this statement is inside the if and not outside
        print(name[ind])
    else:
        print("wrong id")

如果您尝试在检查ID(他们输入的一个)之前尝试使用Inden(..),那么您将获得valueerror:<一些numer>不在列表

,所以最好在IF中进行操作

well what the comments said and change it like the following too :

    id = [1, 2, 3, 4, 5, 6]

    name = ['sarah', 'john', 'mark', 'james', 'jack']

    userid = int(input('enter user ID: '))

    if userid in id:
        ind = id.index(userid) #notice this statement is inside the if and not outside
        print(name[ind])
    else:
        print("wrong id")

if you try to use inden(..) before checking if the userid (the one they entered) is in the id , then you will get ValueError: <some-numer> is not in list

so its better to do it inside the if

离旧人 2025-02-09 11:42:33

您需要做的就是执行以下操作:

id = [10610, 12772, 10611, 13434, 13397, 13854]

name = ['sarah', 'john', 'mark', 'james', 'jack']

userid = int(input('enter user ID: '))
ind = id.index(userid)
if userid in id:
    print(name[ind])  # Change in this line
else:
    print('wrong ID')

All you need to do is do the following:

id = [10610, 12772, 10611, 13434, 13397, 13854]

name = ['sarah', 'john', 'mark', 'james', 'jack']

userid = int(input('enter user ID: '))
ind = id.index(userid)
if userid in id:
    print(name[ind])  # Change in this line
else:
    print('wrong ID')
a√萤火虫的光℡ 2025-02-09 11:42:33

由于要从name在同一索引上打印字符串,因此可以使用name [ind]

不过,上面给出的代码还有其他问题 -

  • if -else条件没有做任何事情。 ind = id.index(userId)将给出valueerror如果userIdid> id list中不存在,程序将崩溃。这可以重构为以下内容:
id = [10610, 12772, 10611, 13434, 13397, 13854]

name = ['sarah', 'john', 'mark', 'james', 'jack']

userid = int(input('enter user ID: '))
 
if userid in id:
    print(name[id.index(userid)])
else:
    print('wrong ID')
  • IDname中的NO元素并不相同。这可能会导致indexError当输入为13854时。可以通过检查len(name)&lt来解决这一点。 Ind
  • 使用两个列表不是一种非常干净的方法。任何一个数组的操作都可以可能更改数据的映射。更好的方法是使用词典。
users = {
    "10610": "sarah",
    "12772": "john",
    "10611": "mark",
    "13434": "james",
    "13397": "jack",
    "13854": None
}

userid = input('enter user ID: ')
print(users.get(userid, "Wrong User ID"))

Since you want to print the string from name at the same index, you can use name[ind].

Though, there are some other issues with the code given above -

  • The if-else condition isn't doing anything. ind = id.index(userid) would give a ValueError if userid isn't present in the id list, and the program would crash. This can be refactored to the following:
id = [10610, 12772, 10611, 13434, 13397, 13854]

name = ['sarah', 'john', 'mark', 'james', 'jack']

userid = int(input('enter user ID: '))
 
if userid in id:
    print(name[id.index(userid)])
else:
    print('wrong ID')
  • The no of elements in id and in name isn't the same. This can lead to an IndexError when the input is 13854. This can be resolved by checking if len(name) < ind
  • Using two lists isn't a very clean approach. Any manipulation of either array can potentially change the mapping of data. A better approach would be to use a dictionary.
users = {
    "10610": "sarah",
    "12772": "john",
    "10611": "mark",
    "13434": "james",
    "13397": "jack",
    "13854": None
}

userid = input('enter user ID: ')
print(users.get(userid, "Wrong User ID"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文