简体   繁体   English

二维数组结构python

[英]2d array structure python

I am taking a python class and have no experience in python. 我正在上一个python课程,没有python经验。 The instructor only teaches documentation and refuses to give code examples. 讲师仅讲授文档,拒绝提供代码示例。 I understand C++ but I'm getting very frustrated with python because I am basically teaching myself. 我了解C ++,但是我对python感到非常沮丧,因为我基本上是在自学。

I want to open a text file that contains one string of random characters per line. 我想打开一个文本文件,每行包含一个随机字符字符串。 The length could be 8, 10, 15, or 20 chars, for example. 例如,长度可以是8、10、15或20个字符。 It has to be flexible. 它必须灵活。 There can be an unspecified number of strings (lines) in the text file. 文本文件中可以有未指定数量的字符串(行)。

The text file might look like this for example: 文本文件可能看起来像这样:

HGAMONIHRA
AOMOKAWONS
NFROLBOBDN
ARFSIHCAGE
LNIEEWONOK
GOLFUNDTHC
KOCATAOHBI
AMRERCGANH
SLGFAMALLC
ALLIGATORX

I just want to figure out how to put each char in an array-like structure where I can access it like a 2d-array --> my array[2][3], for example. 我只想弄清楚如何将每个字符放入类似数组的结构中,就可以像2d数组->我的array [2] [3]那样访问它。

I was practicing with a dictionary just to test the behavior. 我在用字典练习只是为了测试行为。 I thought that I could use a dictionary with a nested for loop and then use a tuple as a key and pass a parsed char from the string as a value. 我以为我可以使用带嵌套的for循环的字典,然后使用元组作为键,并从字符串中传递一个解析的char作为值。 I was thinking this might work but when I try to make a dictionary work with just a single line, my print statement only gives me one key and one value and not the list. 我以为这可能行得通,但是当我尝试使字典仅用一行执行时,我的print语句仅给我一个键和一个值,而不给列表。 I am really confused. 我真的很困惑。 I know this is a pain but will someone please teach me in simple terms? 我知道这很痛苦,但是有人可以简单地教我吗? python is leaving a nasty taste in my mouth. 蟒蛇在我的嘴里留下令人讨厌的味道。

This was my test code for just one line of a text file: 这是我仅测试文本文件一行的代码:

count = 0
text = open("puzzle.txt")
line = text.readline()
for i in line:  #this is probably the problem
    mydict = dict()
    mydict.update({count:line[count-1]}) #was hoping to make many keys and values.
    count += 1

print mydict.values() #want to see there are multiple dict values, each as a char.
print count #just checking to see what count value was

Can someone teach me because my instructor will not and I really want to learn. 有人可以教我,因为我的老师不会,我真的很想学习。

Python strings are treated as immutable sets (alike a tuple), so you only need to hold a set of strings of each line. Python字符串被视为不可变的集合(就像元组一样),因此您只需要保存每一行的一组字符串即可。

count = 0
text = open("puzzle.txt", 'r')
myLines = text.readlines()

#Usage;
print myLines[0][0]
# Prints the first character

Because python strings are immutable you will need to set the whole index as a new string 由于python字符串是不可变的,因此您需要将整个索引设置为新字符串

myLines[0] = mylines[0:1] + 'G' + myLines[2:]

Your code has one obvious problem at first glance - you're resetting mydict to an empty dict each time through the loop, with your line: 乍看之下,您的代码有一个明显的问题-您每次通过循环将mydict重置为一个空dict,并使用以下行:

    mydict = dict()

If you moved that so it was immediately before the loop, you could then test further to see whether there were other problems to fix. 如果您将其移动到紧接循环之前,则可以进一步测试以查看是否还有其他问题需要解决。

However, there's a much more straightforward way to achieve what you want: 但是,有一种更直接的方法来实现您想要的目标:

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

This opens your file puzzle.txt and assigns it to the variable f , then reads each line in the file into a string variable line , strips off any whitespace (including the newline at the end), converts the resulting string into a list of characters, and finally collects each of those lists into another list which it assigns to the variable puzzle . 这将打开您的文件puzzle.txt并将其分配给变量f ,然后将文件中的每一行读入字符串变量line ,剥离所有空格(包括末尾的换行符),将结果字符串转换为字符列表,最后将每个列表收集到另一个列表中,并将其分配给变量puzzle Finally, because all of this was done inside a with statement, the file is automatically closed. 最后,由于所有这些操作都是在with语句内完成的,因此该文件将自动关闭。

There are a couple of important Python concepts in the code above: 上面的代码中有两个重要的Python概念:

The result of this code can be seen more clearly if you use the pprint function in the pprint module: 如果在pprint模块中使用pprint函数,则可以更清楚地看到此代码的结果:

>>> from 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']]

... and you can access an individual character exactly as you proposed in your question: ...,然后您可以完全按照问题中的建议访问单个字符:

>>> puzzle[2][3]
'O'

You could do it with a list of lists: 您可以使用列表列表来做到这一点:

>>> with open('puzzle.txt') as f:
...   a = [list(line.rstrip()) for line in f]
... 
>>> a[0,3]
>>> a[0][3]
'M'
>>> a[1][0]
'A'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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