下面的代码片段以递归方式将嵌套列表和元素展平为新列表,但无法将所有元素附加到列表中.
预期产量:[1,2,4,5,6,7,5,8]
我的输出:[1,2,8]
def foo(l):
result = []
for i in l:
if type(i)==list:
foo(i)
else:
result.append(i)
return result
input_list = [1,2,[4,5,[6,7],5],8]
print (foo(input_list))
Run Code Online (Sandbox Code Playgroud) 我想知道如何使用嵌套for循环来提高此函数的可读性.也许我可以用nested for loop的tags_files =?
def search_repotag_for_file(search_filepath, repo):
'''Goes through all tags, all files to find a github file entity
which matches the search_filepath we are looking for'''
all_tags = (tag for tag in repo.tags)
tags_files = ((tag, file_ent) for tag in all_tags for file_ent in tag.commit.tree.traverse())
matches = (tup for tup in tags_files if tup[1].path == search_filepath)
return matches
Run Code Online (Sandbox Code Playgroud) 如果这是我的目标:
var obj = {
bakery1: {
small: {
name: "Small cookie",
price: 0.75;
}
large: {
name: "Large cookie",
price: 3.00;
}
}
bakery2: {
small: {
name: "Small cookie",
price: 1.00;
}
large: {
name: "Large cookie",
price: 4.00;
}
}
};
Run Code Online (Sandbox Code Playgroud)
我将如何制作一个将所有价格打印到控制台的循环?
并且在打印此值时,是否有办法跟踪名称?
例如,如果输出为3.00,我将如何创建一个函数,该函数的名称与3.00价格一致?
提前致谢!
我试图通过toSearchableArray向Algolia发送一些数据.我存储在我的数据库中的任何字符串都是正常发送的,但是当我尝试推送嵌套的JSON数据时,我遇到了障碍 - 信息被发送为带有字符转义字符串的字符串.
这是我存储在我的表中的嵌套对象的示例(具有JSON数据类型的MySQL):
[
{
"id": 19,
"name": "Mathematics",
"short": "Math"
},
{
"id": 23,
"name": "Science",
"short": "Science"
},
{
"id": 14,
"name": "Health and Life Skills",
"short": "Health"
}
]
Run Code Online (Sandbox Code Playgroud)
我的模型看起来像这样:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Resource extends Model
{
use Searchable;
protected $primaryKey = 'objectID';
public function toSearchableArray()
{
$data = $this->toArray();
$data['grades'] = explode(';', $data['grades']);
$data['units'] = explode(';', $data['units']);
return $data;
}
}
Run Code Online (Sandbox Code Playgroud)
我得到一个如下所示的输出:
array:22 [
"objectID" => 1
"name" => "Resource …Run Code Online (Sandbox Code Playgroud) 任何人都可以向我解释这个Scheme中的表达式如何返回100?
(((lambda (f) ((lambda (g) (lambda (h) (f (g (h 4))))) double)) square) inc)
Run Code Online (Sandbox Code Playgroud)
我知道它以某种方式分解为"(2*(4 + 1))^ 2",但对于我的生活,我无法弄清楚如何.
这是针对类似问题的考试.其中大约有6或7个,我们必须在大约1或2分钟内找到答案(因为它们只是测试的一部分).除了努力学习之外,我们的教授没有提供任何帮助,但我完全不知道如何做到这些,更不用说快速了.
任何帮助将不胜感激!谢谢.
我正在用Java构建一个基本的战舰式游戏,并使用嵌套列表来表示游戏网格.但是,当我试图放下一艘船时,我一直得到IndexOutOfBoundsException.
游戏板具有如下构造函数
public Board(){
theSea = new ArrayList<ArrayList<ShipInterface>>(10);
for(int i = 0; i < theSea.size(); i++){
theSea.set(i, new ArrayList<ShipInterface>(10));
}
}
Run Code Online (Sandbox Code Playgroud)
放置船舶的方法如下:
public void placeShip(ShipInterface ship, Position position, boolean isVertical) throws InvalidPositionException, ShipOverlapException{
for(int i=0; i<ship.getSize(); i++){
theSea.get((position.getX()-1) + i).set(position.getY()-1, ship);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到了错误 theSea.get((position.getX()-1) + i).set(position.getY()-1, ship);
我是初学者,如果我错过了一些明显的代码,我很抱歉!
这里有点新的Python.
我目前有一个嵌套列表列表.我试图从0-25开始标记每个子列表.但是,如果两个子列表相同,则它们应具有相同的标签.
例如:
label_list = [['AH0'], ['AA1', 'K', 'S'], ['AH0', 'N', 'T'], ['AA1', 'K', 'S'], ['IH0', 'N'], ['AA1', 'K', 'S']]
Run Code Online (Sandbox Code Playgroud)
输出应该是 [0, 1, 2, 1, 4, 1]
因为第二,第四和第六个子列表是相同的.其余的子列表应该以连续的数字顺序标记.我知道我需要使用一个循环,但我很困惑,任何人都有任何建议如何处理这个?谢谢.
$json_string = '{
"response_code": 200,
"info": {
"days": [
{
"code": "A",
"runs": "111"
},
{
"code": "B",
"runs": "222"
},
{
"code": "C",
"runs": "333"
}
],
"name": "SUPER MARIO",
"number": "010203",
"classes": [
{
"points": "6523",
"name": "ABC",
"available": "N"
},
{
"points": "4253",
"name": "XYZ",
"available": "N"
},
{
"points": "2323",
"name": "JOHN",
"available": "N"
},
{
"points": "5236",
"name": "TAMIL",
"available": "N"
}
]
}
}';
$jsondata = $json_string;
$arr = json_decode($jsondata, true);
foreach($arr as $k=>$v) …Run Code Online (Sandbox Code Playgroud) 如果我有这样的列表:
L = [
['a', 'b'],
['c', 'f'],
['d', 'e']
]
Run Code Online (Sandbox Code Playgroud)
我知道我可以'f'通过any以下方式检查是否包含在任何子列表中:
if any('f' in sublist for sublist in L) # True
Run Code Online (Sandbox Code Playgroud)
但是,我将如何搜索第二个子列表,即列表是否按以下方式初始化:
L = [
[
['a', 'b'],
['c', 'f'],
['d', 'e']
],
[
['z', 'i', 'l'],
['k']
]
]
Run Code Online (Sandbox Code Playgroud)
我尝试将for in这样的表达式链接起来:
if any('f' in second_sublist for second_sublist in sublist for sublist in L)
Run Code Online (Sandbox Code Playgroud)
但是,这会崩溃,因为name 'sublist' is not defined.
我想写一个函数,它将返回一个嵌套的defaultdict,具体取决于输入n的值.
对于n = 1,它应该返回defaultdict(int).
对于n = 3,它应该返回
defaultdict(lambda: defaultdict(lambda: defaultdict(int))).
我最好的尝试是这样的:
def get_nested_defaultdict(n: int):
bottom_dict = defaultdict(int)
nested_dict = defaultdict
for i in range(n):
nested_dict = nested_dict(lambda: bottom_dict)
return nested_dict
Run Code Online (Sandbox Code Playgroud)
但这在第二次迭代时失败了:
TypeError: 'collections.defaultdict' object is not callable