Python 如何在列表中存储列表中的整数?
我试图将Python 如何在列表中存储列表中的整数?,python,list,integer,Python,List,Integer,我试图将子项分解为一个列表,首先将它们转换为字符串,然后读取第一个数字。如果第一个数字是1,则附加到列表,并将其存储在子列表[0]中。然后,对于以2开头的整数,在child\u list[1]处将它们排序到另一个列表中,依此类推 现在,此代码只能找到符合条件的第一项。最终输出是字符串并不重要。 谢谢。只需添加下一个代码段: elif int(数字[0])==2: var=str(“.”连接(数字)) 子列表2.append([var]) 并删除该行: child_inc+=1 这一行阻止找到符合
子项
分解为一个列表,首先将它们转换为字符串,然后读取第一个数字。如果第一个数字是1,则附加到列表,并将其存储在子列表[0]
中。然后,对于以2开头的整数,在child\u list[1]
处将它们排序到另一个列表中,依此类推
现在,此代码只能找到符合条件的第一项。最终输出是字符串并不重要。
谢谢。只需添加下一个代码段:
elif int(数字[0])==2:
var=str(“.”连接(数字))
子列表2.append([var])
并删除该行:
child_inc+=1
这一行阻止找到符合条件的所有其他项目。您可以简化顶部的
str
,从中获取数字。
您似乎希望按起始数字建立一个列表。
在需要的数字上运行child_inc
,并检查您拥有的数字。
您在if中增加了这个值,因此只会得到第一个匹配的项。
大概是这样的:
children = [1220, 1221, 2220, 2221, 2222, 3220, 3221, 4220, 4221, 5220]
child_list = []
child_inc = 1
for i in children:
digits = [str(x) for x in str(i)]
if int(digits[0]) == child_inc:
var = str("".join(digits))
child_list.append([var])
child_inc += 1
print(child_list)
children=[1220122212221320232232232213221422042215220]
子列表=[]
对于范围(1,10)内的child_inc:
kids=[]#这里也许dict方法更好:
children = [1220, 1221, 2220, 2221, 2222, 3220, 3221, 4220, 4221, 5220]
child_list = []
for child_inc in range(1,10):
kids = [] #<--- new list for this digit
for i in children:
digits = str(i)
if int(digits[0]) == child_inc:
var = str("".join(digits))
kids.append(var)
child_list.append(kids)
print(child_list)
您必须创建一个辅助列表,并执行嵌套循环,这样做将是:
children = [1220, 1221, 2220, 2221, 2222, 3220, 3221, 4220, 4221, 5220]
strs = [ str(x) for x in children]
res=defaultdict(list)
for s in strs :
res[s[0]].append(s[1:])
print(list(res.values()))
#[['220', '221'], ['220', '221', '222'], ['220', '221'], ['220', '221'], ['220']]
让我们首先了解实际问题:
示例提示:
因此,假设您有一个列表:
children = [1220, 1221, 2220, 2221, 2222, 3220, 3221, 4220, 4221, 5220]
child_list = []
for i in range(1,6):
lst = []
for num in children:
digits = str(num)
if int(digits[0]) == i:
lst.append(digits)
child_list.append(lst)
print(child_list)
res => [['1220', '1221'], ['2220', '2221', '2222'], ['3220', '3221'], ['4220', '4221'], ['5220']]
您希望将其转换为dict,作为元组的第一个元素,作为键,作为元组的第二个元素。比如:
a=[(2006,1),(2007,4),(2008,9),(2006,5)]
但有一个陷阱,您也希望那些具有不同值但键相同的键,如(2006,1)和(2006,5)键相同但值不同。您希望这些值仅附加一个键,以便获得预期的输出:
{2008: [9], 2006: [5], 2007: [4]}
对于这类问题,我们采取如下措施:
children = [1220, 1221, 2220, 2221, 2222, 3220, 3221, 4220, 4221, 5220]
child_list = []
child_inc = 1
for i in children:
digits = [str(x) for x in str(i)]
if int(digits[0]) == child_inc:
var = str("".join(digits))
child_list.append([var])
child_inc += 1
print(child_list)
首先创建一个新的dict,然后我们遵循以下模式:
{2008: [9], 2006: [1, 5], 2007: [4]}
因此,我们首先检查key是否在new dict中,如果它已经存在,则将duplicate key的值添加到其值中:
完整代码:
if item[0] not in new_dict:
new_dict[item[0]]=[item[1]]
else:
new_dict[item[0]].append(item[1])
现在回到实际问题:
您可以使用此模式并使用相同的第一个数字保存int:
a=[(2006,1),(2007,4),(2008,9),(2006,5)]
new_dict={}
for item in a:
if item[0] not in new_dict:
new_dict[item[0]]=[item[1]]
else:
new_dict[item[0]].append(item[1])
print(new_dict)
输出:
children = [1220, 1221, 2220, 2221, 2222, 3220, 3221, 4220, 4221, 5220]
track={}
for i in children:
if str(i)[:1] not in track:
track[str(i)[:1]]=[i]
else:
track[str(i)[:1]].append(i)
print(track.values())
发布预期结果这应该做什么digits=[str(x)代表str(i)]
?它与list(str(i))
完全相同。第一个str
调用是完全冗余的。是否要处理所有可能的数字0
到9
并为不匹配的数字提供空列表?“将以1开头的数字放入子列表[0]
中,然后将以2开头的数字放入子列表[1]
…”你知道,如果你使用字典,你可以只做child\u dict[string\u number[0]]。append(string\u number)
或类似的操作(可能使用defaultdict(list)
)你没有使用child\u inc
是吗?@doctorlove你说得对,谢谢!回答得好BenjaminThank You@damianalatteneroth这正是我想要做的。谢谢