蟒蛇“矩阵”问题
我有输入数据(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在编程中,如果你不知道如何做某件事,请将其分解成更小的部分。我不完全清楚你想要什么,所以我将假设它如下。
您有一个列表,
您需要一个新列表来占据该列表的前三分之一。但你不知道该怎么做。所以把它分解一下。那么你怎么做呢?让我们从指令列表开始。
然后尝试按照说明进行操作。首先,我们如何在Python中创建一个空列表?
接下来,我们需要多次重复本质上相同的事情。我们如何在Python中多次重复相同的操作?
好的,但是我们需要重复多少次?显然,您想重复 x 长度的三分之一。我们如何弄清楚这一点?再次,将问题分解为多个步骤,
我们如何在 python 中找到某个东西的长度?
我们如何除以三?
好的,回到循环
现在,如何从 x 中取出 idx 元素并将其放入 y 中?再次,让我们将其分解为几个步骤:
我们如何在 python 中获取列表的第 idx 个元素?
我们如何将某些内容添加到列表中将
它们组合起来:
因此我们有了最终的代码:
在编程中,您需要能够将问题分解为您知道如何解决的部分。如果您做不到这一点,您可能正在尝试超出您技能水平的事情。如果您不知道如何执行简单的任务,我们很乐意为您提供帮助,或者建议您更好地完成现有任务。
不幸的是,你的情况看起来像是一个一直在收集神奇代码片段但不了解代码的作用的人。我无法读懂你的想法,我不知道这是不是真的。但如果您需要帮助,您将需要证明您已经尝试过。示例包括:
我更愿意帮助初学者。我曾经也是其中之一。我没有像 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
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.
Then try to follow the instructions. Firstly, how do we create an empty list in python?
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?
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
How do we find the length of something in python?
How do we divide by three?
Okay, back to the loop
Now, how take the idx element from x and put it into y? Again, let's break it down into steps:
How do we get the idxth element of a list in python?
How do we add something to a list
combine them:
And thus we have the final code:
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:
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.
我不知道你的具体目标是什么,但我认为你应该使你的浮标类型正确
I do not know what exactly you aim at, but I think you should make your floats proper type