Home > Net >  How do I split a list of a single string containing multiple numbers into a list of separated intege
How do I split a list of a single string containing multiple numbers into a list of separated intege

Time:02-20

Sorry, I'm a complete beginner and I couldn't find a lot of information on Google.

For my assignment, I need to read the contents I've generated in a file and only output the values that are more than 20 but less than 50. I have the file generated, when I read the file and print the contents all the numbers show up as a single string value in a list:

['11,24,62,59,1,28,61,5,54,38,31']

I've removed the commas so it shows up

['11 24 62 59 1 28 61 5 54 38 31']

I've been searching around but I can't find something for my exact issue.

This is my code:

import os

file_path = ("/Users/elian/Desktop/School/Scripting/Week 5/")
os.chdir(file_path)
file_name = ("Lab5.txt")

with open(file_name, "r ") as file:
    contents = file.readlines() # Reads the contents of the file, outputs as a list.
    print(type(contents)) # class 'list'
    contents = ' '.join(str(contents).split(',')) # Removes commas
    print(type(contents)) # class 'str' 

I've tried using split() but it seems to just make everything worse.

Edit:

This is the source code for the number generator this file is reading from:

import os
import random
# Set the file name
file_name = input("Please input your desired file name: ")
# Change the working directory
file_path = input("Please input the file path where this file is to be saved in: ") 
os.chdir(file_path)
# Asks the user how many numbers they would like to generate. 
num_of_values = int(input("How many random numbers would you like generated: "))
with open(file_name, "w") as file:
    for i in range(1, num_of_values   1):
        random_raw=random.randrange(0,100)
        random_final=int(random_raw)
        file.write
        # convert to a string for the file write
        num_string=str(random_final)
        print(i)
        if i < num_of_values:
            num_string= num_string   ","            
        file.write(num_string)

# Close the file - OS can have issues with files that are not closed
file.close()

CodePudding user response:

As you want to find round tens, you can use a regex:

l = ['11,24,62,59,1,28,61,5,54,38,31']

import re

out = ','.join(re.findall(r'\b[234]\d\b', l[0]))

output: '24,28,38,31'

regex:

\b        # word boundary
[234]\d   # 2 digit number starting with 2 or 3 or 4
`b        # word boundary

excluding the lower bound:

l = ['11,20,24,62,59,1,28,61,5,54,38,31']  ## added 20 to the list

import re

','.join(re.findall(r'\b(2[1-9]|[34]\d)\b', l[0]))

output: '24,28,38,31'

regex:

\b        # word boundary
(         # start group
2[1-9]    # 2 digit number starting with 2 and ending in 1-9
|         # OR
[34]\d    # 2 digit number starting with 3 or 4
)         # end group
`b        # word boundary

CodePudding user response:

Simple pythonic way:

l = '11,24,62,59,1,28,61,5,54,38,31'
x = [i for i in l.split(",") if 20 < int(i) < 50]
print(x)
# ['24', '28', '38', '31']

CodePudding user response:

Assuming the string always has this exact form:

list_string_in_list = ['11,24,62,59,1,28,61,5,54,38,31'] 
valid_int_list = [int(i) for i in list_string_in_list[0].split(",") if 20 < int(i) < 50]

Or if you prefer using loops:

list_string_in_list = ['11,24,62,59,1,28,61,5,54,38,31']
valid_int_list = []
for number in list_string_in_list[0].split(","):
    number_as_int = int(number)
    if 20 < number_as_int < 50:
        valid_int_list.append(number_as_int)

However it is very likely that how you construct this list is wrong in the first place. If you iterate over something and create this list, why do you append it into one long string inside the list? It might make more sense to use .append() to fill that list in the first place.

Edit: It seems like this format is given in the file you are reading. So just parsing it as a string and then splitting is the right way to do it.

  • Related