例如,假设我有
var label = document.createElement("label");
label.appendChild(<Checkbox />);
如何从其中一个获取单个值?
例如,如果我只想要“ a”而不是1,则不能:
tuple_list = [('a', 1), ('b', 2), ('c', 3)]
因为这样会返回:
tuple_list[0]
所以我的问题是,如何使它只返回'a'或'b'或1或2?
答案 0 :(得分:1)
以与列表相同的方式索引多组(它们的区别在于列表是可变的)。因此,如果您有一个元组列表,则可以像访问列表中的元素一样访问单个元素。
例如,
// I want to change the tempo for bgm audio file dynamically
self.timePitch = AKTimePitch(self.bgmPlayer)
// here I set the initialized rate value to time Pitch
self.timePitch.rate = 1.0
// support iOS10+
self.out = AKOfflineRenderNode()
self.timePitch.connect(to: self.out)
// make the renderer as AudioKit.out
AudioKit.output = self.out
do {
try AudioKit.start()
} catch {
debugPrint(error.localizedDescription)
}
let url = URL(fileURLWithPath: NSTemporaryDirectory() + "output.caf")
// get total duration
let duration = self.duration()
DispatchQueue.global(qos: .background).async {
do {
let avAudioTime = AVAudioTime(sampleTime: 0, atRate:self.out.avAudioNode.inputFormat(forBus: 0).sampleRate)
// start play BGM
self.bgmPlayer.play(at: avAudioTime)
// and render it to an offline file
try self.out?.renderToURL(url, duration: duration)
// **********
// Question:
// Can I change the tempo value when rendering?
// **********
// stop when finished
self.bgmPlayer.stop()
} catch {
debugPrint(error)
}
}
因此,如果您需要一个元组的第>> x = [('a', 0), ('b', 1)]
>> x
[('a', 0), ('b', 1)]
>> type(x)
<class 'list'>
>> type(x[0])
<class 'tuple'>
>> type((x[0])[0]) # which is equivalent to
<class 'str'>
>> type(x[0][0])
<class 'str'>
>> x[0][0]
'a'
个元素,它是列表i
的第j
个元素,则可以使用x
来访问它。
答案 1 :(得分:0)
代码
tuple_list = [('a', 1), ('b', 2), ('c',3)]
print(tuple_list)
print("Tuple [0][0] = ", end = ' ')
print(tuple_list[0][0])
print("Tuple [0][1] = ", end = ' ')
print(tuple_list[0][1])
print("Tuple [1][0] = ", end = ' ')
print(tuple_list[1][0])
print("Tuple [1][1] = ", end = ' ')
print(tuple_list[1][1])
print("Tuple [2][0] = ", end = ' ')
print(tuple_list[2][0])
print("Tuple [2][1] = ", end = ' ')
print(tuple_list[2][1])
输出
(xenial)vash@localhost:~/python/LPTHW$ python3.7 compare.py
[('a', 1), ('b', 2), ('c', 3)]
Tuple [0][0] = a
Tuple [0][1] = 1
Tuple [1][0] = b
Tuple [1][1] = 2
Tuple [2][0] = c
Tuple [2][1] = 3
这应该使您对如何到达每个项目有一个很好的视觉表示,几乎可以像这样思考它:
lista = (0, 1, 2)
lista =([a, b], [c, d],[e, f])
想象一下,现在我们已经了解了list[0] = 0
,这两个重叠,list[0]
有两个子组件list[0][0]
= a
和list[0][1]
= b
代表items
中的第一个list[0]
和第二个list[0]
。并且(a,b)
将代表{{1}}。
此帮助吗?