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 4097976
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:14:34+00:00 2026-05-20T20:14:34+00:00

I have text file(test.data), which include some values and class name, for example 4.5,3.5,U1

  • 0

I have text file(test.data), which include some values and class name, for example

4.5,3.5,U1
4.5,10.5,U2
4.5,6,U1
3.5,10.5,U2
3.5,10.5,U2
5,7,U1
7,6.5,U1

I need to classify this data to rows(matrix), where 1,2 row etc is data, last one is class. So I started with this code:

reader = csv.reader(open('test.data', 'r'))
result = []
for row in reader:
    result.append(row)
print result

output:

[['4.5', '3.5', 'U1'], ['4.5', '10.5', 'U2'], ['4.5', '6', 'U1'], ['3.5', '10.5', 'U2'], ['3.5', '10.5', 'U2'], ['5', '7', 'U1'], ['7', '6.5', 'U1']]

This all work ok, but now I need from this data make matrix classification. In this case I want to make matrix:

test data=[data1, data2,.....,class name1]
test data2=[data1, data2,.....,class name2]...

I need this “matrix”(test data,test data2), because I will then choose from every test data just 2/3 data which will be named “choosen“, other 1/3 data must stay in test data,….

So what I need as output:

choosen=[data,data,......class name1]         # 2/3 from every **test.data**
test data=[data1, data2,.....,class name1]    # other 1/3 from test data
test data2=[data1, data2,.....,class name1]   # other 1/3 from test data 2

.

. . . .

.

Many thanks for help


EDIT2:

If I use your code I get:

{
    'U1': [
              ['4.5','3.5'],
              ['4.5','6'],
              ['5','7'],
              ['7','6.5']
          ],
    'U2': [
              ['4.5','10.5'],
              ['3.5','10.5'],
              ['3.5','10.5']
          ]
}

But I don’t have everytime this data:

4.5,3.5,U1
4.5,10.5,U2
4.5,6,U1
3.5,10.5,U2
3.5,10.5,U2
5,7,U1
7,6.5,U1

I have also:

4.5,3.5,4.5,10.5, U1
3.5,10.5,3.5,10.5,U2
4.5,12.5,3.5,12.5,U2

……
(so I don’t know that class is on second row as you write on your code), but I know that last row is CLASS

So how can I change your code:

reader = csv.reader(open('test.data', 'r'))
result = {}
for row in reader:
    uclass=row[2]                      #-------> must be last row not second !!!!
    if result.has_key(uclass):
        result[uclass].append([row[0],row[1]])   #---->not just 2 row's, on other data I have for example 5 rows..
    else:
        result[uclass]=[[row[0],row[1]]]        #---->not just 2 row's, on other data I have for example 5 rows..
print repr(result)
  • 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-05-20T20:14:34+00:00Added an answer on May 20, 2026 at 8:14 pm

    Edit: Original code snippet modified to handle N-column input (last is class). This requirement was mentioned by the OP in a later reply.

    reader = csv.reader(open('test.data', 'r'))
    result = {}
    for row in reader:
        uclass=row[-1]
        if result.has_key(uclass):
            result[uclass].append(row[0:-1])
        else:
            result[uclass]=row[0:-1]
    print repr(result)
    

    I’m still not completely sure about the second half of your problem.
    Something is missing in how you explained it perhaps.

    If I understand this correctly you want a different list for each class?

    If this is the case a dictionary should do what you want:

    reader = csv.reader(open('test.data', 'r'))
    result = {}
    for row in reader:
        uclass=row[2]
        if result.has_key(uclass):
            result[uclass].append([row[0],row[1]])
        else:
            result[uclass]=[[row[0],row[1]]]
    print repr(result)
    

    result will look like:

    {
        'U1': [
                  ['4.5','3.5'],
                  ['4.5','6'],
                  ['5','7'],
                  ['7','6.5']
              ],
        'U2': [
                  ['4.5','10.5'],
                  ['3.5','10.5'],
                  ['3.5','10.5']
              ]
    }
    

    For the data skipping you can use the list slice skip option available in newer Pythons:

    someList[<start>:<end>:<skip>]
    

    so on a list like:

    someList=[0,1,2,3,4,5,6,7,8,9]
    

    using skip sliceing such as:

    someList[0:10:2]
    

    gives:

    [0,2,4,6,8]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file with dos elements - hex(00) for example. I need
I have a text file of this format: L O A D C A
Suppose I have a text file with data separated by whitespace into columns. I
i have a java ee project which has a text file that uses a
I have a text file on my local machine that is generated by a
I have a text file in the root of my web app http://localhost/foo.txt and
I have a text file where I want to change only the first line
I have a text file of URLs, about 14000. Below is a couple of
I have a text file that contains a long list of entries (one on
I have a text file that contains localized language strings that is currently encoded

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.