更改namedtuples列表的值

时间:2015-07-06 18:20:37

标签: python list python-3.x tuples

我有一个名为Books的命名元组列表,我正在尝试将price字段增加20%,这确实会更改Books的值。我试着这样做:

from collections import namedtuple
Book = namedtuple('Book', 'author title genre year price instock')
BSI = [
       Book('Suzane Collins','The Hunger Games', 'Fiction', 2008, 6.96, 20),
       Book('J.K. Rowling', "Harry Potter and the Sorcerer's Stone", 'Fantasy', 1997, 4.78, 12)]
for item in BSI:
    item = item.price*1.10
print(item.price)

但我一直在接受:

 Traceback (most recent call last):
 print(item.price)
 AttributeError: 'float' object has no attribute 'price'

据我所知,我无法在namedtuple中设置字段。如何更新price

我试图把它变成一个函数:

def restaurant_change_price(rest, newprice):
    rest.price = rest._replace(price = rest.price + newprice)
    return rest.price

print(restaurant_change_price(Restaurant("Taillevent", "French", "343-3434", "Escargots", 24.50), 25))

但是我收到了错误的说法:

 rest.price = rest._replace(price = rest.price + newprice)
 AttributeError: can't set attribute

有人能告诉我为什么会这样吗?

3 个答案:

答案 0 :(得分:35)

命名元组是不可变的,因此您无法操纵它们。

正确的做法:

如果您想要可变,可以使用recordtype

from recordtype import recordtype

Book = recordtype('Book', 'author title genre year price instock')
books = [
   Book('Suzane Collins','The Hunger Games', 'Fiction', 2008, 6.96, 20),
   Book('J.K. Rowling', "Harry Potter and the Sorcerer's Stone", 'Fantasy', 1997, 4.78, 12)]

for book in books:
    book.price *= 1.1
    print(book.price)

PS:如果您没有安装,可能需要pip install recordtype

这样做的方法不好:

您也可以使用namedtuple方法继续使用from collections import namedtuple Book = namedtuple('Book', 'author title genre year price instock') books = [ Book('Suzane Collins','The Hunger Games', 'Fiction', 2008, 6.96, 20), Book('J.K. Rowling', "Harry Potter and the Sorcerer's Stone", 'Fantasy', 1997, 4.78, 12)] for i in range(len(books)): books[i] = books[i]._replace(price = books[i].price*1.1) print(books[i].price)

viewDidLoad()

答案 1 :(得分:4)

这看起来像Python的数据分析库pandas的任务。这样做真的非常容易:

In [6]: import pandas as pd
In [7]: df = pd.DataFrame(BSI, columns=Book._fields)
In [8]: df
Out[8]: 
           author                                  title    genre  year  \
0  Suzane Collins                       The Hunger Games  Fiction  2008   
1    J.K. Rowling  Harry Potter and the Sorcerers Stone  Fantasy  1997   

   price  instock  
0   6.96       20  
1   4.78       12  

In [9]: df['price'] *= 100
In [10]: df
Out[10]: 
           author                                  title    genre  year  \
0  Suzane Collins                       The Hunger Games  Fiction  2008   
1    J.K. Rowling  Harry Potter and the Sorcerer's Stone  Fantasy  1997   

   price  instock  
0    696       20  
1    478       12  

现在这不仅仅比使用namedtuple s更好,更好吗?

答案 2 :(得分:1)

在Python> = 3.7中,您可以使用带有新变量注释功能的dataclass装饰器来生成可变的记录类型:

/*
* @typedef {import('../lib/mycustomservice')} CustomService
*
*/ 
class abcController 
{     
    /**
     * @param {CustomService} mycustomservice
     */

    constructor(mycustomservice)
    {

        this.customService = mycustomservice;
        ....
        ....
    }
}

输出:

from dataclasses import dataclass


@dataclass
class Book:
    author: str
    title: str
    genre: str
    year: int
    price: float
    instock: int


BSI = [
    Book("Suzane Collins", "The Hunger Games", "Fiction", 2008, 6.96, 20),
    Book(
        "J.K. Rowling",
        "Harry Potter and the Sorcerer's Stone",
        "Fantasy",
        1997,
        4.78,
        12,
    ),
]

for item in BSI:
    item.price *= 1.10
    print(f"New price for '{item.title}' book is {item.price:,.2f}")