473,322 Members | 1,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Extract tuples from space separated string of tuples.

I have string in the following form:

Expand|Select|Wrap|Line Numbers
  1.  my_string = '(1, 2) (3, 4)'
As you can see, the tuples inside the string are space separated. I want to know how can I extract those tuples into separate variables.
Jun 16 '15 #1
3 1858
bvdet
2,851 Expert Mod 2GB
There may be a better way.
Expand|Select|Wrap|Line Numbers
  1. >>> my_string = '(1, 2) (3, 4)'
  2. >>> eval(my_string.replace(") (", "),("))
  3. ((1, 2), (3, 4))
  4. >>> a,b = eval(my_string.replace(") (", "),("))
  5. >>> a
  6. (1, 2)
  7. >>> b
  8. (3, 4)
  9. >>> 
Jun 16 '15 #2
Thank you bvdet. But I have a question.

Can you please explain me what is happening in the code ?? I have never used eval function in Python.

Also I have read that using eval is not recommended in Python. Can you tell me why is that ??

EDIT: I used your approach of substituting the
Expand|Select|Wrap|Line Numbers
  1. ) (
part with
Expand|Select|Wrap|Line Numbers
  1. ), (
so that I get the string

Expand|Select|Wrap|Line Numbers
  1. (1, 2), (3, 4)
Then after that, I searched on StackOverflow about ast module and literal_eval function.

converting-string-to-tuple

I used it to separate the tuples into variables.

Thank You for the help .
Jun 16 '15 #3
bvdet
2,851 Expert Mod 2GB
Built-in funcation eval: Python documentation for eval

The function could potentially evaluate malicious code, therefore its use if sometimes frowned upon. A regex solution could potentially be a more robust but complicated solution.

Expand|Select|Wrap|Line Numbers
  1. >>> import re
  2. >>> re.compile("[(](\d+, \d+)[)]")
  3. <_sre.SRE_Pattern object at 0x0000000005C3EA80>
  4. >>> pattern = re.compile("[(](\d+, \d+)[)]")
  5. >>> pattern.findall('(1, 2) (3, 4)')
  6. ['1, 2', '3, 4']
  7. >>> ((int(i) for i in item.split(",")) for item in pattern.findall('(1, 2) (3, 4)'))
  8. <generator object <genexpr> at 0x0000000005E7CD38>
  9. >>> for obj in ((int(i) for i in item.split(",")) for item in pattern.findall('(1, 2) (3, 4)')):
  10. ...     print tuple(obj)
  11. ...     
  12. (1, 2)
  13. (3, 4)
  14. >>> 
Jun 16 '15 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: AMB | last post by:
Hi all, I'm currently working on a large project which uses XML formatted data to communicate between all the various different systems the project ties together, apart from one, which...
4
by: Ryan | last post by:
I am trying to write a c# function to extract all numbers out of a string. What is the easiest way to do this, regular expressions? I must account for numbers formatted with or without commas...
3
by: Maya | last post by:
Hello guys, Is there an easy way to extract individual words that form a string and store them in variables like in this example: String "how are you?" My result would be: var1 = "how"
5
by: deko | last post by:
If I have random and unpredictable user agent strings containing URLs, what is the best way to extract the URL? For example, let's say the string looks like this: registered NYSE 943 <a...
5
by: Helmut Jarausch | last post by:
Hi, I'm looking for an elegant solution to the following (quite common) problem: Given a string of substrings separated by white space, split this into tuple/list of elements. The problem...
4
by: Phantom | last post by:
I'm wondering if anyone knows a trick to iterate a FLWR expression across a literal string that is delimited by spaces. e.g. for $x in ("apple orange banana blueberry") obviously, this...
7
by: ganesh gajre | last post by:
Hi all, I want to read file which is mapping file. Used in to map character from ttf to unicode. eg Map file contain data in the following way: 0 ० 1 १
5
by: Rootz88 | last post by:
ok I have this string and other similar( ull find them in the attachment) "BRUNEI BANDAR SERI BEGAWAN 5770 343653" I need to extract BRUNEI, BANDAR SERI...
3
by: Robert Hodan | last post by:
So, I'm trying to make a method that takes an input stream (from a text file), and churns out each word into it's own string, and sticks it into a list. According to my logic this should work, but...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.