简体   繁体   English

布尔Python的动态列表

[英]Dynamic list of booleans python

This is a very basic example. 这是一个非常基本的例子。 I want to keep a list of variable/flags but i know that lists are immutable. 我想保留一个变量/标志列表,但我知道列表是不可变的。 In the example below, I could simply append a new tuple to hasmoustache (in a log type way) but the list hasmoustache would keep on expanding. 在下面的示例中,我可以简单地将一个新的元组附加到hasmoustache(以日志类型的方式),但是列表hasmoustache将会继续扩展。

I have 3 questions: How to replace an element in one of the tuples in a list (see comment in the example below) - like changing the bole an in the tuples containing sam and sarah in hasmoustache? 我有3个问题:如何替换列表中一个元组中的元素(请参见下面示例中的注释)-就像在hasmoustache中的包含sam和sarah的元组中更改bole an一样? Is it good practice knowing that list are immutable? 知道列表是不可变的,这是一种好习惯吗? Is there another/cleaner way to keep a finite list of flag? 还有另一种/更清洁的方法来保留标志的有限列表吗?

hasmoustache=[('jon',False,'Male'),('sam',False,'Male'),('sarah',False,'Female')]
# hasmoustache is list of tuples describing (name,has a moustache?,gender)

name = 'Joe'
gender = 'Male'
if name not in hasmoustache:
    append hasmoustache.((name,False,gender))

for y in hasmoustache:
    print y

barber=[('jon',1),('sam',8),('sarah',10)]
# barber is a list of tuples describing (name,number of weeks since last visit to the barber)
for m in barber:
    if m[1]>4
        # Do something to change tuple with name=m[0] in hasmoustache to True
        # List in python are immutable so how to do it?

callthem = [x[0] for x in hasmoustache if x[1]]

for y in callthem:
    print y

for y in hasmoustache:
    print y

Output should show: ('jon', False, 'Male') ('sam', False,'Male') ('sarah', False, 'Female') ('Joe', False, 'Male') sam sarah ('jon', False, 'Male') ('sam', True, 'Male') ('sarah', True, 'Female') ('Joe', False, 'Male') 输出应显示:('jon',False,'Male')('sam',False,'Male')('sarah',False,'Female')('Joe',False,'Male')sam sarah ('jon',False,'Male')('sam',True,'Male')('sarah',True,'Female')('Joe',False,'Male')

You have it a little backwards. 你有一点点倒退。 Lists are mutable, meaning you can modify their values after they are created , Tuples are immutable sequence types: such objects cannot be modified once created . 列表 是可变的,这意味着您可以在创建 列表 后修改其值元组 是不可变的序列类型: 创建后就无法修改 此类对象 If you wanted to have a structure to achieve what you want, you can use a list of lists. 如果您想要一种结构来实现所需的功能,则可以使用列表列表。

barber=[['jon',1],['sam',8],['sarah',10]]

mutable sequence types 可变序列类型

So the answer to question 1. 所以问题1的答案。

You cannot change the elements of a tuple so without creating a completely new tuple and replacing the current tuple in the list with that tuple you cannot do what you expect. 您无法更改元组的元素,因此,如果不创建全新的元组并将列表中的当前元组替换为该元组,则您将无法执行预期的工作。

Question 2. 问题2。

Lists are not immutable so that is non event. 列表不是一成不变的,因此这不是事件。

Question 3. 问题3。

You could use a dict using names as keys if they will all be unique and keep a list of information as values. 如果名称都是唯一的,并且可以保留信息列表作为值,则可以使用名称作为键的dict

This is an example of using a dict: 这是使用字典的示例:

barber={'jon': 1,'sam':8,'sarah': 10 }

if barber.get("sam")  > 4: # get value of sam
   print barber.get("sam")     
8

Make hasmoustace a dict also: 使hasmoustace成为一个命令:

hasmoustache={'jon':[False,'Male'],'sam':[False,'Male'],'sarah':[False,'Female']}
barber={'jon':1,'sam':8,'sarah':10}

if barber.get("sam")  > 4:
   hasmoustache.get("sam")[0]=  True
print hasmoustache
{'sarah': [False, 'Female'], 'sam': [True, 'Male'], 'jon': [False, 'Male']}

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

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