5 Replies - 432 Views - Last Post: 03 May 2015 - 04:51 PM Rate Topic: -----

#1 toothpicky   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-May 15

Get elements of list in 1 line

Posted 03 May 2015 - 04:25 PM

I want to get all my elements in one line. Right now it looks like this:

[u'45.3',
 u'45.3',
 u'45.7',
 u'45.7',
 u'45.7',
 u'43.8',
 u'43.8',
 u'43.8',
 u'43.8']



But I want it to be like this.

[u'45.3', u'45.3', u'45.7', u'45.7', u'45.7', u'43.8', u'43.8', u'43.8', u'43.8']



Is This A Good Question/Topic? 0
  • +

Replies To: Get elements of list in 1 line

#2 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 829
  • View blog
  • Posts: 2,250
  • Joined: 23-December 08

Re: Get elements of list in 1 line

Posted 03 May 2015 - 04:36 PM

your question doesn't make sense. Are you saying you want it printed out? ok

myList = [u'45.3', u'45.3', u'45.7', u'45.7', u'45.7', u'43.8', u'43.8', u'43.8', u'43.8']
print(myList)

which outputs: ['45.3', '45.3', '45.7', '45.7', '45.7', '43.8', '43.8', '43.8', '43.8']

or if you want it to look good
myList = [u'45.3', u'45.3', u'45.7', u'45.7', u'45.7', u'43.8', u'43.8', u'43.8', u'43.8']
print(", ".join(myList))

which outputs: 45.3, 45.3, 45.7, 45.7, 45.7, 43.8, 43.8, 43.8, 43.8

This post has been edited by atraub: 03 May 2015 - 04:39 PM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • awks lol ffs
  • member icon

Reputation: 6693
  • View blog
  • Posts: 27,471
  • Joined: 12-December 12

Re: Get elements of list in 1 line

Posted 03 May 2015 - 04:37 PM

Erm, what's to stop you from typing this code on a single line?

Or perhaps you mean that when you run code to print out this text it appears on multiple lines? In which case, post your code (between code tags). Perhaps also mention how you are running this, from which editor.
Was This Post Helpful? 0
  • +
  • -

#4 toothpicky   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-May 15

Re: Get elements of list in 1 line

Posted 03 May 2015 - 04:41 PM

Here a I have list within a list. The first list has all the elements in 1 line but the second lists has all elements in a separate line. When I try to make a data frame, I get this error:

df = pd.DataFrame(zz, columns=zz.pop(0))
>>> Assertionerror: 3 columns passed, passed data had 91 columns

I really just want a df with 3 columns. Please see my output picture attached.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#5 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 829
  • View blog
  • Posts: 2,250
  • Joined: 23-December 08

Re: Get elements of list in 1 line

Posted 03 May 2015 - 04:44 PM

Show us all the code, not just a screen shot of a portion of it.
Was This Post Helpful? 0
  • +
  • -

#6 toothpicky   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-May 15

Re: Get elements of list in 1 line

Posted 03 May 2015 - 04:51 PM

Here is all the code. This is practice scraping website.

def rcp_poll_data2(xml):
    x=[];y=[];z=[];yy=[];zz=[]
    dom=web.Element(xml)
    for a in dom.by_tag('series')[0]:
        datex=a.content
        x.append(datex)
    time=pd.Series([pd.to_datetime(date) for date in x])
    for i in range(0,len(dom.by_tag('graph'))):
        for b in dom.by_tag('graph')[i]:
            approve=b.content
            y.append(approve)
        for cc in dom.by_tag('graphs'):
            t = cc.by_tag('graph')[i].title
            z.append(t)
    zz.append(z)
    yy.extend(y)
    y1=chunks(yy,len(dom.by_tag('graph')))
    zz.extend(y1)
    df = pd.DataFrame(zz, columns=zz.pop(0))



But I think the problem is this: I have a list [[a,b],[1,4,6,3,8],[3,6,8,3,9]]. I want to make it dataframe with header a,b and 2 columns.

View Posttoothpicky, on 03 May 2015 - 04:49 PM, said:

Here is all the code. This is practice scraping website.

But I think the problem is this: I have a list [[a,b],[1,4,6,3,8],[3,6,8,3,9]]. I want to make it dataframe with header a,b and 2 columns.


Also, it references the chunks() breaks it into 3 even chunks:
def chunks(seq, num):
  avg = len(seq) / float(num)
  out = []
  last = 0.0
  while last < len(seq):
    out.append(seq[int(last):int(last + avg)])
    last += avg
  return out


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1