How to iterate over each string in a list of strings and operate on it's elements

前端 未结 8 1926
别跟我提以往
别跟我提以往 2020-12-25 13:00

Im new to python and i need some help with this.

TASK : given a list --> words = [\'aba\', \'xyz\', \'xgx\', \'dssd\', \'sdjh\']

i need to com

相关标签:
8条回答
  • 2020-12-25 13:00

    You are iterating trough items in words but you have to iterate through item's length:

    words = ['aba', 'xyz', 'xgx', 'dssd', 'sdjh']
    c = 0
    for i in range(len(words)):
        w1 = words[i]
        if w1[0] == w1[len(w1) - 1]:
          c += 1
        print (c)
    
    

    In your case i[0] is 'aba' because i is calculated from items in words:

    words = ['aba', 'xyz', 'xgx', 'dssd', 'sdjh']
    c = 0
    for i in words:
    print(i)
    
    

    the output is:

    aba

    0 讨论(0)
  • 2020-12-25 13:04
    c=0
    words = ['challa','reddy','challa']
    
    for idx, word in enumerate(words):
        if idx==0:
            firstword=word
            print(firstword)
        elif idx == len(words)-1:
            lastword=word
            print(lastword)
            if firstword==lastword:
                c=c+1
                print(c)
    
    0 讨论(0)
  • 2020-12-25 13:07

    The suggestion that using range(len()) is the equivalent of using enumerate() is incorrect. They return the same results, but they are not the same.

    Using enumerate() actually gives you key/value pairs. Using range(len()) does not.

    Let's check range(len()) first (working from the example from the original poster):

    words = ['aba', 'xyz', 'xgx', 'dssd', 'sdjh']
        print range(len(words))
    

    This gives us a simple list:

    [0, 1, 2, 3, 4]
    

    ... and the elements in this list serve as the "indexes" in our results.

    So let's do the same thing with our enumerate() version:

    words = ['aba', 'xyz', 'xgx', 'dssd', 'sdjh']    
       print enumerate(words)
    

    This certainly doesn't give us a list:

    <enumerate object at 0x7f6be7f32c30>
    

    ...so let's turn it into a list, and see what happens:

    print list(enumerate(words))
    

    It gives us:

    [(0, 'aba'), (1, 'xyz'), (2, 'xgx'), (3, 'dssd'), (4, 'sdjh')]
    

    These are actual key/value pairs.

    So this ...

    words = ['aba', 'xyz', 'xgx', 'dssd', 'sdjh']
    
    for i in range(len(words)):
        print "words[{}] = ".format(i), words[i]
    

    ... actually takes the first list (Words), and creates a second, simple list of the range indicated by the length of the first list.

    So we have two simple lists, and we are merely printing one element from each list in order to get our so-called "key/value" pairs.

    But they aren't really key/value pairs; they are merely two single elements printed at the same time, from different lists.

    Whereas the enumerate () code:

    for i, word in enumerate(words):
        print "words[{}] = {}".format(i, word)
    

    ... also creates a second list. But that list actually is a list of key/value pairs, and we are asking for each key and value from a single source -- rather than from two lists (like we did above).

    So we print the same results, but the sources are completely different -- and handled completely differently.

    0 讨论(0)
  • 2020-12-25 13:07

    Use range() instead, like the following :

    for i in range(len(words)):
        ...
    
    0 讨论(0)
  • 2020-12-25 13:07
    for i,j in enumerate(words): # i---index of word----j
         #now you got index of your words (present in i)
         print(i) 
    
    0 讨论(0)
  • 2020-12-25 13:12

    The reason is that in your second example i is the word itself, not the index of the word. So

    for w1 in words:
         if w1[0] == w1[len(w1) - 1]:
           c += 1
         print c
    

    would the equivalent of your code.

    0 讨论(0)
提交回复
热议问题