类型错误:比较循环的整数和列表

发布于 2025-02-06 22:43:38 字数 421 浏览 2 评论 0原文

python在这里。 如何解决比较字典循环中的列表和int错误,请提供帮助!

from random import *
passenger = list(range(1,51))

timelist = []
for i in range(1,51):
    n = randint(15,51)
    timelist.append(n)
    
passenger_time = {'passenger':passenger, 'timelist':timelist}

for x in passenger_time.values():
    if 40 > x > 15:
        print('passenger {} accepted'.format(y))
    else:
        print('not accepted')

Python beginner here.
How can you solve comparing list and int error in for loop of a dictionary, please help!

from random import *
passenger = list(range(1,51))

timelist = []
for i in range(1,51):
    n = randint(15,51)
    timelist.append(n)
    
passenger_time = {'passenger':passenger, 'timelist':timelist}

for x in passenger_time.values():
    if 40 > x > 15:
        print('passenger {} accepted'.format(y))
    else:
        print('not accepted')

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

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

发布评论

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

评论(2

洋洋洒洒 2025-02-13 22:43:38

我怀疑您想要的是:

for pass, time in zip(passenger_times['passenger'], passenger_times['timelist']):
    if 15 < time < 40:
        print(f'passenger {pass} is accepted')

这在两个列表中并行迭代。它检查一个列表中的值,然后从另一个列表中打印相应的值。

您的代码只是在字典中的两个值迭代。第一次,x包含整个pastenger列表,第二次包含整个timelis list。做15&lt是没有意义的; x&lt; 40x是列表时。

I suspect what you want is:

for pass, time in zip(passenger_times['passenger'], passenger_times['timelist']):
    if 15 < time < 40:
        print(f'passenger {pass} is accepted')

This iterates over the two lists in parallel. It checks the value in one of the lists, then prints the corresponding value from the other list.

Your code is just iterating over the two values in the dictionary. The first time, x contains the entire passenger list, the second time it contains the entire timelis list. It makes no sense to do 15 < x < 40 when x is a list.

小巷里的女流氓 2025-02-13 22:43:38

您能否再解释一下您要做的事情(您要比较什么)?

*并写下您的书面:

if (x < 40) and (x > 15):
    ...

它可能在python中起作用,就像您写的那样,但是如果您要使用其他语言,您一定会遇到问题

Can you please explain a little more what you're trying to do (what you're trying to compare)?

*and write your if like this:

if (x < 40) and (x > 15):
    ...

It might work in python, the way you wrote it, but you're definately going to have a problem if you're gonna use other languages

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文