Python Forum
[SOLVED] Same input different output
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Same input different output
#1
Question 
What's the reason?
def f(a, n):
	for i in range(n):
		   a[i][n // 2] = '*'
		   a[n // 2][i] = '*'
		   a[i][i] = '*'
		   a[i][n - i - 1] = '*'
	return '\n'.join([' '.join(i) for i in a])

n = 9
a = [['.'] * n for i in range(n)]
b = [['.'] * n] * n
print(a == b, '\n')
print(f(a, n), '\n')
print(f(b, n), '\n')
print(f(a, n) == f(b, n))
Output:

True

* . . . * . . . *
. * . . * . . * .
. . * . * . * . .
. . . * * * . . .
* * * * * * * * *
. . . * * * . . .
. . * . * . * . .
. * . . * . . * .
* . . . * . . . *

* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *

False
Reply
#2
Not the same input. a may equal b, but it is completely different. a contains n different lists. b contains n references to the same list.
antarling likes this post
Reply
#3
(Oct-25-2024, 11:16 PM)deanhystad Wrote: Not the same input. a may equal b, but it is completely different. a contains n different lists. b contains n references to the same list.

Thank you, i didn't expect that same looking lists can be different
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  interactive process for input and output maiya 1 627 Mar-27-2025, 08:40 AM
Last Post: Gribouillis
  output provide the filename along with the input file processed. arjunaram 1 1,532 Apr-13-2023, 08:15 PM
Last Post: menator01
  serial input/output barryjo 3 5,154 Dec-27-2021, 11:57 PM
Last Post: barryjo
  [SOLVED] Input parameter: Single file or glob? Winfried 0 2,078 Sep-10-2021, 11:54 AM
Last Post: Winfried
  [SOLVED] Good way to handle input args? Winfried 2 2,803 May-18-2021, 07:33 PM
Last Post: Winfried
  How to input & output parameters from command line argument shantanu97 1 3,662 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  Code giving same output no matter the input. Yort 2 4,788 Dec-20-2020, 05:59 AM
Last Post: buran
  Handling multi-input/output audio in python bor1904 4 4,953 Nov-04-2020, 08:25 AM
Last Post: CHLOVRL
  single input infinite output problem Chase91 2 2,729 Sep-23-2020, 10:01 PM
Last Post: Chase91
  My code is giving my an output of zero, no matter what value I input PiyushBanarjee 1 2,523 Jul-01-2020, 04:34 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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