最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - 2-d for loop iterates one letter at a time - Stack Overflow

programmeradmin1浏览0评论

I've been working on a code which will flip the order of elements backwards using 2 functions, for some reason the first function which can normally print out two or more lists with spaces between each element is putting a space between each letter of the flipped list, could you tell me why it's doing so

cities = [["Miami", "Atlanta", "Dallas", "Los Angeles"], ["New York", "Chicago", "Portland", "Sacramento"]]

def printList(nightwatch):
    for r in nightwatch:  
        for c in r:        
            print(c, end=' ')  
def flipOrder(listy):
    temp = []
    for r in range (len(listy)):
        for c in range (len(listy[r])):
            temp.insert (0, listy[r][c])
    listy = temp
    printList(listy)
    
printList(cities)
print ()
print ()
print ()
flipOrder(cities)

I've tried implementing the first function into the second as well as messing with the c and r loop, however this usually just causes the code to stop working, ideally i'd like to know why this is processing the list one letter at a time

I've been working on a code which will flip the order of elements backwards using 2 functions, for some reason the first function which can normally print out two or more lists with spaces between each element is putting a space between each letter of the flipped list, could you tell me why it's doing so

cities = [["Miami", "Atlanta", "Dallas", "Los Angeles"], ["New York", "Chicago", "Portland", "Sacramento"]]

def printList(nightwatch):
    for r in nightwatch:  
        for c in r:        
            print(c, end=' ')  
def flipOrder(listy):
    temp = []
    for r in range (len(listy)):
        for c in range (len(listy[r])):
            temp.insert (0, listy[r][c])
    listy = temp
    printList(listy)
    
printList(cities)
print ()
print ()
print ()
flipOrder(cities)

I've tried implementing the first function into the second as well as messing with the c and r loop, however this usually just causes the code to stop working, ideally i'd like to know why this is processing the list one letter at a time

Share Improve this question asked Feb 7 at 17:17 formentera ladyformentera lady 1 New contributor formentera lady is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 1
  • Check out Python: for loops - for i in range(0,len(list) vs for i in list; the way you're handling list iteration in your flipOrder function is rather "unpythonic." Better would be for group in list: for city in group: temp.insert(0, city) – Anerdw Commented Feb 7 at 17:59
Add a comment  | 

2 Answers 2

Reset to default 1

Your printList function is double nested to deal with the two subLists in the cities List. But fliporder creates a single List (no-subs) so the nested loop in printList takes each character each time.

Here is a version which flips the order within each sublist then flips the sublists. It used a List comprehension for simplicity.

cities = [["Miami", "Atlanta", "Dallas", "Los Angeles"], ["New York", "Chicago", "Portland", "Sacramento"]]

def printList(nightwatch):
    for r in nightwatch:  
        for c in r:
            print(c, end=' ')  

def flipOrder(listy):
    listy = [x[::-1] for x in listy[::-1]]
    printList(listy)

    
printList(cities)
print ()
print ()
print ()
flipOrder(cities)

which gives:

Miami Atlanta Dallas Los Angeles New York Chicago Portland Sacramento 


Sacramento Portland Chicago New York Los Angeles Dallas Atlanta Miami 

The following line of code already iterates through the merged list named nightwatch ['Sacramento', 'Portland', 'Chicago', 'New York', 'Los Angeles', 'Dallas', 'Atlanta', 'Miami'] (Your flipOrder function merges both sublists to one):

for r in nightwatch:
...

Therefore, you don't need this:

for c in r:
...

Since it just iterates through every single character and adds a space to it.

This code should work:

cities = [["Miami", "Atlanta", "Dallas", "Los Angeles"], ["New York", "Chicago", "Portland", "Sacramento"]]

def printList(nightwatch):
    for r in nightwatch:          
        print(r, end=' ')  
def flipOrder(listy):
    temp = []
    for r in range (len(listy)):
        for c in range (len(listy[r])):
            temp.insert (0, listy[r][c])
    listy = temp
    printList(listy)
    
printList(cities)
print ()
print ()
print ()
flipOrder(cities)
发布评论

评论列表(0)

  1. 暂无评论