简体   繁体   English

如何用相同的 x 为 y 创建两个点?

[英]How create two points for y with the same x?

I'm trying to make a python code to create "double points", where one single value for x would assume two values for y for any situation.我正在尝试制作一个 python 代码来创建“双点”,其中 x 的一个值将在任何情况下假定 y 的两个值。 Here is a example of data:下面是一个数据示例:

1;5
2;-2
3;4
4;10.4
5;6

where first column(x), is just an index for position of the data (y).其中第一列 (x) 只是数据 (y) 的 position 的索引。

Plotting this data into x and y, would form a straight line.将这些数据绘制成 x 和 y,将形成一条直线。

The result I would like to have is:我想要的结果是:

1;5
1;-2
2;-2
2;4
3;4
3;10.4
4;10.4
4;6
5;6

This would generate a rectangular chart.这将生成一个矩形图表。 The value of x has its respective value of y, but that same value of x also has the value of y for its "x + 1" , and so for the rest of the data. x 的值具有其各自的 y 值,但相同的 x 值也具有其"x + 1"的 y 值,对于数据的 rest 也是如此。

I'm trying to do something like this, however unsuccessful:我正在尝试做这样的事情,但不成功:

l = [5, -2, 4, 10.4, 6]
m = []
i = 0
i = i+1
for i in range(len(l)):
    m.append(l[i])
    if i < len(l)-1:
        m.append([l[i][0], l[i+1][1]])
    print('{}, {}\n' .format(i, m[i]))

The error that appears is 'int' object has no attribute '__getitem__'出现的错误是'int' object has no attribute '__getitem__'

I am new to python and this is part of my studies, so if anyone can help me and can explain where I am going wrong, I would be grateful.我是 python 的新手,这是我学习的一部分,所以如果有人可以帮助我并解释我哪里出错了,我将不胜感激。

Ps the x column is not part of the data, is just an index (like time for example). Ps x 列不是数据的一部分,只是一个索引(例如时间)。 And the data (y column) can be negative, floats... all of types of numbers并且数据(y列)可以是负数,浮点数......所有类型的数字

can be done using list comprehension like below可以使用下面的列表理解来完成

from itertools import chain
ls = [5, -2, 4, 10.4, 6]
list(chain(*([(i,v)]*2 for i, v in enumerate(ls))))[1:]

Try matching the items on each list depending on their index.尝试根据索引匹配每个列表中的项目。 Eg with list.index(item) :例如使用list.index(item)

y=[5, -2, 4, 10.4, 6]
x=[i for i in range(len(y)+1) if i!=0]

for item in x:
    for i in y:
        if x.index(item) == y.index(i):
            print('%s;%s' %(item, i))
        if x.index(item)==y.index(i) -1:
            print('%s;%s' %(item, i))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM