Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:列表索引必须是整数或片,而不是str";“计算字符串”;_Python_Arrays_List_Python 3.5 - Fatal编程技术网

Python TypeError:列表索引必须是整数或片,而不是str";“计算字符串”;

Python TypeError:列表索引必须是整数或片,而不是str";“计算字符串”;,python,arrays,list,python-3.5,Python,Arrays,List,Python 3.5,我试图计算输入字符串中连续的d或d的数量 但它不适用于此代码,我不知道我在哪里犯了错误 请解决这个问题 up = 0 down = 0 down_count = 0 test = input("Enter test cases: ") for i in test: s = input("Enter the string: ") l = list(s) for c in l: if l[c] == 'u' or 'U': up +

我试图计算输入字符串中连续的d或d的数量

但它不适用于此代码,我不知道我在哪里犯了错误

请解决这个问题

up = 0
down = 0
down_count = 0

test = input("Enter test cases: ")

for i in test:
    s = input("Enter the string: ")
    l = list(s)
    for c in l:
        if l[c] == 'u' or 'U':
            up += 1
        if l[c] == 'd' or 'D':
            down += 1
        down_count += 1

print(down_count)
错误是:

Enter test cases: 1
Enter the string: duuuuuddddduuudddd
Traceback (most recent call last):
  File "C:/Users/HAMEED/PycharmProjects/crayons/ada_and_crayons.py", line 11, in <module>
    if l[c] == 'u' or 'U':
TypeError: list indices must be integers or slices, not str
输入测试用例:1
输入字符串:duuuudddduuuddd
回溯(最近一次呼叫最后一次):
文件“C:/Users/HAMEED/PycharmProjects/crayons/ada_和_crayons.py”,第11行,in
如果l[c]=“u”或“u”:
TypeError:列表索引必须是整数或片,而不是str

谢谢。

Python for循环实际上是一个for-each循环
c
已经是列表中的元素,而不是索引

还要注意,
不是这样工作的;每次都需要显式地与元素进行比较

for c in l:
    if c == 'u' or c == 'U':
        ...

顺便说一句,你的代码有更多的问题。回到你的问题:

替换这部分代码

    for c in l:
        if l[c] == 'u' or 'U':
            up += 1
        if l[c] == 'd' or 'D':
            down += 1
        down_count += 1
为此:

    for c in l:
        if c.lower() == 'u':
            up += 1
        if c.lower() == 'd':
            down += 1
        down_count += 1

解释如下:

  • c
    本身是一个字符(
    表示l中的c:
    ,其中
    l
    是一个字符列表);它不是一个索引

  • c.lower()
    将字母转换为小写,因此只需将其与小写字母进行比较

  • 但是,正如我告诉你的那样,你的代码有更多的问题。

    (你的代码中有足够多的多余命令来实现你的目标,所以我把它们扔掉了。)

    您只想对连续的
    d
    (或
    d
    )字母组进行计数。一种方法(不是很像python,但您是初学者)是:

    当当前字母为
    d
    (或
    d
    )时,您将增加
    down\u计数,但前提是前一个字母不是
    d
    (或
    d

    因此,我引入了新的布尔(逻辑)变量
    prev\u d
    ,用于测试前一个字符是否为
    d
    (或
    d
    )。对于每次迭代,我都将该值设置为
    True
    False
    ),因为它将在下一次迭代中使用:

    down\u count=0
    prev_d=False#从前面没有“d”(或“d”)开始
    s=输入(“输入字符串:”)
    对于s中的c:
    如果c.lower()==“d”:
    如果不是上一个:#这与“如果上一个=False:
    向下计数+=1
    prev_d=True#对于下一次迭代:前一个字符是'd'或'd'
    其他:
    prev_d=False#对于下一次迭代:prev。char不是“d”或“d”
    打印(向下计数)
    
    if
    s的形式应为
    c==“u”或c==“u”
    …替换
    l[c]
    只需
    c
    。谢谢,错误消失了,但我得到了输入u和d的次数,但没有得到“d”连续出现的次数。例如:“UUUDDDDUUUDDD”答案应该是2。由于“d”在给定字符串中连续重复两次,谢谢,错误消失了,但我得到了输入u和d的次数,但不是how多次“d”连续出现。例如:“uuudddduuudddd”答案应该是2。as“d”在给定字符串中连续重复两次,但您似乎没有编写任何代码来实现这一点。您能告诉我吗?谢谢,我对编程非常陌生。想练习一些程序。Donno如何将逻辑写入连续字母在一个字符串中。你能帮助我吗?“ZaHIdHaMier--我写了一个新的答案,它解决了你的目标。非常感谢你。):你真棒。ZaHIdHaMier--不欢迎。请考虑接受这个答案(点击检查标记)和/或投票表决(点击上三角箭头),如果它对你有用。