使用另一个数组搜索数组

时间:2017-12-11 22:42:45

标签: python arrays python-3.x

我将文本文件作为多维数组拉入,并让用户选择其中一个项目并将其存储在另一个数组中。我试图找出如何使用第二个元素找到第一个数组的索引。

代码:

+-------+--------+------+----------+
| sale# | SaleID | POID | Received |
+-------+--------+------+----------+
| GHI   |      3 |    4 | True     |
| GHI   |      3 |    5 | True     |
| GHI   |      3 |    5 | True     |
+-------+--------+------+----------+

文字档案:

import {CollapsePanelProps as OriginalCollapsePanelProps} from 'antd/Collapse';

export interface CollapsePanelProps extends OriginalCollapsePanelProps {
  showArrow?: boolean;
}

我试过了

with open("books.txt") as b:
    books = b.readlines()[7:]

books = [x.strip() for x in books]
books = [x.split(",") for x in books]

def welcome():
    print("Welcome to the Bookstore")
    global name
    name = input("What is your name? ")
    print("Our current list of books are: ")
    inventory()

def choice():
    select = input("Which books would you like? (ID):\n")
    global chosen
    chosen = []
    flag = "y"

    while flag == "y":
        chosen.append(select)

        flag = input("Would you like to add more books to your cart? (y/n): ")
    print(chosen)

    for chosen in books:
        books.index(chosen[0])

def inventory():
    length = len(books)
    for i in range(length):
        print(books[i][0], books[i][1].strip(), ("$" + books[i][2]).replace(" ", ""))
    choice()

def receipt():
    print("Thank you", name)

welcome()

如果我选择To add books to your store please have a new book on each line, and use the format ItemNumber,BookName,BookPrice an example would be as follows: B142, Prelude to Programing, 5.25 Please start entering books under the heading Books Available. Thank You Books Available: B12, Prelude to Programing, 5.25 B13, Lazy Python, 10.25 B14, Coding for Dummys, 19.25 ,我希望结果为for chosen in books: books.index(chosen[0]) B12索引号。

1 个答案:

答案 0 :(得分:1)

的问题:

  1. 您正在chosen行覆盖for chosen in books:
  2. 循环提示输入更多图书只会在输入y时附加最后选择的图书ID。
  3. 我的编辑器中的select字作为模块select存在。您可能想要更改名称。
  4. 用此更改替换choice()。

    def choice():
        global chosen
        chosen = []
    
        while True:
            select = input("Which books would you like? (ID):\n")
            chosen.append(select)
    
            flag = input("Would you like to add more books to your cart? (y/n): ")
            if flag != 'y':
                break
        print(chosen)
    
        index = []
        for item in chosen:
            for idx, book in enumerate(books):
                if item == book[0]:
                    index.append([idx, 0])
    
        print('index:', index)
    

    索引列表包含ie [[2, 0], ...]

    2是在书中找到书的索引。 0是书id的索引。 如果结果不完全符合您的要求,您可以进行所需的任何更改。

    存储图书ID意味着稍后搜索。你可以存储书的索引。

    def choice():
        global chosen
        chosen = []
    
        while True:
            select = input("Which books would you like? (ID):\n")
            # Get the index of the selected book in books.
            for idx, book in enumerate(books):
                if select == book[0]:
                    chosen.append(idx)
                    break
    
            flag = input("Would you like to add more books to your cart? (y/n): ")
            if flag != 'y':
                break
        print(chosen)
    
        # [[0, 0], ...]
        result = [[idx, 0] for idx in chosen]
        print(result)
    

    此函数存储所选书籍的索引而不是书籍ID的ID,因为稍后使用索引会更方便,因为列表理解的使用最后会显示。