简体   繁体   中英

Is there a way to print a list that moves to a new line after specific items in the list?

I want to make a program that can print out a piece of information and then another piece relating to the first. I have a list:

householdDecor= ['Potted Plant', 24.00, 'Painting', 35.30, 'Vase', 15.48, 
                 'Rug', 49.99, 'Fancy Bowl', 28.00]

And I want It so when I print the list it moves to a new line after each number. So that It looks like this:

Potted Plant 24.00
Painting 35.30
Vase 15.48
Rug 49.99
Fancy Bowl 28.00

Is there any way to do this instead of moving lines every item like you can with: print(householdDecor, sep = "\n") ?

The correct way to do this would be to have a list of two- tuple s rather than a flat list where the meaning varies by index.

Luckily, it's not hard to convert from the form you have to the form you want:

for item, price in zip(householdDecor[::2], householdDecor[1::2]):
    print(item, price)

You just slice the even and odd numbered elements separately, zip them together to make pairs, and print them as a pair (which puts in a newline implicitly).

A more magical looking, but somewhat more efficient version would be:

for item, price in zip(*[iter(householdDecor)]*2):
    print(item, price)

that uses zip to pull two items at a time from a single iterator over the list without requiring slicing (avoiding additional temporaries).

householdDecor= ['Potted Plant',24.00,'Painting',35.30,'Vase',15.48,'Rug',49.99,'Fancy Bowl',28.00]


for i in range(len(householdDecor)):
    if i%2==0:
        print('\n',end=' ')
    print(householdDecor[i],end=' ')

Output

 Potted Plant 24.0                                                                                                                   
 Painting 35.3                                                                                                                       
 Vase 15.48                                                                                                                          
 Rug 49.99                                                                                                                           
 Fancy Bowl 28.0

@shadowranger already provided two nice ways of slicing and dicing a list into a list of tuples.

Another approach that may be easier to understand (although not better per se):

householdDecor= ['Potted Plant',24.00,'Painting',35.30,'Vase',15.48,'Rug',49.99,'Fancy Bowl',28.00]

for item, price in [householdDecor[i:i+2] for i in range(0, len(householdDecor), 2)]:  # step 2
    print(item, price)

Also, just as a note, if you're really just printing the data, consider something like:

    print(f'{item:20}{price:>6}')

You can do it with a generic helper function that lets you iterate through the sequence is groups of two:

def pairwise(iterable):
    "s -> [[s0,s1], [s1,s2], [s2, s3], ...]"
    a, b = iter(iterable), iter(iterable)
    next(b, None)
    return zip(a, b)

for pair in pairwise(householdDecor):
    print(pair[0], pair[1])

This can be abstracted even further to do different group sizes like this:

def grouper(n, iterable):
    "s -> (s0,s1,s2,...sn-1), (sn,sn+1,sn+2,...s2n-1), (s2n,s2n+1,s2n+2,...s3n-1), ..."
    return zip(*[iter(iterable)]*n)

for pair in grouper(2, householdDecor):
    print(pair[0], pair[1])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM