Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.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_Python 3.x_List_Dictionary_Indentation - Fatal编程技术网

如何在python中根据不同的条件动态创建列表?

如何在python中根据不同的条件动态创建列表?,python,python-3.x,list,dictionary,indentation,Python,Python 3.x,List,Dictionary,Indentation,文本文件: Animals Tiger Lion Cat Mice Birds Parrot Peacock Hen chicken Reptiles Mammals 我想根据缩进动态分离单词,并将其存储在不同的列表中 预期产出: a=['Animals','Birds','Reptiles','Mammals'] b=['Tiger','Lion','Parrot','Peacock'] c=['Cat','Hen'] d=['Mice

文本文件:

Animals
  Tiger
  Lion
    Cat
      Mice
Birds
  Parrot
  Peacock
    Hen
      chicken
Reptiles
Mammals
我想根据缩进动态分离单词,并将其存储在不同的列表中

预期产出:

a=['Animals','Birds','Reptiles','Mammals']
b=['Tiger','Lion','Parrot','Peacock']
c=['Cat','Hen']
d=['Mice','Chicken']
这是我的密码:

a=[]
b=[]
c=[]
d=[]
for i in text:
  indent = len(i)-len(i.lstrip())
  if indent == 0:
    a.append(i)
  if indent == 2:
    b.append(i)
  if indent == 4:
    c.append(i)
  if indent == 6:
    d.append(i)        
它给出了预期的输出,但我希望采用动态方法。

您可以使用:

0[“动物”、“鸟类”、“爬行动物”、“哺乳动物”] 1[“老虎”、“狮子”、“鹦鹉”、“孔雀”] 2[“猫”、“母鸡”] 3[‘老鼠’、‘鸡’]
也许你应该用字典

 data = {0:[...], 1:[...], ...}
然后你可以使用

data[indent].append(i) 
因此,它将与任何缩进一起工作,并且如果,您将不需要所有这些

text = '''Animals
  Tiger
  Lion
    Cat
      Mice
Birds
  Parrot
  Peacock
    Hen
      chicken
Reptiles
Mammals'''

data = {}

for line in text.split('\n'):
    striped = line.lstrip()
    indent = len(line) - len(striped)

    # create empty list if not exists
    if indent not in data:
        data[indent] = []

    # add to list
    data[indent].append(striped)

#print(data)

for key, value in data.items():
    print(key, value)
结果:

0 ['Animals', 'Birds', 'Reptiles', 'Mammals']
2 ['Tiger', 'Lion', 'Parrot', 'Peacock']
4 ['Cat', 'Hen']
6 ['Mice', 'chicken']

在动态规划中,应该使用字典和列表,而不是单独的变量
a
b
c
d
。最终,您可以将字典与字符串键
“a”
“b”
“c”
“d”
一起使用,但您始终可以将值
0,2,4
转换为字符
a,b,c

for key, value in data.items():
    char = chr(ord('a') + key//2)
    print(char, value)
结果

a ['Animals', 'Birds', 'Reptiles', 'Mammals']
b ['Tiger', 'Lion', 'Parrot', 'Peacock']
c ['Cat', 'Hen']
d ['Mice', 'chicken']

您可以使用缩进长度为键的词典尝试以下操作:

d={}

def categ(x):
    n=0
    i=0
    while x[i]==' ':
        n+=1
        i+=1
    return n

for i in text:
    if categ(i) in d:
        d[categ(i)].append(i[categ(i):])
    else:
        d[categ(i)]=[i[categ(i):]]

也许你应该使用dictionary
data={0:[…],1:[…],…}
,然后你可以使用
data[indent].append(i)
-这样它就可以处理任何缩进,如果
,你就不需要所有这些
。它起作用了,你让这变得很容易。
a ['Animals', 'Birds', 'Reptiles', 'Mammals']
b ['Tiger', 'Lion', 'Parrot', 'Peacock']
c ['Cat', 'Hen']
d ['Mice', 'chicken']
d={}

def categ(x):
    n=0
    i=0
    while x[i]==' ':
        n+=1
        i+=1
    return n

for i in text:
    if categ(i) in d:
        d[categ(i)].append(i[categ(i):])
    else:
        d[categ(i)]=[i[categ(i):]]