简体   繁体   English

如何在列表列表中添加特定元素(即字符串)?

[英]How to add specific elements (that are strings) in a List of Lists?

This is my List of Lists.这是我的列表列表。 Notice that the elements are strings:请注意,元素是字符串:

BTC_USDT = [['16941.19', '0.000796'], ['16941.98', '0.205845'], ['16941.99', '0.011509'], ['16942.34', '0.043801'],
            ['16942.35', '0.253870'], ['16942.36', '0.572059'], ['16942.41', '0.129081'], ['16942.42', '0.017000'],
            ['16942.43', '0.003698'], ['16942.44', '0.048544'], ['16942.52', '0.005100'], ['16942.53', '0.017272'],
            ['16942.56', '0.010200'], ['16942.57', '0.010268'], ['16942.58', '0.010192'], ['16942.59', '0.001071'],
            ['16942.60', '0.011390'], ['16942.64', '0.010115'], ['16942.67', '0.032785'], ['16942.68', '0.062645']]

I'm trying to output a sum all the 2-nd elements.我正在尝试 output 对所有第二个元素求和。 And I've come this far:我已经走了这么远:

for i in range(len(BTC_USDT)):
    ob_quantities = BTC_USDT[i][1]
    print(ob_quantities)

This will print the sum of all the 2nd elements:这将打印所有第二个元素的总和:

total_sum = 0
for element in BTC_USDT:
    total_sum += float(element[1])
print(total_sum)

or, to put it another way:或者,换句话说:

total_sum = sum([float(element[1]) for element in BTC_USDT])
print(total_sum)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM