2 Replies - 3403 Views - Last Post: 26 November 2011 - 03:29 PM Rate Topic: -----

#1 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Tuple with number mysteriously added to list

Posted 25 November 2011 - 09:15 PM

I was using a regex sub replace that got assigned to a list in a for loop.

When the replace got assigned to the list it got converted to a tuple inside the list and a number got added as a second item in each tuple.

For example:

['thing1', 'thing2', 'thing3'] # would be converted to

[('thing1', 1), ('thing2', 1), ('thing3', 1)]



# so basically this
things = ['thing1', 'thing2', 'thing3']
i = 0
while i < len(things)
    thingies.append(re.sub('thing', 'a thing', things[i]))
    i += 1
print "".join(thingies) # shows error because [('thing1', 1), ('thing2', 1), ('thing3', 1)]



The code above is just a quick summary of what's going on.

I just want to know how those tuples with ones are being added, and how to prevent it if there is a way.
Whatcha think?

Unless this is normal. :smile2:

This post has been edited by hiddenghost: 25 November 2011 - 10:04 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Tuple with number mysteriously added to list

#2 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Tuple with number mysteriously added to list

Posted 26 November 2011 - 02:17 PM

I cannot see any reason why should your output come like this, except that you might have used re.subn inadvertently in place of re.sub. The former returns a tuple consisting of the new string and the number of non-overlapping occurrences that have been replaced. In your case, it is 1 for each value in things array, so each element should return something like ('a thing1',1).
Was This Post Helpful? 1
  • +
  • -

#3 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: Tuple with number mysteriously added to list

Posted 26 November 2011 - 03:29 PM

View Postcupidvogel, on 26 November 2011 - 03:17 PM, said:

I cannot see any reason why should your output come like this, except that you might have used re.subn inadvertently in place of re.sub. The former returns a tuple consisting of the new string and the number of non-overlapping occurrences that have been replaced. In your case, it is 1 for each value in things array, so each element should return something like ('a thing1',1).


I think you might be right. I am going to check my code and see if i did use subn.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1