蟒蛇“矩阵”问题

发布于 2024-10-26 06:15:14 字数 990 浏览 4 评论 0原文

我有输入数据(test.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

代码:

import csv

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

现在我的输出是:

'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']]

其中U1和U2....是类


现在我的问题: 我现在如何制作for循环(或其他任何东西),我可以在其中调用U1,U2,....(我的数据类)并接收数据。

我喜欢做的是在这种情况下:

first1 = 1/3 * U1 (1/3 of data U1) = ?
first2 = 1/3 * U2 (1/3 of data U2) = ?
second1 = 2/3 * U2 (2/3 of data U1) = ?
second2 = 2/3 * U2 (2/3 of data U2) = ?

我需要这个矩阵(first1,first2,second1,second2)来进行下一步计算...

I have input data(test.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

code I have:

import csv

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

Now my output is:

'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']]

Where U1 and U2....is class


NOW THERE IS MY PROBLEM:
How can I now make for loop(or anything else), where I can call this U1, U2,....(my classes from data) and receive data.

What I like to do is in this case:

first1 = 1/3 * U1 (1/3 of data U1) = ?
first2 = 1/3 * U2 (1/3 of data U2) = ?
second1 = 2/3 * U2 (2/3 of data U1) = ?
second2 = 2/3 * U2 (2/3 of data U2) = ?

I need this matrix(first1,first2,second1,second2) for next calculating...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不弃不离 2024-11-02 06:15:14

在编程中,如果你不知道如何做某件事,请将其分解成更小的部分。我不完全清楚你想要什么,所以我将假设它如下。

您有一个列表,

x = [4.5, 6.9, 7.5, 9.2, ...]

您需要一个新列表来占据该列表的前三分之一。但你不知道该怎么做。所以把它分解一下。那么你怎么做呢?让我们从指令列表开始。

  1. 创建列表, y
  2. 取出 x 中的第一项并将其放入 y
  3. 取出 x 中的第二项并将其放入 y
  4. 取出 x 中的第三项并将其放入 y
  5. 当 y 包含 x 中的前三分之一项时停止

然后尝试按照说明进行操作。首先,我们如何在Python中创建一个空列表?

y = []

接下来,我们需要多次重复本质上相同的事情。我们如何在Python中多次重复相同的操作?

for idx in range(number of times to repeat):

好的,但是我们需要重复多少次?显然,您想重复 x 长度的三分之一。我们如何弄清楚这一点?再次,将问题分解为多个步骤,

  1. 找到 x 的长度
  2. ,将该长度除以 3

我们如何在 python 中找到某个东西的长度?

len(x)

我们如何除以三?

len(x) // 3

好的,回到循环

for idx in range( len(x) // 3 ):

现在,如何从 x 中取出 idx 元素并将其放入 y 中?再次,让我们将其分解为几个步骤:

  1. 获取 x 的第 idx 个元素
  2. 将该元素添加到 y

我们如何在 python 中获取列表的第 idx 个元素?

x[idx]

我们如何将某些内容添加到列表中将

y.append( object to add )

它们组合起来:

y.append( x[idx] )

因此我们有了最终的代码:

y = []
for idx in range( len(x) // 3 ):
    y.append( x[idx] )

在编程中,您需要能够将问题分解为您知道如何解决的部分。如果您做不到这一点,您可能正在尝试超出您技能水平的事情。如果您不知道如何执行简单的任务,我们很乐意为您提供帮助,或者建议您更好地完成现有任务。

不幸的是,你的情况看起来像是一个一直在收集神奇代码片段但不了解代码的作用的人。我无法读懂你的想法,我不知道这是不是真的。但如果您需要帮助,您将需要证明您已经尝试过。示例包括:

  1. 您编写的代码没有实现您想要的功能
  2. 您读到的您不理解的文档
  3. 讨论您尝试将问题分解为更小的问题

我更愿意帮助初学者。我曾经也是其中之一。我没有像 Stack Overflow 这样的资源,所以必须自己解决所有问题。我今天的编码能力可能很好,因为没有人给我答案。当您遇到困难时,我们很乐意为您提供帮助,但您似乎被困的程度比移动时还要多,这才是真正的问题。

你老向我们要鱼,你需要学会钓鱼。

In programming, if you don't know how to do something, break it into smaller pieces. I'm not entirely clear on what you want, so I'm going to assume its the following.

You have a list

x = [4.5, 6.9, 7.5, 9.2, ...]

You want a new list that takes the first third of that list. But you don't know how to do it. So break it down. So how do you this? Let's start with a list of instructions.

  1. Create the list, y
  2. Take the first item in x and put it in y
  3. Take the second item in x and put it in y
  4. Take the third item in x and put it in y
  5. Stop when y contains the first third of items in x

Then try to follow the instructions. Firstly, how do we create an empty list in python?

y = []

Next, we need to repeat essentially the same thing a bunch of different times. How do we repeat the same action many times in python?

for idx in range(number of times to repeat):

Ok, but how many times do we need to repeat? Clearly, you want to repeat it one third of the length of x. How do we figure that out? Again, take the problem and break it down into steps

  1. find the length of x
  2. divide that length by 3

How do we find the length of something in python?

len(x)

How do we divide by three?

len(x) // 3

Okay, back to the loop

for idx in range( len(x) // 3 ):

Now, how take the idx element from x and put it into y? Again, let's break it down into steps:

  1. Get the idxth element of x
  2. Add that element to y

How do we get the idxth element of a list in python?

x[idx]

How do we add something to a list

y.append( object to add )

combine them:

y.append( x[idx] )

And thus we have the final code:

y = []
for idx in range( len(x) // 3 ):
    y.append( x[idx] )

In programming you need to be able to break down your problem into pieces that you know how to solve. If you can't do this you are probably attempting something beyond your skill level. We are happy to help if you don't know how to perform a simple task or to suggest betters of doing what you have.

Unfortunately, you case looks like someone who has been collecting magic pieces of code without understanding what the code does. I can't read your mind, I don't know if that's true. But if you want assistance, you are going to need to demonstrate what you have tried. Examples include:

  1. Code that you wrote that didn't do what you wanted
  2. Documentation that you read that you didn't understand
  3. Discussion of your attempt to break down the problem into smaller problems

I am more then willing to help a beginning coder. I used to be one. I didn't have a resource like Stack Overflow and had to figure everything out myself. My ability to code today is probably good because nobody just gave me answers. We are happy to help you when stuck, but you seem to be stuck more then you are moving, that's the real problem here.

You keep asking us for fish, you need to learn to fish.

奢华的一滴泪 2024-11-02 06:15:14

我不知道你的具体目标是什么,但我认为你应该使你的浮标类型正确

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

I do not know what exactly you aim at, but I think you should make your floats proper type

import csv
reader = csv.reader(open('test.data', 'r'))
result = {}
for row in reader:
    uclass, values = row[-1], [float(item) for item in  row[:-1]]
    if uclass in result:
        result[uclass].append(values)
    else:
        result[uclass] = [values]
print(result)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文