Friday, 15 August 2014

2d array structure python -



2d array structure python -

i taking python class , have no experience in python. instructor teaches documentation , refuses give code examples. understand c++ i'm getting frustrated python because teaching myself.

i want open text file contains 1 string of random characters per line. length 8, 10, 15, or 20 chars, example. has flexible. there can unspecified number of strings (lines) in text file.

the text file might example:

hgamonihra aomokawons nfrolbobdn arfsihcage lnieewonok golfundthc kocataohbi amrercganh slgfamallc alligatorx

i want figure out how set each char in array-like construction can access 2d-array --> array[2][3], example.

i practicing dictionary test behavior. thought utilize dictionary nested loop , utilize tuple key , pass parsed char string value. thinking might work when seek create dictionary work single line, print statement gives me 1 key , 1 value , not list. confused. know pain please teach me in simple terms? python leaving nasty taste in mouth.

this test code 1 line of text file:

count = 0 text = open("puzzle.txt") line = text.readline() in line: #this problem mydict = dict() mydict.update({count:line[count-1]}) #was hoping create many keys , values. count += 1 print mydict.values() #want see there multiple dict values, each char. print count #just checking see count value

can teach me because instructor not , want learn.

your code has 1 obvious problem @ first glance - you're resetting mydict empty dict each time through loop, line:

mydict = dict()

if moved before loop, test farther see whether there other problems fix.

however, there's much more straightforward way accomplish want:

>>> open("puzzle.txt") f: ... puzzle = [list(line.strip()) line in f]

this opens file puzzle.txt , assigns variable f, reads each line in file string variable line, strips off whitespace (including newline @ end), converts resulting string list of characters, , collects each of lists list assigns variable puzzle. finally, because of done within with statement, file automatically closed.

there couple of of import python concepts in code above:

using statement, , iterating on lines of file list comprehensions

the result of code can seen more if utilize pprint function in pprint module:

>>> pprint import pprint >>> pprint(puzzle) [['h', 'g', 'a', 'm', 'o', 'n', 'i', 'h', 'r', 'a'], ['a', 'o', 'm', 'o', 'k', 'a', 'w', 'o', 'n', 's'], ['n', 'f', 'r', 'o', 'l', 'b', 'o', 'b', 'd', 'n'], ['a', 'r', 'f', 's', 'i', 'h', 'c', 'a', 'g', 'e'], ['l', 'n', 'i', 'e', 'e', 'w', 'o', 'n', 'o', 'k'], ['g', 'o', 'l', 'f', 'u', 'n', 'd', 't', 'h', 'c'], ['k', 'o', 'c', 'a', 't', 'a', 'o', 'h', 'b', 'i'], ['a', 'm', 'r', 'e', 'r', 'c', 'g', 'a', 'n', 'h'], ['s', 'l', 'g', 'f', 'a', 'm', 'a', 'l', 'l', 'c'], ['a', 'l', 'l', 'i', 'g', 'a', 't', 'o', 'r', 'x']]

... , can access individual character proposed in question:

>>> puzzle[2][3] 'o'

python multidimensional-array

No comments:

Post a Comment