如何获取每个字符串中的数字总和

发布于 2025-01-27 06:01:46 字数 834 浏览 3 评论 0原文

到目前为止,这就是我所做的:

我已经将Excel数据转换为列表

将所有小时数据列表组合到一个

使用的拆分函数中,以删除使用的数字和使用的数字之间的所有逗号和空格,

以每12个ZEROS将数据组分开以将数据组分开

现在,我试图将所有分开的数字总结到每个字符串中,该怎么做?

这是我的代码:

df = pd.read_excel("2010.xlsx")
df2 = df.to_numpy()
df3 = df2[0:366, 1:25]
list1 = df3.tolist()
list2 = (list(itertools.chain.from_iterable(list1)))
b = ''.join(str(list2).split(','))
c = ''.join(str(b).split(' '))
e = c.split('0'*12)

打印此代码提供类似:

['[','','000000000100111101',',',','000000112111111111110000000000001',',','',','',',',',',',',',',',',',',',',',',',',' ','','','','','','','','','','',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',',', ',','

' '00001121121111111111000000001'等等

This is what I have done so far:

I have converted excel data to list

combined all the hourly data lists into one list

used split function to remove all commas and spaces between the numbers

used split function again to separate group of data by every 12 zeros

Now I'm trying to sum all the numbers that are separated into each string how would I do this?

Here is my code:

df = pd.read_excel("2010.xlsx")
df2 = df.to_numpy()
df3 = df2[0:366, 1:25]
list1 = df3.tolist()
list2 = (list(itertools.chain.from_iterable(list1)))
b = ''.join(str(list2).split(','))
c = ''.join(str(b).split(' '))
e = c.split('0'*12)

printing this code gives something like:

['[', '', '000000000100111101', '', '', '0000112112111101011100000000001', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '0000002433151111000001223221', '', '', '', '', ]

so I'm trying to add all the numbers in '000000000100111101' and '0000112112111101011100000000001' and so on

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橙味迷妹 2025-02-03 06:01:46

不确定这是您想要的,但请尝试此

import numpy as np
e = ['[', '', '000000000100111101', '', '', '0000112112111101011100000000001', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '0000002433151111000001223221', '', '', '', '', ]


tot = np.zeros(len(e))
for idx, item in enumerate(e):
    for i in item:
        if i.isdigit():
            tot[idx] += int(i)

print(tot)

产生此输出

print(tot)
[ 0.  0.  6.  0.  0. 17.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. 35.  0.
  0.  0.  0.]

Not sure this is what you want, but try this

import numpy as np
e = ['[', '', '000000000100111101', '', '', '0000112112111101011100000000001', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '0000002433151111000001223221', '', '', '', '', ]


tot = np.zeros(len(e))
for idx, item in enumerate(e):
    for i in item:
        if i.isdigit():
            tot[idx] += int(i)

print(tot)

producing this output

print(tot)
[ 0.  0.  6.  0.  0. 17.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. 35.  0.
  0.  0.  0.]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文