在python中合并列表,但保持顺序

时间:2020-04-24 22:15:03

标签: python python-3.x list matplotlib numbers

尝试使用matplotlib绘制蒸汽价格历史数据图表 我正在尝试在特定时间范围内获得平均图表 一切正常,直到在绘制日期时matplotlib给我一个值错误

我的代码:

import requests
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import datetime
from tkinter import *
from tkinter import ttk
import time
from tkinter import messagebox
import configparser
import os

def parseDate(dateString):
  month, day, year, hour, tz = dateString.split()
  month = ["", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"].index(month)
  day = int(day)
  year = int(year)
  hour = int(hour[:-1])
  return datetime.datetime(year, month, day, hour)

def get_data(id,item):
    page = requests.get(f"https://steamcommunity.com/market/listings/{id}/{item}")
    #To do: make sure that the response code is 200 etc...
    source = page.text
    line1Index = source.find("line1=[[")
    dataString = source[line1Index + 6: source.find("]]", line1Index) + 2]
    data = eval(dataString)
    #Convert quantity solds to integers
    data = list(map(lambda x : x[:2] + [int(x[-1])], data))
    data = list(map(lambda x: [parseDate(x[0])] + x[1:], data))
    return (data)

def smooth(data):
  newData = []
  currDate = data[0][0].date()
  totalPrice, numEntries, totalVolume = 0, 0, 0
  for quote in data:
    if quote[0].date() == currDate:
      totalPrice += quote[1]
      totalVolume += quote[2]
      numEntries += 1
    else:
      newData += [[currDate, totalPrice / numEntries, totalVolume]]
      totalPrice, numEntries, totalVolume = quote[1], 1, quote[2]
      currDate = quote[0].date()
  newData += [[currDate, totalPrice / numEntries, totalVolume]]
  return newData

def getLastXDays(data, days):
    today = datetime.datetime.now()
    today = datetime.datetime(today.year, today.month, today.day)
    startDate = today - datetime.timedelta(days = days)
    return list(filter(lambda quote: quote[0] >= startDate, data))

gameIDs = {"CS:GO" : 730, "RUST" : 252490}
def GetGameID(game):
  return gameIDs.get(game.upper(), None)

def plot_lastxdays(game,item,days):
  data = get_data(game,item)
  data=getLastXDays(data,days)
  data=smooth(data)
  xs = [quote[0] for quote in data]
  ys = [quote[1] for quote in data]
  plt.plot(xs,ys,)
def plot_lastxdays_quant(game,item,days,divisor) :
  data = get_data(game,item)
  data=getLastXDays(data,days)
  data=smooth(data)
  xx = [quote[2] for quote in data]
  xs = [x / divisor for x in xx]
  ys = [quote[0] for quote in data]
  plt.plot(ys,xs,)

def get_many_items():
  config = configparser.ConfigParser()
  config.read('config.ini')

  itemlist = config['Settings']['Items'].split(',')
  days = config['Settings']['Days']
  days = int(days)
  game = config['Settings']['Game']
  game = GetGameID(game)
  count=0

  for item in itemlist:
    plot_lastxdays(game, item, days)
    count +=1
    print(f'Completed : {count} / {len(itemlist)}')
    if count == len(itemlist):
      print('completed')
  plt.title(f'Price in USD over the past {days} days for {itemlist}')
  plt.show()

def myFancyAverage(*args):
  average = lambda xs: sum(xs) / len(xs)
  return list(map(average, zip(*args)))

def plottrend() :
  config = configparser.ConfigParser()
  config.read('config.ini')

  itemlist = config['Settings']['Items'].split(',')
  days = config['Settings']['Days']
  days = int(days)
  game = config['Settings']['Game']
  game = GetGameID(game)
  count=0

  for item in itemlist :
    print(f'Complete {count} of {len(itemlist)}')
    if count == 0 :
      olddata = get_data(game,item)
      olddata=getLastXDays(olddata,days)
      olddata=smooth(olddata)
      oldpricedata =  [quote[1] for quote in olddata]
      xaxis = [quote[0] for quote in olddata]
      plt.plot(xaxis)
      count += 1
    else:
      newdata = get_data(game,item)
      newdata=getLastXDays(newdata,days)
      newdata=smooth(newdata)
      newpricedata =  [quote[1] for quote in newdata]
      oldpricedata = myFancyAverage(oldpricedata,newpricedata)
      xaxis = [quote[0] for quote in newdata]
      plt.plot(xaxis)
      count += 1
      if count == len(itemlist):
        print('complete')
  yaxis = (oldpricedata)
  plt.plot((),yaxis)
  plt.title(f'General Trend for {itemlist} over  the last {days} days')
  plt.show()

plottrend()

我的.ini文件:

[Settings]
Items =Danger Zone Case,Falchion Case,Prisma Case,Gamma Case,Prisma 2 Case,Gamma 2 Case,Horizon Case,Clutch Case,Glove Case,Spectrum Case
Days =365
Game =CS:GO
Divisor =10000

我的错误:

C:\Users\Donov\OneDrive\Desktop\grapher_clean>python Grapher_clean.py
Complete 0 of 10
Complete 1 of 10
Complete 2 of 10
Complete 3 of 10
Complete 4 of 10
Complete 5 of 10
Complete 6 of 10
Complete 7 of 10
Complete 8 of 10
Complete 9 of 10
complete
Traceback (most recent call last):
  File "Grapher_clean.py", line 137, in <module>
    plottrend()
  File "Grapher_clean.py", line 133, in plottrend
    plt.plot(xaxis,yaxis)
  File "C:\Users\Donov\AppData\Local\Programs\Python\Python38-32\lib\site-packages\matplotlib\pyplot.py", line 2761, in plot
    return gca().plot(
  File "C:\Users\Donov\AppData\Local\Programs\Python\Python38-32\lib\site-packages\matplotlib\axes\_axes.py", line 1646, in plot
    lines = [*self._get_lines(*args, data=data, **kwargs)]
  File "C:\Users\Donov\AppData\Local\Programs\Python\Python38-32\lib\site-packages\matplotlib\axes\_base.py", line 216, in __call__
    yield from self._plot_args(this, kwargs)
  File "C:\Users\Donov\AppData\Local\Programs\Python\Python38-32\lib\site-packages\matplotlib\axes\_base.py", line 342, in _plot_args
    raise ValueError(f"x and y must have same first dimension, but "
ValueError: x and y must have same first dimension, but have shapes (366,) and (24,)

我怀疑这与某些项目比其他项目晚出现有关,这意味着它们的数据在时间上较晚开始,因此我无法从我查看的最新项目中提取日期,因为其他项目可能具有早于时间,但是我不知道如何找到包含最早数据的项目,只能从中提取数据,或者如何像下面这样连接字符串: 请帮助并谢谢你

List1 = [7,8,9]
List2 = [6,7,8,9]
list3 = [2,3,4,5,6,7,8,9]

预期输出:

newlist = [2,3,4,5,6,7,8,9]

1 个答案:

答案 0 :(得分:1)

您可以使用:

list1 = [7,8,9]
list2 = [6,7,8,9]
list3 = [2,3,4,5,6,7,8,9]
print(sorted(set(list1+list2+list3)))
# [2, 3, 4, 5, 6, 7, 8, 9]

Demo