简体   繁体   中英

Converting a list of strings of numbers to tuples with integers Python3

Example:

I have a file named data.txt saved on my system. This file contains information like:

'Noah,14,233,66,21,88,42'
'Robert,34,11,667,2,785,23'
'Jackson,85,22,73,12,662,5'

and so on,

My question is how can I make this a tuple with integers?, so this would be desired:

('Noah', [14,233,66,21,88,42] ),
('Robert', [34,11,667,2,785,23] )

I assume you must use a for loop for each line but I can't figure anything out. Any help would be appreciated!

If you want exactly the same output you mentioned, you can split each line on , and store it in a tuple,

x = 'Noah,14,233,66,21,88,42'
# split string on , 
x = x.strip().split(',')
# x[0] is name, while x[1]....x[n] are numbers
y = (x[0], x[1:])
print(y)

Outputs,

('Noah', ['14', '233', '66', '21', '88', '42'])

Obviously you would need to read each line from file separately first, so,

with open("file_name", "r") as file:
    for line in file:
      line = line.strip().split(',')
      output = (line[0], line[1:])

will output,

('Noah', ['14', '233', '66', '21', '88', '42'])
('Robert', ['34', '11', '667', '2', '785', '23'])
('Jackson', ['85', '22', '73', '12', '662', '5'])

Agree with others that a dictionary might be best for this circumstance, but this should achieve your goal:

data= """Noah,14,233,66,21,88,42
Robert,34,11,667,2,785,23
Jackson,85,22,73,12,662,5"""

[(row.split(',')[0], row.split(',')[1:]) for row in data.split('\n')]

Output:

[('Noah', ['14', '233', '66', '21', '88', '42']),
('Robert', ['34', '11', '667', '2', '785', '23']),
('Jackson', ['85', '22', '73', '12', '662', '5'])]

Assuming the text is exactly as you have it in the question:

import re

input = """
'Noah,14,233,66,21,88,42'

'Robert,34,11,667,2,785,23'

'Jackson,85,22,73,12,662,5'
"""

lines = re.findall("'.*'", input) # Find lines which contain text between single quotes

tuples = []

for line in lines:
  line = line.replace("'", "").split(",") # Remove single quotes, split by comma
  tuples.append((line[0], line[1:])) # Add to tuple

print(tuples)

Which would print:

[
  ('Noah', ['14', '233', '66', '21', '88', '42']), 
  ('Robert', ['34', '11', '667', '2', '785', '23']), 
  ('Jackson', ['85', '22', '73', '12', '662', '5'])
]

First we grab all the lines which has something between two single quotes. Then take out the single quotes and create an array by splitting it by the commas. Finally create a tuple with the first element and the sub-array of the second to the last element.

You can try this:

   with open(data.txt, 'r') as f:
        lines = [(line.strip().split(',')[0], line.strip().split(',')[1:])for line in f]
   print(lines)

It will return a list of tuple in the format that you described.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM