如何在不在列表中插入列表的情况下附加到列表?
How to append to list without inserting a list within a list?
我有一本字典,其中 origin:row_rate 作为键值对。 df2 是一个 pandas 数据框,当我遍历数据框时,我想将 row_rate 附加到它们对应的原始键的值中,如果它们的键已经在字典中的话。否则,我想为原点创建新密钥并保存当前 row_rate 和与该原点密钥对应的所有其他 row_rate。
我试过这个:
data = {'Cuba': 999}
def append_rating(country, data):
if country in data:
return data[country]
for row in df2.itertuples(index=True):
index = row.Index
origin = row._9
row_rate = row.Rating
values = [append_rating(origin, data)]
print(origin, values)
values.append(row_rate)
data[origin] = values
print(data)
这个 returns 这个:
Congo [[[[[[[[[[None, 3.75], 3.5], 3.5], 3.5], 3.75], 3.0], 2.75], 3.25], 3.25]]
还有这个……
def append_rating(country, data):
if country in data:
return data[country]
else:
return []
其中 returns 这个:
Congo [[[[[[[[[[[], 3.75], 3.5], 3.5], 3.5], 3.75], 3.0], 2.75], 3.25], 3.25]]
我要的是列表的所有元素,只驻留在一个列表中。是这样的:
Congo [3.75, 3.5, 3.5, 3.5, 3.75, 3.0, 2.75, 3.25, 3.25]
抱歉这个基本问题。感谢您的帮助:)
我相信您正在寻找 list.extend(other_list)
方法。
https://docs.python.org/3/tutorial/datastructures.html
a = [1, 2, 3]
b = [4, 5, 6]
a.extend(b)
a == [1, 2, 3, 4, 5, 6]
您可以简单地使用 list.extend()
初步评论
首先:你希望data
包含列表,所以最好将data['Cuba']
初始化为单例列表[999]
而不是到 int 999
.
其次: 您将拥有以下逻辑:
If origin
is already in data
, append to data[origin]
; otherwise,
create a new entry data[origin]
.
当你有这种逻辑时,你可以使用 defaultdict
而不是 dict
。 defaultdict
是dict
的子类,在需要的时候自动创建不存在的条目,并用默认值初始化;在我们的例子中,默认值将是一个空列表。
使用 defaultdict
考虑到这两点,我们可以简化您的代码:
from collections import defaultdict
data = defaultdict(list, {'Cuba': [999]})
for row in df2.itertuples(index=True):
index = row.Index
origin = row._9
row_rate = row.Rating
data[origin].append(row_rate)
print(data)
使用if
/else
如果我们没有使用 defaultdict
,代码会类似但稍微复杂一些:
data = {'Cuba': [999]}
for row in df2.itertuples(index=True):
index = row.Index
origin = row._9
row_rate = row.Rating
if origin in data:
data[origin].append(row_rate)
else:
data[origin] = [row_rate]
print(data)
使用带有默认参数的dict.get
或者,我们可以使用 dict.get
及其默认参数;此 dict
方法类似于您的函数 append_rating
:
data = {'Cuba': [999]}
for row in df2.itertuples(index=True):
index = row.Index
origin = row._9
row_rate = row.Rating
data_origin = data.get(origin, [])
data_origin.append(row_rate)
data[origin] = data_origin
print(data)
请注意 data.get(origin, [])
与 append_rating(origin, data)
:
完全等价
data = {'Cuba': [999]}
def append_rating(country, data):
if country in data:
return data[country]
else:
return []
for row in df2.itertuples(index=True):
index = row.Index
origin = row._9
row_rate = row.Rating
data_origin = append_rating(origin, data)
data_origin.append(row_rate)
data[origin] = data_origin
print(data)
注:我觉得函数名append_rating
不合适。 “附加”通常意味着我们正在通过添加一个元素来主动修改某些东西。例如,在 python 中,l.append(x)
通过将元素 x
添加到 l
来修改列表 l
。但是,您的函数 append_rating(origin, data)
不会修改 data
。相反,它在 data
和 returns 中查找条目(如果存在)。这就是为什么最好将其称为 get_rating
或 get_if_exists
或类似名称的原因。但是实际上这个函数已经存在于python,它是dict.get
.
有用的文档
我有一本字典,其中 origin:row_rate 作为键值对。 df2 是一个 pandas 数据框,当我遍历数据框时,我想将 row_rate 附加到它们对应的原始键的值中,如果它们的键已经在字典中的话。否则,我想为原点创建新密钥并保存当前 row_rate 和与该原点密钥对应的所有其他 row_rate。
我试过这个:
data = {'Cuba': 999}
def append_rating(country, data):
if country in data:
return data[country]
for row in df2.itertuples(index=True):
index = row.Index
origin = row._9
row_rate = row.Rating
values = [append_rating(origin, data)]
print(origin, values)
values.append(row_rate)
data[origin] = values
print(data)
这个 returns 这个:
Congo [[[[[[[[[[None, 3.75], 3.5], 3.5], 3.5], 3.75], 3.0], 2.75], 3.25], 3.25]]
还有这个……
def append_rating(country, data):
if country in data:
return data[country]
else:
return []
其中 returns 这个:
Congo [[[[[[[[[[[], 3.75], 3.5], 3.5], 3.5], 3.75], 3.0], 2.75], 3.25], 3.25]]
我要的是列表的所有元素,只驻留在一个列表中。是这样的:
Congo [3.75, 3.5, 3.5, 3.5, 3.75, 3.0, 2.75, 3.25, 3.25]
抱歉这个基本问题。感谢您的帮助:)
我相信您正在寻找 list.extend(other_list)
方法。
https://docs.python.org/3/tutorial/datastructures.html
a = [1, 2, 3]
b = [4, 5, 6]
a.extend(b)
a == [1, 2, 3, 4, 5, 6]
您可以简单地使用 list.extend()
初步评论
首先:你希望data
包含列表,所以最好将data['Cuba']
初始化为单例列表[999]
而不是到 int 999
.
其次: 您将拥有以下逻辑:
If
origin
is already indata
, append todata[origin]
; otherwise, create a new entrydata[origin]
.
当你有这种逻辑时,你可以使用 defaultdict
而不是 dict
。 defaultdict
是dict
的子类,在需要的时候自动创建不存在的条目,并用默认值初始化;在我们的例子中,默认值将是一个空列表。
使用 defaultdict
考虑到这两点,我们可以简化您的代码:
from collections import defaultdict
data = defaultdict(list, {'Cuba': [999]})
for row in df2.itertuples(index=True):
index = row.Index
origin = row._9
row_rate = row.Rating
data[origin].append(row_rate)
print(data)
使用if
/else
如果我们没有使用 defaultdict
,代码会类似但稍微复杂一些:
data = {'Cuba': [999]}
for row in df2.itertuples(index=True):
index = row.Index
origin = row._9
row_rate = row.Rating
if origin in data:
data[origin].append(row_rate)
else:
data[origin] = [row_rate]
print(data)
使用带有默认参数的dict.get
或者,我们可以使用 dict.get
及其默认参数;此 dict
方法类似于您的函数 append_rating
:
data = {'Cuba': [999]}
for row in df2.itertuples(index=True):
index = row.Index
origin = row._9
row_rate = row.Rating
data_origin = data.get(origin, [])
data_origin.append(row_rate)
data[origin] = data_origin
print(data)
请注意 data.get(origin, [])
与 append_rating(origin, data)
:
data = {'Cuba': [999]}
def append_rating(country, data):
if country in data:
return data[country]
else:
return []
for row in df2.itertuples(index=True):
index = row.Index
origin = row._9
row_rate = row.Rating
data_origin = append_rating(origin, data)
data_origin.append(row_rate)
data[origin] = data_origin
print(data)
注:我觉得函数名append_rating
不合适。 “附加”通常意味着我们正在通过添加一个元素来主动修改某些东西。例如,在 python 中,l.append(x)
通过将元素 x
添加到 l
来修改列表 l
。但是,您的函数 append_rating(origin, data)
不会修改 data
。相反,它在 data
和 returns 中查找条目(如果存在)。这就是为什么最好将其称为 get_rating
或 get_if_exists
或类似名称的原因。但是实际上这个函数已经存在于python,它是dict.get
.