Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9006419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:24:24+00:00 2026-06-16T01:24:24+00:00

So let’s say I got a list of 200 numeric values in List A.

  • 0

So let’s say I got a list of 200 numeric values in List A. I want to make a list B that splits list A in clusters of 4, so I would get 50 clusters. In list B I want to make a list for every cluster of 4 values, so it would contain 50 lists in list B.

I’ll explain my problem using my source:

    from pprint import pprint

    FileValuelist = []

    def DetermineClusterNumber(File):               #determine digits in a cluster
            Lines = open(File, "r")
            i = 0 # used for iterating through the lines
            FirstLine = Lines.readline()
            for char in FirstLine:                  # read through first line, till hyphen.
                    if char != "-":
                            i += 1
                    elif char == "-":
                            return i # Return number of digits in the cluster 

    def ReadLines(File, Cluster_Number):
            Lines = open( File, "r" )
            for Line in Lines:
                    for char in Line:
                            if char != "-":
                                            FileValuelist.append(char)

    def RemoveNewlines(Rawlist):
            for x in range(len(FileValuelist)-9):
                    if FileValuelist[x] == "\n":
                            FileValuelist.remove(FileValuelist[x])
                    if FileValuelist[x] == "\r":
                            FileValuelist.remove(FileValuelist[x])


    Cluster_Number = DetermineClusterNumber("Serials.txt") # Amount of chars in a cluster. Example: 1234-2344-2345. clusternumber = 4
    ReadLines ("Serials.txt", Cluster_Number)
    RemoveNewlines(FileValuelist)

    list_iterater = 0

    FinishedList = ([[None]*(Cluster_Number)])*((len(FileValuelist)))
    amount_of_clusters = len(FileValuelist)/Cluster_Number

    for x in range(0, amount_of_clusters):
            for y in range(0, Cluster_Number):
                    FinishedList[x][y] = FileValuelist[list_iterater]
                    list_iterater += 1

    pprint(FinishedList)

With serials.txt containing:

    4758-8345-1970-4486-2348
    2346-1233-3463-7856-4572
    6546-6874-1389-9842-4185
    9896-4688-4689-6455-4712
    9541-5621-8414-7465-5741
    4545-9959-5632-6845-1351
    5643-2435-5854-6754-8749
    7892-3457-8923-4572-5397
    5623-5698-5468-5476-9874
    8762-3487-6123-7861-2679

When I run this, I would expect it to print serials.txt in a list, containing the 50 split 50 lists. However when I run it, it prints out [2,6,7,8] fifty times. That’s the last cluster. So I guess the problem is somewhere located at line 39.
I already tried to look what value was assigned to FinishedList at line 41, and it was the right value everytime (so not 2,6,7,9, like when the list is printed out). I already rechecked the x and y iteraters (yes, I do know it’s spelled iterator) and they are correct too.

So what is wrong in my code that makes it print the last cluster fifty times?
I’m using Python 2.7 by the way, if you couldn’t tell.

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:24:25+00:00Added an answer on June 16, 2026 at 1:24 am

    This line isn’t doing what you think it’s doing:

    FinishedList = ([[None]*(Cluster_Number)])*((len(FileValuelist)))
    

    It’s storing the reference to the same [None, None, None... None] list, len(FileValuelist) times (the * operator basically performs a shallow copy). If you want to ensure that it creates new lists, the easiest way is to use a list comprehension.

    FinishedList = [[None] * Cluster_Number for _ in xrange(len(FileValuelist))]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let's say I have thousands of users and I want to make the passwords
Let say I want to create an iOS app that download music files from
Let's say I want to write a function that validates an email address with
let's say i want to build a smartphone app that tells a user when/where
Let's say that I've got a sheet - number one - with over 5000
Let's say I've got a collection of 10 million documents that look something like
Let's say that each Product has a category. I want to ask the Users
Let me explain best with an example. Say you have node class that can
Let's say that I have a SQLite database that I create in a separate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.