添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
list1=[1,2,3,4,5]
list2=[2001,2002,2003,2004,2005]
list3=[0.05, -0.07, -0.08, 0.004, -0.02, -0.03, 0.03, 0.02, 0.06]

So these are my 3 lists that are used

for l2,l1 in zip(list2,list1):
    if list3>= 0.04: #where the type error issue has occurred
        print(f"{l2} and {l1}")

Any ways to fix this coding or improve it?

Unfortunately it's hord to understand what do you want to achieve. You zip two lists and iterate over the pairs, but list3 is not an element but an entire third list which you try to compare to 0.04 on every iteration. – Shmygol Aug 10, 2021 at 15:11 @Shmygol tbh this is a project im working on and im quite confuse as of what im doing as well but the output im suppose to get is for example since list3[0] >=0.04, the output will be "1 and 2001" and if others is more than or equal to 0.04 it will be printed as well – Student.py Aug 10, 2021 at 15:16 What does represent the values in list1? BTW it's a good example why you should use proper names. list2 looks like a list of years, so call it years, list3 is probably temperatures. And what is list1? I have fealing, that sizes should match. – Shmygol Aug 10, 2021 at 15:28 @Shmygol no worries I got the answers to my question already thanks for tryin to help , appreciate it. :) – Student.py Aug 10, 2021 at 15:58 print(f"{l2} and {l1}")

Notice that lengths of list1 and list2 are smaller than the length of list3. Therefore this for loop won't iterate through the end of the list3.
Output will be:

2001 and 1
                @Student.py please pay attention, that the half of the elements of list3 is just ignored in this snipped.
– Shmygol
                Aug 10, 2021 at 15:30
                Yeah i tbh list 1 and list 2 i had to make it easier to type so values are actually different from the real list I want to see some examples on how to approach this questions. I dont want to copy and paste what I have so i can type and change the stuff myself to learn :)
– Student.py
                Aug 10, 2021 at 15:33

I am not sure what you want to do but I think you want to print the list1 and list2 elements if the list3 element is greater than 0.04.

If you want to do the above thing, you can use the enumerate for it. The enumerate add the list index to the for loop.

Code:

list1 = [1, 2, 3, 4, 5]
list2 = [2001, 2002, 2003, 2004, 2005]
list3 = [0.05, -0.07, -0.08, 0.004, -0.02, -0.03, 0.03, 0.02, 0.06]
for idx, l3 in enumerate(list3):
    if idx > len(list1) or idx > len(list2):
        break
    if l3 >= 0.04:
        print(f"{list1[idx]} and {list2[idx]}")

Output:

>>> python3 test.py 
1 and 2001