Simple Problem: Getting rid of {}

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dont bother

    Simple Problem: Getting rid of {}

    Hi,
    I have done some string concatenation:

    feature_vector= {}
    feature_element ={}

    #inside a loop

    if (v==0):
    feature_element =str(1)+"
    "+str(index[i])+":"+str(vecto r[i])+" "
    print feature_element
    else:

    feature_element =str(index[i])+":"+str(vecto r[i])+" "


    feature_vector= str(feature_ele ment)+str(featu re_vector)
    if (v==0):
    print feature_vector


    when I print feature_vector I get my desired output
    but with an extra {} in the end.

    Output:

    1 22389:0.0291806 958474
    1 22389:0.0291806 958474 {}



    I fail to understand why this is happening since
    feature_element should also have {} in the end, if it
    was the case with the variables I declared with like:
    syntax{}

    How to get rid of this extra {} from my string.

    Thanks
    Dont




    _______________ _______________ ____
    Do you Yahoo!?
    Yahoo! Search - Find what you’re looking for faster


  • Jeroen Wenting

    #2
    Re: Simple Problem: Getting rid of {}

    > #inside a loop[color=blue]
    >
    > if (v==0):
    > feature_element =str(1)+"
    > "+str(index[i])+":"+str(vecto r[i])+" "
    > print feature_element
    > else:
    >[/color]
    feature_element is a string here.
    [color=blue]
    > feature_element =str(index[i])+":"+str(vecto r[i])+" "
    >[/color]
    same here
    [color=blue]
    >
    > feature_vector= str(feature_ele ment)+str(featu re_vector)
    > if (v==0):
    > print feature_vector
    >[/color]
    here feature_vector is made a string you're doing feature_vector = "1
    22389:0.0291806 958474" + "{}" which yields the result you get.[color=blue]
    >
    > when I print feature_vector I get my desired output
    > but with an extra {} in the end.
    >
    > Output:
    >
    > 1 22389:0.0291806 958474
    > 1 22389:0.0291806 958474 {}
    >
    >
    >
    > I fail to understand why this is happening since
    > feature_element should also have {} in the end, if it
    > was the case with the variables I declared with like:
    > syntax{}
    >
    > How to get rid of this extra {} from my string.
    >[/color]
    feature_vector. add(feature_ele ment)
    print " ".join("%s" %x for x in feature_vector)

    or something along those lines :)


    Comment

    • Neal Holtz

      #3
      Re: Simple Problem: Getting rid of {}

      By starting it

      feature_vector= ''
      feature_element =''

      To see why, try this:

      fv = {} # this creates an empty dictionary, not a string
      print str(fv)
      fv =''
      print str(fv)

      dont bother <dontbotherworl d@yahoo.com> wrote in message news:<mailman.3 30.1079123993.1 9534.python-list@python.org >...[color=blue]
      > Hi,
      > I have done some string concatenation:
      >
      > feature_vector= {}
      > feature_element ={}
      >
      > #inside a loop
      >
      > if (v==0):
      > feature_element =str(1)+"
      > "+str(index[i])+":"+str(vecto r[i])+" "
      > print feature_element
      > else:
      >
      > feature_element =str(index[i])+":"+str(vecto r[i])+" "
      >
      >
      > feature_vector= str(feature_ele ment)+str(featu re_vector)
      > if (v==0):
      > print feature_vector
      >
      >
      > when I print feature_vector I get my desired output
      > but with an extra {} in the end.
      >
      > Output:
      >
      > 1 22389:0.0291806 958474
      > 1 22389:0.0291806 958474 {}
      >
      >
      >
      > I fail to understand why this is happening since
      > feature_element should also have {} in the end, if it
      > was the case with the variables I declared with like:
      > syntax{}
      >
      > How to get rid of this extra {} from my string.
      >
      > Thanks
      > Dont
      >
      >
      >
      >
      > _______________ _______________ ____
      > Do you Yahoo!?
      > Yahoo! Search - Find what you?re looking for faster
      > http://search.yahoo.com[/color]

      Comment

      Working...