Python arrangement

Asked 2 years ago, Updated 2 years ago, 18 views

arr1 = []
arr2 = []
arr3 = []

for i in arr1:
  for j in i:
     if j==0:
         print('', end='')
     else:
         print('*', end='')
  print()

for i in arr2:
  for j in i:
     if j==0:
         print('', end='')
     else:
         print('*', end='')
  print()

for i in arr3:
  for j in i:
     if j==0:
         print('', end='')
     else:
         print('*', end='')
  print()

When I print out arr1, arr2, arr3, I want to do the last line and turn it over to the side without opening it, what should I do?

I thought I could do end='\t', but it doesn't work well even if I put it in here and there<

python

2022-09-22 18:44

1 Answers

Python's zip (*iterables) allows you to tour multiple lists at once.

list1 = [1, 2, 3, 4]
list2 = [100, 120, 30, 300]
list3 = [392, 2, 33, 1]
answer = []
for i, j, k in zip(list1, list2, list3):
   answer.append( i + j + k )

If you do not modify the original logic, apply it

arr1 = [[0,1,1], [0,0,1],[0,0,0]]
arr2 = [[1,1,1], [1,0,0], [0,0,0]]
arr3 = [[1,0,1], [1,1,0], [0,0,0]]

for i, j, k in zip(arr1, arr2, arr3):
    str1, str2, str3 = '', '', ''
    for a,b,c in zip(i,j,k):

     if a==0:
         str1 += ''
     else:
         str1 += '*'

     if b==0:
         str2 += ''
     else:
         str2 += '*'

     if c==0:
         str3 += ''
     else:
         str3 += '*'
    print(str1, str2, str3)

I think we can do it like this!


2022-09-22 18:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.