【问题标题】:How can I use a list in a for in range loop?如何在 for in range 循环中使用列表?
【发布时间】:2020-01-08 19:49:49
【问题描述】:
consonant_letters = ['B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'X', 'Z']
for x in range(9999):
    for y in range(consonant_letters):
        for z in range(consonant_letters):
            for a in range(consonant_letters):
                print(f'{x} {y} {z} {a}')

*我得到的是这个

TypeError: 'list' 对象不能被解释为整数

*我正在尝试打印西班牙车牌系统中的所有数字,将其转换为列表,最后,执行输入功能,用户输入某个车牌,程序会告诉他有多少车牌直到这个车牌系统结束。

*编辑:我现在发现我唯一剩下但不知道的是如何制定输入函数。这是解决方案:

consonant_letters = ['B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M',    
'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'X', 'Z']

for x in range(10000):
    break
    if x < 1000 and x > 99:
        x = f"0{x}"
    elif x < 100 and x > 9:
        x = f"00{x}"
    elif x < 10:
        x = f"000{x}"
    for y in consonant_letters:
        for z in consonant_letters:
            for a in consonant_letters:
                print(f'{x} {y} {z} {a}')

*我尝试引入输入功能,以便用户可以输入某个车牌号,我会检测它是否存在但有问题。 这是我尝试过的:

`consonant_letters = ['B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L',      
'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'X', 'Z']
for x in range(10000):
if x < 1000 and x > 99:
    x = f"0{x}"
elif x < 100 and x > 9:
    x = f"00{x}"
elif x < 10:
    x = f"000{x}"
for y in consonant_letters:
    for z in consonant_letters:
        for a in consonant_letters:
            list_1 = f'{x} {y}{z}{a}'
            license_plate_user = input("Write the license plate: ")
            print(license_plate_user in list_1)`

*当我介绍不是 0000 BBB(第一个)的任何其他车牌时,它显示为 False。我知道这意味着循环只会执行一次,但我不知道如何修复它。

*编辑;我想出了如何执行循环,然后制定输入,但我还有一个问题。有没有我可以在知道特定车牌的情况下编写的操作来知道还剩下多少?

consonant_letters = ['B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M',   
'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'X', 'Z']
for x in range(10000):
if x < 1000 and x > 99:
    x = f"0{x}"
elif x < 100 and x > 9:
    x = f"00{x}"
elif x < 10:
    x = f"000{x}"
for y in consonant_letters:
    for z in consonant_letters:
        for a in consonant_letters:
            list1 = f'{x} {y}{z}{a}'
            if "9999 ZZZ" in list1:
                license_plate_user = input("Write the license plate: ")
                license_plate_user in list1
                if license_plate_user:
                    print("Operation I do not know yet")
                else:
                    print("Wrong values")

*编辑:我找到了一种方法,但它不起作用,我不知道为什么:

consonant_letters = ['B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 
'N', 'P', 'R', 'S', 'T', 'V', 'X', 'Z']
total_number_of_license_plates = 80000000
for x in range(10000):
    if x < 1000 and x > 99:
        x = f"0{x}"
    elif x < 100 and x > 9:
        x = f"00{x}"
    elif x < 10:
        x = f"000{x}"
    for y in consonant_letters:
        for z in consonant_letters:
            for a in consonant_letters:
                list1 = f'{x} {y}{z}{a}'
                if "9999 ZZZ" in list1:
                    license_plate_user = input("Write the license   
plate: ")
                    license_plate_user in list1
                    if license_plate_user:
                        print(list1.index(license_plate_user))
                        license_plates_left = 
total_number_of_license_plates - list1.index(license_plate_user)
                        print(f'There are {license_plates_left} license 
plates left')
                    else:
                        print("Wrong values")

我得到的是:

ValueError: 未找到子字符串

【问题讨论】:

  • 你的意思可能是for y in consonant_letters:
  • 只是不要使用范围,按原样使用列表

标签: python python-3.x list python-requests


【解决方案1】:

range 接受一个或多个整数作为输入。如果您不确定如何使用给定的单词,您可以启动 python intrepeter 并询问它 - 在这种情况下:

help(range)

在将所有range(consonant_letters) 替换为consonant_letters 后,您的代码将起作用,但我想指出您的任务是使用itertools.product 的理想场所。考虑以下示例:

import itertools
x = ['A','B','C']
for x1,x2,x3 in itertools.product(x,repeat=3):
    print(f'{x1} {x2} {x3}')

输出:

A A A
A A B
A A C
A B A
A B B
A B C
A C A
A C B
A C C
B A A
B A B
B A C
B B A
B B B
B B C
B C A
B C B
B C C
C A A
C A B
C A C
C B A
C B B
C B C
C C A
C C B
C C C

如您所见,它允许您使用一个for-loop,而不是(在这种情况下)3 个嵌套的for-loop。

【讨论】:

  • 所以你基本上是说如果我使用“itertools.product”它是相同的结果但更简单,对吧?如果我的问题很明显,我很抱歉,但我是新手,我正在尽力而为。
  • 是的,还要注意顺序 - 使用help(itertools.product) 措辞以类似于里程表的方式循环(每次迭代时最右边的元素都会改变)。 因此,如果您替换 @ 987654331@ 在我的 ['0','1','2','3','4','5','6','7','8','9'] 示例中,您将获得从 0 0 09 9 9(包括)按升序排列的所有数字。
  • 100% 理解。我有最后一个问题。如果我已经弄清楚如何知道用户介绍的车牌(输入功能)还剩下多少个车牌,但它不起作用?请查看我对这篇文章的最新更新,如果你能帮助我,那就太好了,因为我现在被困住了。 @Daweo
【解决方案2】:

你不能在列表中使用 range() 使用这个:

for x in range(9999):
    for y in consonant_letters:
        for z in consonant_letters:
            for a in consonant_letters:
                print(f'{x} {y} {z} {a}')

【讨论】:

  • @Guillermo - Amir 的回答会为您解决。原因:range 需要一个整数作为输入。所以你也可以使用类似:for y in range(len(consonant_letters)),它返回一个整数。但是你也可以直接遍历一个列表(比如 Amir 显示的)。
  • 如果我想添加一个输入功能,让用户输入某种数字和字母的组合,在这种情况下是4个数字和3个字母,所以我可以打印有多少个车牌剩下? (编号,而不是许可证)
【解决方案3】:

你可以用 one 循环来做到这一点:

from itertools import product

for x, (y, z, a) in product(range(9999), product(consonant_letters, repeat=3)):
    print(f'{x} {y} {z} {a}')

【讨论】:

  • 如果我想添加一个输入功能,让用户输入某种数字和字母的组合,在这种情况下是4个数字和3个字母,这样我就可以打印有多少个车牌剩下? (编号,而不是许可证)
猜你喜欢
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 2016-01-26
  • 1970-01-01
  • 2016-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多