Wednesday 15 May 2013

Difficulty Using Python Split String Method -


I hate to ask such a basic question to me, but I do not seem to be working in the partitioning method Could python

I have a dictionary, when I print it, it looks like:

  data_dict = & lt; Query Date: {u'ids ': [u'1012, 738'], You'ct ': [u'9']} & gt;  wants to create an array of id objects from the list, such as:  
  ids = [1012, 738]   

It appears That list 'id' is actually only a string of different IDs by commas, so I tried:

  id_string = data_dict ['id'] id_string.split for id id ',') In ID: Print (id)   

Nothing is printed. Then I thought, maybe u means that this is not a string and I need to convert it to string first, so I tried:

  id_string = data_dict ['id'] Id_string = str (id_string) id = id_string.split (',') For IDs in ID: Print (id)   

Nothing is printed. Not sure what I'm doing.

[u'10123838] is a list so that you get the first of that list To get the element, use id_string = data_dict ['id'] [0] .

The string in the list of integers either uses the map or comprehension of the list to convert items :

  

Demo:

  & gt; & Gt; & Gt; Strs = '1012,738' & gt; & Gt; & Gt; Map (int, strs.split (',')) [1012, 738]> gt; & Gt; & Gt; [Int (x) for x in Strs.split (',')] [1012, 738]    

No comments:

Post a Comment