如何根据 python 中的不同条件动态创建列表?
How to create list dynamically based on different conditions in python?
文本文件:
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)
它给出了预期的输出,但我希望它采用动态方法。
您可以使用 defaultdict
:
from collections import defaultdict
data = defaultdict(list)
with open('test.txt') as f:
for line in f:
indentation_level = (len(line) - len(line.lstrip())) // 2
data[indentation_level].append(line.strip())
for indent_level, values in data.items():
print(indent_level, values)
0 ['Animals', 'Birds', 'Reptiles', 'Mammals']
1 ['Tiger', 'Lion', 'Parrot', 'Peacock']
2 ['Cat', 'Hen']
3 ['Mice', 'chicken']
也许你应该使用字典
data = {0:[...], 1:[...], ...}
然后你就可以使用
data[indent].append(i)
所以它适用于任何缩进,你不需要所有这些 if
。
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):]]
文本文件:
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)
它给出了预期的输出,但我希望它采用动态方法。
您可以使用 defaultdict
:
from collections import defaultdict
data = defaultdict(list)
with open('test.txt') as f:
for line in f:
indentation_level = (len(line) - len(line.lstrip())) // 2
data[indentation_level].append(line.strip())
for indent_level, values in data.items():
print(indent_level, values)
0 ['Animals', 'Birds', 'Reptiles', 'Mammals'] 1 ['Tiger', 'Lion', 'Parrot', 'Peacock'] 2 ['Cat', 'Hen'] 3 ['Mice', 'chicken']
也许你应该使用字典
data = {0:[...], 1:[...], ...}
然后你就可以使用
data[indent].append(i)
所以它适用于任何缩进,你不需要所有这些 if
。
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):]]