473,465 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Extract tuples from space separated string of tuples.

2 New Member
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 1863
bvdet
2,851 Recognized Expert Moderator Specialist
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
sudeepto
2 New Member
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 Recognized Expert Moderator Specialist
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.