我有一个具有这样布局的多维列表
> set.seed(1)
> res <- testOutMat(ncols=5,nrows=5,col_prob = runif(20, 0.1, 0.2))
> res
[,1] [,2]
[1,] 0 0
[2,] 3 1
我需要删除每个项目中第一个索引中名称前后的空格。
我已经尝试过:
[['Company Name ', ['companyemail@email.com']]
['Company Name ', ['companyemail@email.com','companyemail@email.com']]
['Company Name', ['companyemail@email.com']]
['Company Name ', ['companyemail@email.com']]]
但是,当我打印列表时,除了一个字母之外,它什么都不显示
我的预期结果是:
def name_filter(vendors):
unfiltered=vendors
filtered=[]
count=0
for i in unfiltered:
filtered = [x.strip(' ') for x in unfiltered[count][0]]
count+=1
return filtered
答案 0 :(得分:3)
我将递归执行此操作-如果每个元素是字符串,则剥离每个元素;如果它是列表,则重复:
def recursive_strip(lst):
return [(elem.strip() if type(elem) == str else recursive_strip(elem)) for elem in lst]
您可能需要考虑列表可以包含哪些数据类型(以上仅假设字符串或列表)。
答案 1 :(得分:2)
问题是您在每个列表的第一项上使用了for循环,然后去除了空格。结果,您将获得单个字符。您不需要列表理解内的第二个for循环。只需剥离第一个字符串元素并同时附加第二个。附注:我也将嵌套列表vendors
定义为输入,因为我认为这是基于您的代码逻辑的正确输入结构
vendors = [['Company Name ', ['companyemail@email.com']],
['Company Name ',
['companyemail@email.com','companyemail@email.com']],
['Company Name', ['companyemail@email.com']],
['Company Name ', ['companyemail@email.com']]]
def name_filter(vendors):
unfiltered=vendors
filtered=[]
for i in unfiltered:
print (i)
filtered.append([i[0].strip(' '), i[1]]) # <--- This is the key line
return filtered
name_filter(vendors)
# [['Company Name', ['companyemail@email.com']],
# ['Company Name', ['companyemail@email.com', 'companyemail@email.com']],
# ['Company Name', ['companyemail@email.com']],
# ['Company Name', ['companyemail@email.com']]]
答案 2 :(得分:2)
您可以使用str.rstrip()
vendors = \
[['Company Name ', ['companyemail@email.com']],
['Company Name ', ['companyemail@email.com','companyemail@email.com']],
['Company Name', ['companyemail@email.com']],
['Company Name ', ['companyemail@email.com']]]
filtered_vendors = [[vendor[0].rstrip(),vendor[1]] for vendor in vendors]
答案 3 :(得分:1)
您可以在列表理解内完成
vendors = [['Company Name ', ['companyemail@email.com']],
['Company Name ', ['companyemail@email.com','companyemail@email.com']],
['Company Name', ['companyemail@email.com']],
['Company Name ', ['companyemail@email.com']]]
vendors = [ [company[0].strip(),*company[1:]] for company in vendors ]
供应商中的每个项目本身就是一个列表,您需要输出一个列表,其中第一项内容被理解。
答案 4 :(得分:1)
我认为您忘记了逗号
unfiltered = [['Company Name ', ['companyemail@email.com']], ['Company Name ', ['companyemail@email.com','companyemail@email.com']], ['Company Name', ['companyemail@email.com']] ,['Company Name ', ['companyemail@email.com']]]
filtered = []
count=0
text = ''
for i in unfiltered:
print(i[0])
test = str((i[0])).strip()
filtered.append( (str(i)).replace(i[0],test))
print(filtered)
答案 5 :(得分:1)
for (int i = 0; i < response.length(); i++) {
JSONObject cake = response.getJSONObject(i);
String cakeId = cake.optString("id");
String cakeName = cake.optString("name");
JSONArray JSONingredients = cake.optJSONArray("ingredients");
List<Ingredients> ingredients = new List<Ingredients>();
for (int j = 0; j < JSONingredients.length(); j++) {
JSONObject item = JSONingredients.getObject(j);
String measure = item.getString(“measure”);
String ingredient = item.getString(“ingredient”);
Double quantity = item.getDouble(“quantity”);
Ingredients item2 = new Ingredients(measure, ingredient, quantity);
ingredients.add(item2);
}
mCakeList.add(new CakesItem(cakeId,ingredients, cakeName));
}