- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直在尝试解决一个问题,该问题需要一个返回标题问题的函数。输出的一个示例如下:
sum_of_digits_sorted([56, 2131])
返回
[2131, 56]
自 2+1+3+1
< 大于 5+6
,新列表将按照我们在示例中看到的方式进行排序。
我使用了一个在列表上迭代的代码,并且还编写了一种对数字求和的方法,但我不知道如何将它们应用在一起
PD:我是这个网络的新手,所以我不知道如何附加我的程序和那些东西。谢谢!
最佳答案
你可以试试这个:
In [553]: l
Out[553]: [56, 2131]
In [554]: new = [] # define an empty list
In [555]: for c,i in enumerate(l):
...: if c < len(l)-1:
...: lst = map(int, str(l[c])) # converts individual element into a list
...: lst1 = map(int, str(l[c+1]))
...: if sum(lst) > sum(lst1): # sum(lst) gives the sum of the list
...: new.append(l[c+1])
...: new.append(l[c])
...: else:
...: new.append(l[c])
...: new.append(l[c+1])
...:
...:
In [556]: new # will be a sorted list based on the sum of digits of individual elements.
Out[556]: [2131, 56]
关于python - 按列表中项目的数字之和对数字列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53479508/
这个问题可能类似于In Angular2 *ngFor iteration, how do I output only unique values from the array?但我的问题是还有更多功
我编写了一个算法来获取 float 的总和,该算法对于整数来说非常有效,但是当我应用于 float 时,我得到的总和是负数。但是我的 float 数组只有正 float 。在这里我发布我的代码,感谢您
我想将这个简单的 for 循环转换为并行循环。它遍历字符串数组(从文本文件读取的 float )并计算总和。 for (int i = 0; i { float tmp; if (f
我正在尝试总结日期差异,一切都很好,除了如果有相同日期我想添加 1,例如,如果起始日期是:01/01/2003到目前为止是 01/01/2003 那么我想添加 1 天,但它没有添加 1 天,而是仅在
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: Is JavaScript’s Floating-Point Math Broken? 这将是一个非常基本的计算机科
我刚接触sql,卡住了。我正在尝试计算每个用户走过的(每年)距离总和。我有一个具有以下结构的表(我们称之为 dist_table): rowid user_name date
我刚接触sql,卡住了。我正在尝试计算每个用户走过的(每年)距离总和。我有一个具有以下结构的表(我们称之为 dist_table): rowid user_name date
给定一个正数数组。我想将数组拆分为 2 个不同的子集,以使它们的 gcd(最大公约数)之和最大。 示例数组:{6,7,6,7}。 答案:需要的两个子集是:{6,6}和{7,7};它们各自的 gcd(s
我想在我的数组中求和:
我想将下面的字符串拆分为字母和数字,然后我需要计算数字的总和。我的示例问题是 a[20]={"abcd123dc2"}; 预期输出: abcddc 8 我的代码: int main() { c
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
我是一名优秀的程序员,十分优秀!