Python Forum
iterate through a list with comparison of expression
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
iterate through a list with comparison of expression
#1
Hello everybody,

I have a question about the lists.

How can I go through a list and compare the contents of the list and if it is correct then instead of a 1 there should always be a 0

Thank you for your Help. Smile

wordList = ['hi', 'hello', 'this', 'that', 'is', 'of']
    
    
    for word in wordList:
        print(word)
        
    print() 
when expression is == "Hello" than it should be 1 other 0
Reply
#2
hi Alexandro,

I think this could be right for you :

wordList = ['hi', 'hello', 'this', 'that', 'is', 'of']

for word in wordList:
    if word == 'hello':
        print (1)
    else :
        print (0)
Reply
#3
Hello,
Thanks for the quick help
Please see the questions in the Output Area

wordList = ['hi', 'hello', 'this', 'that', 'is', 'of', 'cat']

for word in wordList:
if word == 'hello':
print (1)
elif word =='that':
print (0)

else :
print (0)

print (word)

output:
0
1
0
0 ???? Why That dont work"???
0
0
0
cat ??? Why that dont go????
Reply
#4
maybe I don't understand what you would like to obtain with you code

your code print "0" for the 4th "print" because you write that in your "print". if you want to print another value for "that", you just need to change

and for the last print, "cat" is the last value of "word" when your list is scanned

hope to help you
Reply
#5
(Jan-31-2019, 04:48 PM)Scorpio Wrote: maybe I don't understand what you would like to obtain with you code

your code print "0" for the 4th "print" because you write that in your "print". if you want to print another value for "that", you just need to change

and for the last print, "cat" is the last value of "word" when your list is scanned

hope to help you


Thak you for your Help,
My Question is.

How can I go through a list and I need for "this" and for "that" 1 statement

wordList = ['hi', 'hello', 'this', 'that', 'is', 'of', 'cat']
Reply
#6
(Jan-31-2019, 04:59 PM)Alexandro Wrote: How can I go through a list and I need for "this" and for "that" 1 statement

This is not very helpful. From that I personally can't understand what you want to accomplish. Please put some effort in defining your objective.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
(Jan-31-2019, 05:05 PM)perfringo Wrote:
(Jan-31-2019, 04:59 PM)Alexandro Wrote: How can I go through a list and I need for "this" and for "that" 1 statement
This is not very helpful. From that I personally can't understand what you want to accomplish. Please put some effort in defining your objective.

I agree with perfringo, I m trying to understand what you need but I Cann't do it

let us know more details
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 1,662 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  Regular expression: return string, not list Pavel_47 3 2,492 Jan-14-2021, 11:49 AM
Last Post: Pavel_47
  LIST or ARRAY Comparison and change of value nio74maz 0 1,698 Dec-21-2020, 05:52 PM
Last Post: nio74maz
  Pass results of expression to another expression cmdr_eggplant 2 2,283 Mar-26-2020, 06:59 AM
Last Post: ndc85430
  Trying to iterate through a list... t4keheart 2 1,671 Feb-07-2020, 08:15 PM
Last Post: t4keheart
  list of lists iterate only sends the last value batchenr 1 1,894 Sep-24-2019, 07:04 AM
Last Post: Gribouillis
  Iterate through a list of dictionary and append a new value. erina 1 2,096 May-16-2019, 09:55 AM
Last Post: perfringo
  modifying a list in an expression Skaperen 2 2,141 Feb-15-2019, 07:57 PM
Last Post: Skaperen
  List comparison in Python Nirmal 4 3,079 Sep-26-2018, 06:23 PM
Last Post: nilamo
  List comparison in Python Nirmal 13 6,457 Aug-09-2018, 04:30 PM
Last Post: Vysero

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020