【问题标题】:How can I select a char from a string inside a list in python and compare it with a single char?如何从 python 列表中的字符串中选择一个字符并将其与单个字符进行比较?
【发布时间】:2020-06-13 22:15:25
【问题描述】:

这是我尝试从 li 列表中的“two”字符串中获取“t”字符并将其与单个“t”字符进行比较的代码:

li=["one","two","three"];

choice=1;

if li[choice[1]]=="t":
    print("valid");

else:
    print("unvalid");

问题是当我运行它时,它说:

Traceback (most recent call last):
  File "C:\Users\Yobob\Documents\cours\ISN\ProjetISN\testPython.py", line 3, in <module>
    if li[choice[1]]=="t":
TypeError: 'int' object is not subscriptable

【问题讨论】:

    标签: python string list char


    【解决方案1】:

    choice[1] 正在做1[1] 这没有意义。我想你想要li[choice][1]。即

    if li[choice][1] == "t":
    

    【讨论】:

    • 非常感谢! (选择目的旁边的 [1] 是选择第一个字符,但它需要像这样位于整个数组旁边:li[choice][1])
    猜你喜欢
    • 2011-10-05
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    • 1970-01-01
    • 2013-01-20
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    相关资源
    最近更新 更多