I am importing a list of strings into python from a text file. When I print the list to check it has been done properly, I see it is double bracketed.
From what I understand this means it multidimensional but this is not what I want. How do I prevent this?
An example of my code is shown below.
test_list_a_file = "path/to/test_list_a_file"
with open(test_list_a_file, 'r') as openfile:
test_list_a = [line.split() for line in openfile.readlines()]
print(test_list_a[0:4])
This returns...
[['A'], ['B'], ['C'], ['D']]
When I manually create my list in python to see what happens:
test_list_b = ['A', 'B', 'C', 'D', 'E', 'F']
print(test_list_b[0:4])
It works just fine...
['A', 'B', 'C', 'D']
The file test_list_a_file looks like this:
A
B
C
D
E
F
Whats wrong with the way I am importing it?
test_list_a_file = "path/to/test_list_a_file"
with open(test_list_a_file, 'r') as openfile:
test_list_a = [line.strip() for line in openfile.readlines()]
should work
or alternatively
with open(test_list_a_file, 'r') as openfile:
test_list_a = openfile.read().split()
you can use a loop
like this:
`for i in range(0,5):
print(test_list_b[i])`
output:
A
B
C
D
E
F
Try line.split('\n') It will detect the Value after Enter in the text file.
Related
Im trying to join the letters as a string that's inside the list which is also inside the list. So for example, it looks like this [['a', 'b', 'c'], ['d', 'e', 'f']] however I want the result to look like 'ad be cf' which is basically taking the element that lies in the same position in the list. I know how to join the elements into a list that can look like 'abcdef', however, i don't know which I could add in order to return a string that looks like above.
Any advice would be thankful!
string = ''
new_grid = []
for a in grid:
for b in a:
string += b
return string
When you want to transpose lists into columns, you typically reach for zip(). For example:
l = [['a', 'b', 'c'], ['d', 'e', 'f']]
# make a list of columns
substrings = ["".join(sub) for sub in zip(*l)]
#['ad', 'be', 'cf']
print(" ".join(substrings))
# alternatively print(*substrings, sep=" ")
# ad be cf
This works:
my_list = [['a', 'b', 'c'], ['d', 'e', 'f']]
sorted_list = [list(pair) for pair in zip(my_list[0], my_list[1])]
for i in range(3):
string = ''.join(sorted_list[i])
print(string, end=" ")
First, we are pairing each individual list to its corresponding value using [zip][1], then we are joining it into a string, and printing it out.
This solution may not be the most efficient, but it's simple to understand.
Another quick solution without zip could look like this:
my_list = [['a', 'b', 'c'], ['d', 'e', 'f']]
sorted_list = list(map(lambda a, b: a + b, my_list[0], my_list[1]))
print(" ".join(sorted_list))
How do we create a nested list by taking off the terminal output ?
Ex: I am querying the terminal to get some output (In this case, it's related to Yarn)
import subprocess
outputstring=subprocess.check_output("yarn application -list | grep " + user, shell=True)
mylist = (re.split("[\t\n]+",outputstring))=
This produces a output on each line for each Job that's running on Yarn.
Eg:
line1 = a,b,c,d,e
line2 = f,g,h,i,j
line3 = k,l,m,m,o
I am able to create a list off this output, but as a single list with all the words as comma separated values like
mylist = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o] using the regex above.
but need to create a list as below:
mylist = [[a,b,c,d,e], [f,g,h,i,j], [k,l,m,n,o]]
i.e:
mylist = [[line1],[line2],[line3]]
can anyone please suggest how to achieve this ?
Regex I am currently using is:
mylist = (re.split("[\t\n]+",outputstring))
Try this list comprehension:
a="""a,b,c,d,e
f,g,h,i,j
k,l,m,m,o"""
mylist=[e.split(",") for e in a.split("\n")]
Can't you just do this?
my_list = [line1.split(','), line2.split(','), line3.split(',')]
or this
initial_list = []
initial_list.append(line1)
initial_list.append(line2)
initial_list.append(line3)
final_list = [x.split(',') for x in initial_list]
I know you surely have not only 3 lines, but if you can do ["a,b,c,d,e,f,g,h,j,k,l"] with your output, maybe you can do this as well.
You can also do it using the map function:
output = """a,b,c,d,e
f,g,h,i,j
k,l,m,m,o"""
output = list(map(lambda i: i.split(','), output.split('\n')))
print(output)
Output:
[['a', 'b', 'c', 'd', 'e'], ['f', 'g', 'h', 'i', 'j'], ['k', 'l', 'm', 'm', 'o']]
I have a file with a list of letters corresponding to another letter:
A['B', 'D']
B['A', 'E']
C[]
D['A', 'G']
E['B', 'H']
F[]
G['D']
H['E']
I need to import these lists to their corresponding letter, to hopefully have variables that look like this:
vertexA = ['B', 'D']
vertexB = ['A', 'E']
vertexC = []
vertexD = ['A', 'G']
vertexE = ['B', 'H']
vertexF = []
vertexG = ['D']
vertexH = ['E']
What would be the best way to do this? I tried searching for an answer but was unlucky in doing so. Thanks for any help.
You can try using dictionaries rather than variables, and I think it makes it easier as well to populate your data from your textfile.
vertex = {}
vertex['A'] = ['B', 'D']
vertex['A']
>>> ['B', 'D']
When you read your input file, the inputs should look like this:
string='A["B","C"]'
So, we know that the first letter is the name of the list.
import ast
your_list=ast.literal_eval(string[1:])
your_list:
['B', 'C']
You can take care of the looping, reading file, and string manipulation for proper naming...
Building a dictionary would probably be best. Each letter of the alphabet would be a key, and then the value would be a list of associated letters. Here's a proof of concept (not tested):
from string import string.ascii_uppercase
vertices = {}
# instantiate dict with uppercase letters of alphabet
for c in ascii_uppercase:
vertices[c] = []
# iterate over file and populate dict
with open("out.txt", "rb") as f:
for i, line in enumerate(f):
if line[0].upper() not in ascii_uppercase:
# you probably want to do some additional error checking
print("Error on line {}: {}".format(i, line))
else: # valid uppercase letter at beginning of line
list_open = line.index('[')
list_close = line.rindex(']') + 1 # one past end
# probably would want to validate record is in correct format before getting here
# translate hack to remove unwanted chars
row_values = line[list_open:list_close].translate(None, "[] '").split(',')
# do some validation for cases where row_values is empty
vertices[line[0].upper()].extend([e for e in row_values if e.strip() != ''])
Using it would then be easy:
for v in vertices['B']:
# do something with v
File A.txt:
A['B', 'D']
B['A', 'E']
C[]
D['A', 'G']
E['B', 'H']
F[]
G['D']
H['E']
The code:
with open('A.txt','r') as file:
file=file.read().splitlines()
listy=[[elem[0],elem[1:].strip('[').strip(']').replace("'",'').replace(' ','').split(',')] for elem in file]
This makes a nested list, but as Christian Dean said, is a better way to go.
Result:
[['A', ['B', 'D']], ['B', ['A', 'E']], ['C', ['']], ['D', ['A', 'G']], ['E', ['B', 'H']], ['F', ['']], ['G', ['D']], ['H', ['E']]]
So I have a text file that looks like this:
abcd
efghij
klm
and I need to convert it into a two-dimensional list. And it should look like this:
[['a', 'b', 'c', 'd'],
['e', 'f', 'g', 'h', 'i', 'j'],
['k', 'l', 'm']]
so far I have managed to get this result:
[["abcd"], ["efghij"], ["klm"]]
Can anyone help me figure out what the next step should be?
Here is my code so far:
def readMaze(filename):
with open(filename) as textfile:
global mazeList
mazeList = [line.split() for line in textfile]
print mazeList
str.split() splits on whitespace. str.split('') splits each character separately. (apparently I'm misremembering, str.split('') throws a ValueError for "empty separator")
You'll just build a list from it.
text = """abcd
efghij
klm"""
mazelist = [list(line) for line in text.splitlines()]
# the splitlines call just makes it work since it's a string not a file
print(mazelist)
# [['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h', 'i', 'j'], ['k', 'l', 'm']]
Make a list of each line in the file:
with open('tmp.txt') as f:
z = [list(thing.strip()) for thing in f]
As explained above, you just need to build a list from the strings.
Assuming the string is held in some_text;
lines = some_text.split('\n')
my_list = []
for line in lines:
line_split = list(line)
my_list.append(line_split)
as one-liner;
my_list = map(lambda item: list(item), some_text.split('\n'))
should do the trick.
How would I get python to run through a .txt document, find a specific heading and then put information from each line in to a list for printing? And then once finished, look for another heading and do the same with the information there...
If you had a csv file as follows:
h1,h2,h3
a,b,c
d,e,f
g,h,i
Then the following would do as you request (if I understood you correctly)
def getColumn(title,file):
result = []
with open(file) as f:
headers = f.readline().split(',')
index = headers.index(title)
for l in f.readlines():
result.append(l.rstrip().split(',')[index])
return result
For example:
print(getColumn("h1",'cf.csv') )
>>> ['a', 'd', 'g']
File test.txt
a
b
c
heading1
d
e
f
heading2
g
h
heading3
>>> from itertools import takewhile, imap
>>> with open('test.txt') as f:
for heading in ('heading1', 'heading2', 'heading3'):
items = list(takewhile(heading.__ne__, imap(str.rstrip, f)))
print items
['a', 'b', 'c']
['d', 'e', 'f']
['g', 'h']