假设我有一个这样的数组:
$people = array(
array(
'name' => 'Bob',
'job' => 'Carpenter'
),
array(
'name' => 'George',
'job' => 'Programmer'
),
array(
'name' => 'Clint',
'job' => 'Actor'
)
);
Run Code Online (Sandbox Code Playgroud)
我想知道所有这些人的名字.
我知道我可以这样做:
$names = array();
foreach($people as $person)
$names[] = $person['name'];
Run Code Online (Sandbox Code Playgroud)
但是,让我说我很懒,所以我为它创建了一个函数:
/**
* Returns an Array of Values paired with the specified $key
* in the $array of Associative Arras.
*
* @param array $array The Array of Associative Arrays.
* @param string $key The Key within each Associative Array …
Run Code Online (Sandbox Code Playgroud) 这是我的清单。
[('a', 12), ('c', 4), ('b', 3), ('e', 6), ('d', 5), ('g', 50), ('f', 30),]
Run Code Online (Sandbox Code Playgroud)
对该列表进行排序的结果将是。
[('g', 50), ('f', 30), ('a', 12), ('e', 6), ('d', 5), ('c', 4), ('b', 3)]
Run Code Online (Sandbox Code Playgroud)
我尝试使用:
x = sorted(alpha_items, key=lambda x: x[1],)
Run Code Online (Sandbox Code Playgroud)
但是我需要扭转它。
我可以添加另一个密钥吗?
我试图在歌词典中搜索关键词.它们的键是歌曲标题,值是歌曲的长度.我想在字典中搜索这首歌,然后打印出那首歌和它的时间.我已经想出要搜索这首歌,但是不记得如何突出它的价值.这是我现在拥有的.
def getSongTime(songDictionary):
requestedSong=input("Enter song from playlist: ")
for song in list(songDictionary.keys()):
if requestedSong in songDictionary.keys():
print(requestedSong,value)
Run Code Online (Sandbox Code Playgroud) 以下两个"开关"工作:
return {
1: func1,
2: func2,
3: func3
}[random.choice([1,2,3])]()
d = {
1: func1,
2: func2,
3: func3
}
return d[random.choice(d.keys())]()
Run Code Online (Sandbox Code Playgroud)
它们实际上是一样的.
现在我想做同样的事情,但没有将字典分配给变量 - 这可能吗?我知道_
运营商会做类似的事情,所以我想知道这是否也可以做到.
return {
1: func1,
2: func2,
3: func3
}[random.choice(???.keys())]()
Run Code Online (Sandbox Code Playgroud)
我只是出于好奇而问.
我有一个哈希数组,在数组中有不同的键:
csv = [{:fruit=>"apple", :number=>23},{:age=>12,:name=>"XYZ"}]
Run Code Online (Sandbox Code Playgroud)
我如何得到这样的所有键:
[:fruit,:number,:age,:name]
Run Code Online (Sandbox Code Playgroud)
我试过了
array = csv.collect {|key,value| key }
Run Code Online (Sandbox Code Playgroud) 我是Python的新手,所以请原谅我这是一个非常基本的问题.我有一个像这样的Python字典:
foo = {'bar1':['a','b','c'], 'bar2':['d','e']}
我想把它写成一个csv文件,每个值一行,键作为每行的第一个元素.输出将是(无论引号):
bar1,'a'
bar1,'b'
bar1,'c'
bar2,'d'
bar2,'e'
Run Code Online (Sandbox Code Playgroud)
我试过这样的建议
import csv
with open('blah.csv', 'wb') as csv_file:
writer = csv.writer(csv_file)
for key, value in foo.items():
writer.writerow([key, value])
Run Code Online (Sandbox Code Playgroud)
但这会给出以下输出,每个键一行:
bar1,"['a', 'b', 'c']"
bar2,"['d', 'e']"
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
original = ["aga", "aaa", "aba"]
dict = {
"aba": 1,
"aaa": 0,
"aga": 1
}
Run Code Online (Sandbox Code Playgroud)
我需要按dict值排序,并且断路器需要保持原始顺序,我该怎么做?示例非常简化.
我试过了:
final = sorted(sorted(original, key=lambda x: (dict[x]), key=original.index))
Run Code Online (Sandbox Code Playgroud) 我可以使用jq
as 获得顶级JSON密钥
jq 'keys'
Run Code Online (Sandbox Code Playgroud)
例如,看到以下问题:使用jq从JSON文件获取键名
我可以在中启用递归jq
:
jq '..'
Run Code Online (Sandbox Code Playgroud)
如:按键递归搜索值
但是jq '.. | keys'
回报jq: error at <stdin> string has no keys
。
我有3个数组,它们的值来自之前执行的循环.结果如下:
1.当我打印_r时变量
Array
(
[0] = some title
[1] = some title
[2] = some title
[3] = some title
)
Run Code Online (Sandbox Code Playgroud)
2.当我print_r时变量
Array
(
[0] = some id
[1] = some id
[2] = some id
[3] = some id
)
Run Code Online (Sandbox Code Playgroud)
3.当我print_r时变量
Array
(
[0] = some image
[1] = some image
[2] = some image
[3] = some image
)
Run Code Online (Sandbox Code Playgroud)
现在我希望每个数组都有自己的密钥,所以我可以将它们组合起来并得到这个结果:
结果
Array
(
[0]=> Array
(
[Title] = title
[Id] = id
[Image] = image
)
[1]=> Array …
Run Code Online (Sandbox Code Playgroud) 我的数组是这样的:
$arr = array('chelsea.jpg', 'arsenal.jpg');
Run Code Online (Sandbox Code Playgroud)
如果我跑: echo '<pre>';print_r($arr);echo '</pre>';
结果 :
Array
(
[0] => chelsea.jpg
[1] => arsenal.jpg
)
Run Code Online (Sandbox Code Playgroud)
我想改变关键.所以结果是这样的:
Array
(
[cover1] => chelsea.jpg
[cover2] => arsenal.jpg
)
Run Code Online (Sandbox Code Playgroud)
我该怎么做?