0

I am trying to append a list of 4 letters in my number as a [[a, b, c, d]] type of list.
I am looping through a list and appending the letter to a temp list and then appending it to my main list to make it into a matrix. However, the main list is only storing the number (8, 26) for some reason

ciphertext = "asfgasgsaga"
counter = 0
templist = []
xyz = []

for abc in ciphertext:
    if(counter == 5):
        print(templist)
        xyz.append(templist)
        templist.clear()
        counter = 0
    else:
        templist.append(abc);
    counter += 1

print(xyz)

The result is for some reason giving [[8, 26]]

Nouman
  • 6,947
  • 7
  • 32
  • 60
lyncx
  • 680
  • 4
  • 8
  • 1
    your code is inconsistent with your description. – LiuChang Aug 25 '19 at 14:04
  • The result of this is not `[[8,26]]`. After this code, `xyz` equals `[[], []]`. Anyway, your main problem is that you are putting `templist` into `xyz` and then you are modifying `templist`. That same list is in `xyz`. You should put a copy into `xyz`. – zvone Aug 25 '19 at 14:05
  • @zvone So what im trying to do is have a list xyz as my main list which will contain list like [[a,b,c,d] , [e,f,g,h]] so what im trying to do there is create a temp list of [a,b,c,d] and add it do the main list xyz. So what am i doing wrong? – lyncx Aug 25 '19 at 14:30
  • 1
    If you put `templist` inside `xyz` and then modify `templist`, you are then modifying `xyz` as well. See https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly – zvone Aug 25 '19 at 15:00

2 Answers2

0

The result is not the same as your expected because there some concepts that you need to know about objects in Python:

Immutable Objects: int, float, complex, string, tuple, frozen set, bytes. These kind of data types can't be changed the value after it is created. So that when we assign to another variable, it will copy the value to new variable. E.g:

a = 123
b = a
a = 456
print(b) #123

Mutable Objects: list, dict, set, byte array. These can be changed the value after it is created. And when you assign to another variable, it basically just assign the reference to previous variable like so:

a = []
b = a
a.append(123)
print(b) #[123]

So back to your problem, you're using list to create a list with 4 characters and then append it into another list, it's not append the expected list but instead a reference to it. That's why you got unexpected result.

And about the logic of your code, there are something go wrong, because when counter you will miss 1 character. You actually can switch to use slicing in Python:

ciphertext = "asfgasgsaga"
xyz = [ciphertext[start:start + 4] for start in range(0, len(ciphertext), 4)]
print(xyz) #['asfg', 'asgs', 'aga']

I'm using List Comprehension to append to xyz instead of call append function, create step like: 0:4, 4:8, 8:12, ... voila

Hope that helpful for you.

Toan Quoc Ho
  • 3,266
  • 1
  • 14
  • 22
0

Just as @zvone says, don's use the same array and clear it, because they ref the same memory;

ciphertext = "asfgasgsaga"
counter = 0
templist = []
xyz = []

for abc in ciphertext:
    if(counter == 4):
        print(templist)
        xyz.append(templist)
        templist = []     # <--- use a new empty array
        counter = 0
    else:
        templist.append(abc);
        counter += 1

print(xyz)

Also, the correct logic(handle the letters less than 4) should be:

ciphertext = "asfgasgsaga"
counter = 0
templist = []
xyz = []

for abc in ciphertext:
    templist.append(abc);
    counter += 1
    if(counter == 4):
        print(templist)
        xyz.append(templist)
        templist = []
        counter = 0

if templist:
    xyz.append(templist)

print(xyz)

Just see @Toan Quoc Ho's answer, which should make more sense. Just leave the answer here to compare your origin logic.

Fogmoon
  • 569
  • 5
  • 16