Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于用户输入的循环矩阵的Python_Python_Arrays_Matrix - Fatal编程技术网

用于用户输入的循环矩阵的Python

用于用户输入的循环矩阵的Python,python,arrays,matrix,Python,Arrays,Matrix,所以我已经在Overflow上搜索了几天,寻找我正在解决的一个问题。我理解社区为限制家庭作业问题所做的努力,但我被难住了,我想学习这个概念并继续学习更多的编程 在Python中,我正在开发矩阵或2D数组 以下是用户对阵列的编程要求,并将该值与阵列值进行比较: 然后要求用户输入其中一个用户的名字和姓氏 在矩阵中,然后打印相应的信息(整行) 如发现该人,;如果未找到,请打印“未找到用户!” 以下是我到目前为止对该阵列的了解 rows = 5 cols = 7 names_matrix = ([['l

所以我已经在Overflow上搜索了几天,寻找我正在解决的一个问题。我理解社区为限制家庭作业问题所做的努力,但我被难住了,我想学习这个概念并继续学习更多的编程

在Python中,我正在开发矩阵或2D数组

以下是用户对阵列的编程要求,并将该值与阵列值进行比较:

然后要求用户输入其中一个用户的名字和姓氏 在矩阵中,然后打印相应的信息(整行) 如发现该人,;如果未找到,请打印“未找到用户!”

以下是我到目前为止对该阵列的了解

rows = 5
cols = 7
names_matrix = ([['lname', 'fname', 'city', 'state', 'zipcode'],
                 ['Zdolfalos', 'Johnson', 'Terrell', 'Wilson', 'Key', 'Smith',     
                  'Alfonso'], 
                 ['Fred', 'Malcom', 'Monkey', 'Wilson', 'LeDoor', 'Jim Bob', 'Ralph'],
                 ['Charlotte', 'Monroe', 'Broken Pine', 'Hogwart', 'Spot in Road',  
                  'Denver','Gastonia'], 
                 ['NC', 'NC', 'SC', 'VA', 'AL', 'NC', 'NC' ],
                 ['28210', '28337', '28974', '27457', '36827', '28037', '28559'] ])

print names_matrix

#Create a Boolean variable flag.
found = False

#Create a variable to use as a loop counter.
index = 0

#Get the strings to search for.

for in names_matrix:  #Having problems here with what goes here in For In Loop
    userFirstName = raw_input('What is the user first name?')
    userLastName =  raw_input('What is the user last name?')

    if userFirstName == names_matrix [1] and userLastName == names_matrix [0]:
        print('')
    #I want to print the Matrix value from the user input
    else
        print('User Not Found!')
# nested find columns
# https://stackoverflow.com/questions/7845165/how-to-take-input-in-an-array-python
我是python和编程新手,在书中看到了他们是如何使用false和index进行While循环的。我想我也很难理解传递值和引用

# Get the string to search for.
searchValue = raw_input('Enter a name to search for in the list: ')
# Step through the list searching for the
# specified name.
while found == False and index < len(names):
    if names[index] == searchValue:
        found = True
    else:
        index = index + 1
# Display the search results.
if found:
    print 'That name was found in element ' + str(index + 1)
else:
    print 'That name was not found in the list.'
#获取要搜索的字符串。
searchValue=raw_input('在列表中输入要搜索的名称:')
#在列表中单步搜索
#指定的名称。
while found==False,index
我想知道如何使用For-In-Range循环来实现这一点。它可能是一个嵌套的循环,这些循环有点棘手

我不认为我需要布尔标志或索引部分在我的范围内循环编码的开始。我只是展示了到目前为止我的进步,并试图让这个计划更好地运作

我在范围内研究了以下有用的链接,但被难住了

我们没有讨论过类和对象,也没有讨论过如何实现这一点,我们也没有讨论过numpy并尝试使用import numpy,但在这方面遇到了一些问题,因为我也是numpy新手。我正在阅读《用Python像CS一样思考》一书,这是对本课程的额外帮助,也是对Python的艰苦学习


感谢您的时间。

循环迭代的
的正确语法是:

for x in iterable_object:
    # do something with each x
在英语中,将
iterable\u对象中的每一项都称为
x
,并执行一些涉及x的代码

对于
范围
对象:

for i in range(0,10,1):
    print i 
这将打印
0-9
中的数字,即
i
将具有值
0
,并由第三个参数
1
递增,直到其值
10
,并且不会重新进入循环,这意味着最后打印的值将是
9

Python允许对此进行一些速记调用:

for i in range(10):
    print i
做同样的事情。当提供一个参数时,它被解释为上限

参考:

在您的情况下,您拥有以下数据:

names_matrix = ([['lname', 'fname', 'city', 'state', 'zipcode'],
             ['Zdolfalos', 'Johnson', 'Terrell', 'Wilson', 'Key', 'Smith',     
              'Alfonso'], 
             ['Fred', 'Malcom', 'Monkey', 'Wilson', 'LeDoor', 'Jim Bob', 'Ralph'],
             ['Charlotte', 'Monroe', 'Broken Pine', 'Hogwart', 'Spot in Road',  
              'Denver','Gastonia'], 
             ['NC', 'NC', 'SC', 'VA', 'AL', 'NC', 'NC' ],
             ['28210', '28337', '28974', '27457', '36827', '28037', '28559'] ])
您可能希望信息按列正确排列并忽略标题

iteration 1 - Zdolfalos, Fred, Charlotte, NC, 28210
iteration 2 - Johnson, Malcom, Monroe, NC, 28337
etc ...
这意味着您希望遍历列表中的第二个对象
名称矩阵[1]
的大小

L = len(names_matrix[1])
print names_matrix[0][0],names_matrix[0][2],names_matrix[0][2],names_matrix[0][3],names_matrix[0][4]
for i in range(L):
    print names_matrix[1][i],names_matrix[2][i],names_matrix[3][i],names_matrix[4][i],names_matrix[5][i]
将为您提供:

lname fname city state zipcode
Zdolfalos Fred Charlotte NC 28210
Johnson Malcom Monroe NC 28337
Terrell Monkey Broken Pine SC 28974
Wilson Wilson Hogwart VA 27457
Key LeDoor Spot in Road AL 36827
Smith Jim Bob Denver NC 28037
Alfonso Ralph Gastonia NC 28559
看起来您正试图在数据中搜索某人。我想说的是在循环之前执行用户输入,然后与上面执行的索引进行比较

注意,我发现您的数据以一种非常奇怪的方式排列。我希望将其结构设置为:

names_matrix = (
   [['lname',     'fname',   'city', 'state', 'zipcode'],               
    ['Zdolfalos', 'Fred',    'Charlotte','NC','28210'],
    ['Malcom',    'Johnson', 'Monroe', 'NC', '28337',],
    ['Monkey',    'Terrell', 'Broken Pine', 'SC','28974',],
    # ...etc...
   ] 
使迭代变得非常简单,以便迭代条目:

for user in names_matrix[1:]: # [1:] means take the list from the 1st element to the end, noted by the lack of a number after the colon (on a 0 based index)
    print user
python具有惊人的性能,为此类转换提供了非常快速和简单的操作:

names_matrix = zip(*names_matrix[1:])
本例中的函数告诉python获取矩阵,不包括第一个条目(即标题)

([['lname', 'fname', 'city', 'state', 'zipcode'],
             ['Zdolfalos', 'Johnson', 'Terrell', 'Wilson', 'Key', 'Smith',     
              'Alfonso'], 
             ['Fred', 'Malcom', 'Monkey', 'Wilson', 'LeDoor', 'Jim Bob', 'Ralph'],
             ['Charlotte', 'Monroe', 'Broken Pine', 'Hogwart', 'Spot in Road',  
              'Denver','Gastonia'], 
             ['NC', 'NC', 'SC', 'VA', 'AL', 'NC', 'NC' ],
             ['28210', '28337', '28974', '27457', '36827', '28037', '28559'] ])
按每个条目进行升级,这些条目是您的类别

zip(  ['Zdolfalos', 'Johnson', 'Terrell', 'Wilson', 'Key', 'Smith','Alfonso'],
      ['Fred', 'Malcom', 'Monkey', 'Wilson', 'LeDoor', 'Jim Bob', 'Ralph'],
      ['Charlotte', 'Monroe', 'Broken Pine', 'Hogwart', 'Spot in Road','Denver','Gastonia'], 
      ['NC', 'NC', 'SC', 'VA', 'AL', 'NC', 'NC' ],
      ['28210', '28337', '28974', '27457', '36827', '28037', '28559'] )
并通过索引将每个列表配对成
tuple
s:

[ ('Zdolfalos', 'Fred', 'Charlotte', 'NC', '28210'),
  ('Johnson', 'Malcom', 'Monroe', 'NC', '28337'),
  ('Terrell', 'Monkey', 'Broken Pine', 'SC', '28974'),
# ... etc ...
]
现在,您可以遍历用户,而不必处理当前设置所需的更复杂的索引

如果您希望以当前的格式保存原始数据,则可以将其作为一个临时步骤来完成,以便于使用

当然,如果您不想更改数据,您仍然可以这样做

userFirstName = raw_input('What is the user first name?')
userLastName =  raw_input('What is the user last name?')
L = len(names_matrix[1])
for i in range(L):
    if userFirstName == names_matrix[0][i] and userLastName == names_matrix[1][i]:
        print('Found!')
    else
        print('User Not Found!')

此格式可能会为您提供一些如何实现所需内容的想法

循环迭代的
正确语法为:

for x in iterable_object:
    # do something with each x
在英语中,将
iterable\u对象中的每一项都称为
x
,并执行一些涉及x的代码

对于
范围
对象:

for i in range(0,10,1):
    print i 
这将打印
0-9
中的数字,即
i
将具有值
0
,并由第三个参数
1
递增,直到其值
10
,并且不会重新进入循环,这意味着最后打印的值将是
9

Python允许对此进行一些速记调用:

for i in range(10):
    print i
做同样的事情。当提供一个参数时,它被解释为上限

参考:

在您的情况下,您拥有以下数据:

names_matrix = ([['lname', 'fname', 'city', 'state', 'zipcode'],
             ['Zdolfalos', 'Johnson', 'Terrell', 'Wilson', 'Key', 'Smith',     
              'Alfonso'], 
             ['Fred', 'Malcom', 'Monkey', 'Wilson', 'LeDoor', 'Jim Bob', 'Ralph'],
             ['Charlotte', 'Monroe', 'Broken Pine', 'Hogwart', 'Spot in Road',  
              'Denver','Gastonia'], 
             ['NC', 'NC', 'SC', 'VA', 'AL', 'NC', 'NC' ],
             ['28210', '28337', '28974', '27457', '36827', '28037', '28559'] ])
您可能希望信息按列正确排列并忽略标题

iteration 1 - Zdolfalos, Fred, Charlotte, NC, 28210
iteration 2 - Johnson, Malcom, Monroe, NC, 28337
etc ...
这意味着您希望遍历列表中的第二个对象
名称矩阵[1]
的大小

L = len(names_matrix[1])
print names_matrix[0][0],names_matrix[0][2],names_matrix[0][2],names_matrix[0][3],names_matrix[0][4]
for i in range(L):
    print names_matrix[1][i],names_matrix[2][i],names_matrix[3][i],names_matrix[4][i],names_matrix[5][i]
将为您提供:

lname fname city state zipcode
Zdolfalos Fred Charlotte NC 28210
Johnson Malcom Monroe NC 28337
Terrell Monkey Broken Pine SC 28974
Wilson Wilson Hogwart VA 27457
Key LeDoor Spot in Road AL 36827
Smith Jim Bob Denver NC 28037
Alfonso Ralph Gastonia NC 28559
看起来您正试图在数据中搜索某人。我想说的是在循环之前执行用户输入,然后与上面执行的索引进行比较

注意,我发现您的数据以一种非常奇怪的方式排列。我希望将其结构设置为:

names_matrix = (
   [['lname',     'fname',   'city', 'state', 'zipcode'],               
    ['Zdolfalos', 'Fred',    'Charlotte','NC','28210'],
    ['Malcom',    'Johnson', 'Monroe', 'NC', '28337',],
    ['Monkey',    'Terrell', 'Broken Pine', 'SC','28974',],
    # ...etc...
   ] 
使迭代变得非常简单,以便迭代条目:

for user in names_matrix[1:]: # [1:] means take the list from the 1st element to the end, noted by the lack of a number after the colon (on a 0 based index)
    print user
python具有惊人的性能,为此类转换提供了非常快速和简单的操作:

names_matrix = zip(*names_matrix[1:])
函数i