Python Forum
python 2D array creation and print issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python 2D array creation and print issue
#1
str1 = "snbdsbhaswertybvbx"
str2 = "bhzbhcgsdwertyndvdddwsdws"
str1len = len(str1)
str2len = len(str2)

arr = [[0]*str2len]*str1len

for i in range(0,str1len):
    for j in range(0,str2len):
        if str1[i] == str2[j]:
            if i == 0 or j == 0:
                arr[i][j] = 1
    break

for row in arr:
    print(row)
When printing the arr
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1]

But it is showing wrong as i have already broken the loop after first iteration..

any help
Reply


Messages In This Thread
python 2D array creation and print issue - by developerbrain - May-15-2019, 05:47 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  dynamic object creation using python gary 7 1,304 Oct-15-2022, 01:35 PM
Last Post: Larz60+
  Question: print issue python202209 3 977 Sep-18-2022, 11:51 AM
Last Post: jefsummers
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 7,213 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  how to print all data from all data array? korenron 3 2,485 Dec-30-2020, 01:54 PM
Last Post: korenron
  How to print array indexes? Mark17 6 2,804 Aug-03-2020, 04:26 PM
Last Post: Mark17
  Issue with creating an array of pixel data for PNG files in Google Colab The_Sarco 1 1,947 Apr-29-2020, 12:03 AM
Last Post: bowlofred
  Socket creation speed difference Python 2.7 / Python 3.7 PiAil 1 2,473 Feb-13-2019, 01:55 PM
Last Post: PiAil
  Print 2D Array dragu_stelian 3 2,753 Jan-27-2019, 06:09 PM
Last Post: aakashjha001
  python rrdtool graph creation issue anna 0 4,450 Sep-10-2018, 11:10 AM
Last Post: anna
  Issue using print statement in a thread bweiss1258 9 5,450 Jan-16-2018, 02:07 AM
Last Post: bweiss1258

Forum Jump:

User Panel Messages

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