Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中管理列表_Python_List - Fatal编程技术网

如何在python中管理列表

如何在python中管理列表,python,list,Python,List,以下是我的两份清单: l1 = ['5', '10', '15', '20', '25', '30', '35', '40', '45'] l2 = ['0.011530', '0.039914', '0.085069', '0.145798', '0.213572', '0.287898', '0.355587', '0.413209', '0.472346'] 由于这两个列表,我想创建两个具有相同值但有一些更改的列表: new_list1 = [5,10,10,15,15,20,20,2

以下是我的两份清单:

l1 = ['5', '10', '15', '20', '25', '30', '35', '40', '45']
l2 = ['0.011530', '0.039914', '0.085069', '0.145798', '0.213572', '0.287898', '0.355587', '0.413209', '0.472346']

由于这两个列表,我想创建两个具有相同值但有一些更改的列表:

new_list1 = [5,10,10,15,15,20,20,25,25,30,30,35,35,40,40,45,45]
new_list2 = ['0.011530', '0.011530', '0.039914', '0.039914', '0.085069', '0.085069', '0.145798', '0.145798', '0.213572', '0.213572', '0.287898', '0.287898', '0.355587', '0.355587', '0.413209', '0.413209', '0.472346']
这两个新列表的目的是创建一个图形,而不是一条直线,我将获得步骤。
[我不想要的]

[我想要的]

因此,作为一名初学者,我写道:

def test():
 
  new_list1.insert(0,l1[0])
  new_list2.insert(0,l2[0])

  new_list1.insert(1,l1[1])
  new_list2.insert(1,l2[0])

  new_list1.insert(2,l1[1])
  new_list2.insert(2,l2[1])

  new_list1.insert(3,l1[2])
  new_list2.insert(3,l2[1])

  new_list1.insert(4,l1[2])
  new_list2.insert(4,l2[2])
  
  new_list1.insert(5,l1[3])
  new_list2.insert(5,l2[2])

  new_list1.insert(6,l1[3])
  new_list2.insert(6,l2[3])

  new_list1.insert(7,l1[4])
  new_list2.insert(7,l2[3])
  
  new_list1.insert(8,l1[4])
  new_list2.insert(8,l2[4])

  new_list1.insert(9,l1[5])
  new_list2.insert(9,l2[4])

  new_list1.insert(10,l1[5])
  new_list2.insert(10,l2[5])
  
  new_list1.insert(11,l1[6])
  new_list2.insert(11,l2[5])

  new_list1.insert(12,l1[6])
  new_list2.insert(12,l2[6])

  new_list1.insert(13,l1[7])
  new_list2.insert(13,l2[6])
  
  new_list1.insert(14,l1[7])
  new_list2.insert(14,l2[7])

  new_list1.insert(15,l1[8])
  new_list2.insert(15,l2[7])

  new_list1.insert(16,l1[8])
  new_list2.insert(16,l2[8])

  print(new_list1)
  print(new_list2)

  x = np.array(new_list1) 
  y = np.array(new_list2)
  
  # plotting
  plt.title("Line graph") 
  plt.xlabel("X axis") 
  plt.ylabel("Y axis") 
  plt.plot(x, y, color ="green") 
  plt.show()

这使得技巧,但我希望它更“漂亮”和易于阅读,与一些“for”循环。 我该怎么做呢?

您可以使用
plt。步骤(x,t)
如下:

导入matplotlib.pyplot作为plt
l1=['5','10','15','20','25','30','35','40','45']
l2=['0.011530','0.039914','0.085069','0.145798','0.213572','0.287898','0.355587','0.413209','0.472346']
x=[l1中v的整数(v)]
y=[l2中k的浮点(k)]
plt.阶跃(x,y)

您根本不需要弄乱原始阵列!
plt.plot()
接受一个参数
drawstyle
,您可以将该参数设置为
“步骤”
,以在不更改原始数组的情况下获得所需的输出

import matplotlib.pyplot as plt

l1 = ['5', '10', '15', '20', '25', '30', '35', '40', '45']
l2 = ['0.011530', '0.039914', '0.085069', '0.145798', '0.213572', '0.287898', '0.355587', '0.413209', '0.472346']

x = [int(v) for v in l1]
y = [float(k) for k in l2]

plt.plot(x, y, drawstyle='steps')

但是,请注意,这不是您想要的绘图:在绘图中,第一条水平线位于
y=0.011530
,但在这条曲线中,它位于
y=0.039914

要解决此问题,只需复制最后一个
x
值和第一个
y

x.insert(-1, x[-1])
y.insert(0, y[0])
plt.plot(x, y, drawstyle='steps')


如果你仍然想弄乱你的阵列,使用一个循环

new_list_x = []
new_list_y = []
# zip three lists: 
#    0th through penultimate elements of x -> currentX
#    0th through penultimate elements of y -> currentY
#    1st through last elements of x -> nextX
for currentX, currentY, nextX in zip(x[:-1], y[:-1], x[1:]):
    # Add the point at the current (x, y)
    new_list_x.append(currentX)
    new_list_y.append(currentY)

    # Add the point at the next x but the current y
    new_list_x.append(nextX)
    new_list_y.append(currentY)

plt.plot(new_list_x, new_list_y)

这是
for
循环的
,我相信它取代了
插入
函数

def test():
  for i in range(16+1):
    new_list1.append(l1[i//2+i%2])
    new_list2.append(l2[i//2])

这是我的方法,我认为很容易理解:

new_lst1=[]
新的_lst2=[]
对于计数,枚举中的i(l1):
如果计数!=0:
新增1.append(int(i))
新增1.append(int(i))
其他:
新增1.append(int(i))
对于计数,枚举中的i(l2):
如果计数!=len(l2)-1:
新增2.追加(一)
新增2.追加(一)
其他:
新增2.追加(一)

基本上,
enumerate
允许我们循环遍历列表,同时获取列表的计数。这允许我们只添加一个
5

l1
意味着有2个
5
,还是只有一个
5
。为什么要使用
insert
?你就不能用
append
来代替吗?@ThePilotDude是的,只有1->
5
plot()
需要一个参数
drawstyle='steps'
@这是因为我不知道如何使用append,而且我认为这个函数更直观。但是如果使用append更好,我会尝试更改谢谢,我真的做得太过分了,出现这个错误正常吗
x=[int(v)表示l1中的v]ValueError:以10为基数的int()的文本无效:“”
@Kiri您使用的列表与此示例相同吗?所有的值都是整数吗?哎呀,我没看到,我弄错了谢谢你,我会改变
范围(16+1)
@的目的,我可以写17,但是因为在他的问题中他需要16,所以我只举了16作为例子。我没有使用
len(list)
,因为我不确定他到底需要什么,他要求为
循环使用
。@thetiny-Ahhh-right-nice。谢谢你,我现在就更改它!你忘了数数%2了吗?没有,我只是觉得这样会让你更难阅读。对于这样的简单情况,我更喜欢使用简单的方法。