Split a Python List into two lists

Question:

Code that im working with :

  def METRICS():
  if os.path.exists(""):
    os.remove("")
  link = ""
  options = webdriver.ChromeOptions()
  options.add_argument("--headless")
  options.add_argument("--window-size=1920,1080")
  prefs = {'download.default_directory' : ''}
  options.add_experimental_option('prefs', prefs)
  driver = webdriver.Chrome(options=options)
  driver.get(link)
  sleep(10)
  all_avg = driver.find_elements_by_xpath('//*[@id="panel-4"]/div/div[1]/div/div[2]/div/plugin-component/panel-plugin-graph/grafana-panel/ng-transclude/div/div[2]/div/div[1]/div')
  lst = []
  for avg in all_avg:
      #print(avg.text)
      lst.append(avg.text)
  #print(lst)
  list = [x.replace('n',',')for x in lst]
  
  print (list)

OUTPUT :

['801,100.0%,802,78.3%,803,99.8%,804,18.5%,805,99.9%,811,100.0%,812,99.9%,813,97.9%,814,99.8%,815,99.9%,816,98.5%,817,100.0%,818,69.9%,819,100.0%,820,100.0%,821,95.9%,822,100.0%,823,100.0%']

I need the first set of numbers ie 801 802 803 etc to be in one list and the percentages% to be in another
. this is so i can format in to a DF correctly.

Desired output :

list_1 = ['801',802',803',804', etc ]
list_2 = ['100%',78.3%,'99.8%','18.5%', etc ]

Final output after i use pandasDF and tabulate :

| Placehol     | 701   | 702   | 703   | 704   | 705   | 706   | 707   | 708   | 709    | 710    | 711   | 712   | 713   |
|:-------------|:------|:------|:------|:------|:------|:------|:------|:------|:-------|:-------|:------|:------|:------|
| Availability | 99.1% | 98.5% | 67.0% | 87.0% | 98.3% | 98.0% | 96.9% | 99.1% | 100.0% | 100.0% | 95.9% | 98.1% | 98.1% |
Asked By: DATTO MOTORSPORTS

||

Answers:

You can use Python List Slicing: https://www.geeksforgeeks.org/python-list-slicing/

lst = [x.replace('n',',') for x in lst] # ['801,100.0%,802,78.3%,803...']

# convert to a list of strings
result = lst.join(",").split(",") # ['801','100.0%','802','78.3%','803',...]

# start from the first element, add every 2n + 1 element to result_1
result_1 = result[::2]

# start from the second element, add every 2n element to result_2
result_2 = result[1::2]

Hmm, I have not noticed that your list has just one element. Edited my code sample.

Answered By: Vlad Emelianov

In addition to the solution provided by Vlad, you can solve your problem using the regex module.

The regex patterns I would like to use are (,|^)((d+)(.d+)?)(,|$) which selects the number without percentage sign and the other one is (,|^)((d+)(.d+)?%)(,|$) which selects the number with percentage sign.

I am not sure what there is a list with one string, but since you have used a list I am going to give you an answer based on your output:

import re
myList = ['801,100.0%,802,78.3%,803,99.8%,804,18.5%,805,99.9%,811,100.0%,812,99.9%,813,97.9%,814,99.8%,815,99.9%,816,98.5%,817,100.0%,818,69.9%,819,100.0%,820,100.0%,821,95.9%,822,100.0%,823,100.0%']

list_1 = []
list_2 = []
for string in myList:
  temp = re.compile("(,|^)((d+)(.d+)?)(,|$)").findall(string)
  list_1 += [x[1] for x in temp]
  temp = re.compile("(,|^)((d+)(.d+)?%)(,|$)").findall(string)
  list_2 += [x[1] for x in temp]
print(list_1)
print(list_2)

Output

['801', '802', '803', '804', '805', '811', '812', '813', '814', '815', '816', '817', '818', '819', '820', '821', '822', '823']
['100.0%', '78.3%', '99.8%', '18.5%', '99.9%', '100.0%', '99.9%', '97.9%', '99.8%', '99.9%', '98.5%', '100.0%', '69.9%', '100.0%', '100.0%', '95.9%', '100.0%', '100.0%']
Answered By: TheFaultInOurStars