Posts: 5
Threads: 2
Joined: May 2020
Hi i have to create a 2d game of sorts and to start it i need to create a function which reads in a text file and creates a list from each line.
and then from that line those characters are separated.
for example if the text file has
******F****
*****W*****
it needs to become
[****F****, *****W*****] and then
[[*,*,*,F,*,*,*,*],[*,*,*,*,W,*,*,*,*]]
from what ive heard you need to use a while loop with a nested while loop to achieve this but i just have no idea where to start.
Posts: 94
Threads: 0
Joined: Apr 2020
Posts: 5
Threads: 2
Joined: May 2020
(May-04-2020, 10:59 AM)anbu23 Wrote: Check https://docs.python.org/3.3/tutorial/inp...ting-files
Use list(line_read_file) to convert into list
sorry i have to create a another list within that list if that makes sense
Posts: 2,021
Threads: 9
Joined: May 2017
May-04-2020, 11:20 AM
(This post was last modified: May-04-2020, 11:20 AM by DeaD_EyE.)
In Python sequences are iterable. Also a str is iterable.
A list , tuple , set , dict , ..., consumes iterables.
greeting = "Hello World"
print(list(greeting)) Output: ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
So you can use this, to split the chars of a line, which represents the row (y coordinates).
text = """
******F****
*****W*****
""".strip()
# the text has a leading '\n'
# and a tailing '\n'
# strip removes whitespaces from left and right side of a str
# also a newline is interpreted as whitespace.
matrix2d = [list(line.strip()) for line in text.splitlines()]
# iterate over lines, which are the rows
# make a list from each row, which represents the columns
# if you want to transpose the 2d matrix:
matrix2d_transposed = list(zip(*matrix2d))
Posts: 5
Threads: 2
Joined: May 2020
May-04-2020, 12:10 PM
(This post was last modified: May-04-2020, 12:33 PM by garry1415.)
(May-04-2020, 11:20 AM)DeaD_EyE Wrote: In Python sequences are iterable. Also a str is iterable.
A list , tuple , set , dict , ..., consumes iterables.
greeting = "Hello World"
print(list(greeting)) Output: ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
So you can use this, to split the chars of a line, which represents the row (y coordinates).
text = """
******F****
*****W*****
""".strip()
# the text has a leading '\n'
# and a tailing '\n'
# strip removes whitespaces from left and right side of a str
# also a newline is interpreted as whitespace.
matrix2d = [list(line.strip()) for line in text.splitlines()]
# iterate over lines, which are the rows
# make a list from each row, which represents the columns
# if you want to transpose the 2d matrix:
matrix2d_transposed = list(zip(*matrix2d))
another way to sort of say what i need is
ls = ['string one' , 'string two']
ls2 = [[s,t,r,i,g, ,o,n,e], [,s,t,r,i,n,g, ,t,w,o]]
(May-04-2020, 12:10 PM)garry1415 Wrote: (May-04-2020, 11:20 AM)DeaD_EyE Wrote: In Python sequences are iterable. Also a str is iterable.
A list , tuple , set , dict , ..., consumes iterables.
greeting = "Hello World"
print(list(greeting)) Output: ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
So you can use this, to split the chars of a line, which represents the row (y coordinates).
text = """
******F****
*****W*****
""".strip()
# the text has a leading '\n'
# and a tailing '\n'
# strip removes whitespaces from left and right side of a str
# also a newline is interpreted as whitespace.
matrix2d = [list(line.strip()) for line in text.splitlines()]
# iterate over lines, which are the rows
# make a list from each row, which represents the columns
# if you want to transpose the 2d matrix:
matrix2d_transposed = list(zip(*matrix2d))
another way to sort of say what i need is
ls = ['string one' , 'string two']
ls2 = [[s,t,r,i,g, ,o,n,e], [,s,t,r,i,n,g, ,t,w,o]] **X**
* *
**Y**
thats what the board looks like i need to put it in cells if that makes sense
Posts: 34
Threads: 0
Joined: Apr 2020
May-04-2020, 12:46 PM
(This post was last modified: May-04-2020, 12:46 PM by TomToad.)
text = '''******F****
*****W*****'''
print ("text =",text)
ls = text.splitlines()
print ("ls =",ls)
ls2 = [[letter for letter in line] for line in ls]
print ("ls2 =",ls2)
Posts: 5
Threads: 2
Joined: May 2020
(May-04-2020, 12:46 PM)TomToad Wrote: text = '''******F****
*****W*****'''
print ("text =",text)
ls = text.splitlines()
print ("ls =",ls)
ls2 = [[letter for letter in line] for line in ls]
print ("ls2 =",ls2)
could i apply this to a text file ill send you the code i have for that
def read_lines(filename):
try:
file = open(filename, 'r')
readfile = file.readlines()
return list(readfile)
except FileNotFoundError:
return "{} does not exist!".format(filename) and the file the code opens contains this
**X**
* *
**Y**
Posts: 2,021
Threads: 9
Joined: May 2017
May-04-2020, 01:29 PM
(This post was last modified: May-04-2020, 01:30 PM by DeaD_EyE.)
Strings are sorted by lexicographical order (also known as alphabetical order).
Here an example with some German words:
Output: In [70]: sorted(random.sample(words, 10))
Out[70]:
['Bakterienzüchtung',
'Chrzowitz',
'Generalfragen',
'Latzrock',
'Prognosemodell',
'Reliefklischee',
'chaotisieren',
'dreireihig',
'erwünschtestes',
'kampfunfähigem']
In [71]: sorted(random.sample(words, 10))
Out[71]:
['Ablehnen',
'Attenschweiler',
'Bewahrheitung',
'Filzgarn',
'Rollin',
'Schneckengrün',
'Textilgürtelreifen',
'anpflanzender',
'herausgestrichenen',
'kostenschonendes']
words = ["Ca", "ca", "cA"]
print(sotred(words)) Output: ['Ca', 'cA', 'ca']
Python is "seeing" the chars as values and they are compared as values
words = ['Ca', 'cA', 'ca']
chars_as_int = [tuple(ord(c) for c in word) for word in sorted(words)]
print(chars_as_int) Output: [(67, 97), (99, 65), (99, 97)]
Python sorts by first letter, then second letter, ...
Do you want to sort via alphabet?
Uppercase letters are smaller in their value as Lowercase letters.
|