我根本无法很好地解释这个概念,但我正在尝试使用嵌套循环遍历列表,但我不知道如何使用相同的元素来避免它们。
list = [1, 2, 2, 4]
for i in list:
    for j in list:
        print(i, j) # But only if they are not the same element
Run Code Online (Sandbox Code Playgroud)
所以输出应该是:
1 2
1 2
1 4
2 1
2 2
2 4
2 1
2 2
2 4
4 1
4 2
4 2
Run Code Online (Sandbox Code Playgroud)
编辑,因为解决方案不适用于所有场景:
该if i != j解决方案仅在列表中的所有元素都不同时才有效,我显然选择了一个糟糕的示例,但我的意思是相同的元素而不是相同的数字;我改变了例子
我有以下数据帧:
| 用户身份 | 列_1 | 列_2 | 第 3 列 | 
|---|---|---|---|
| 一种 | 4.959 | 3.231 | 1.2356 | 
| 乙 | 0.632 | 0.963 | 2.4556 | 
| C | 3.234 | 7.445 | 5.3435 | 
| D | 1.454 | 0.343 | 2.2343 | 
我想对上一列的 argsort wrt 列进行排序:
| 用户身份 | 第一的 | 第二 | 第三 | 
|---|---|---|---|
| 一种 | 第 3 列 | 列_2 | 列_1 | 
| 乙 | 列_1 | 列_2 | 第 3 列 | 
| C | 列_1 | 第 3 列 | 列_2 | 
| D | 列_2 | 列_1 | 第 3 列 | 
如何仅删除标点符号而不删除表情符号。我想也许有办法用正则表达式来做到这一点?但不确定。
sentence = ['hello', 'world', '!', '']
def remove_punct(token):
    return [word for word in token if word.isalpha()]
print(remove_punct(sentence))
#output
#['hello', 'world']
#desired output
#['hello', 'world', '']
Run Code Online (Sandbox Code Playgroud) 我有一个字符串列表
X=['kmo','catlin','mept']
Run Code Online (Sandbox Code Playgroud)
我试图编写一个循环,该循环将返回一个列表,其中包含每个单词的每个第 N 个字母的列表:
[['k','c','m'], ['m','a','e'],['o','t','p']]
Run Code Online (Sandbox Code Playgroud)
但是我尝试的所有方法都只返回一个列表,其中包含一个列表中连续返回的所有字母:
['k','m','o','c','a','t','l','i'.....'t'] 
Run Code Online (Sandbox Code Playgroud)
这是我的代码的一个版本:
def letters(X):
    prefix=[]
    for i in X:
      j=0
      while j < len(i):
        while j < len(i):
          prefix.append(i[j])
          break
        j+=1
    return prefix
Run Code Online (Sandbox Code Playgroud)
我知道我在每个单词中循环,但我不知道如何更正它。
我有一个清单:
lst = [('a', 1), ('b', 2), ('c', 3), ('a', 4), ('c', 5)]
Run Code Online (Sandbox Code Playgroud)
我想按元组的第一个元素进行分组并附加第二个元素:
group = {'a': [1, 4], 'b': [2], 'c': [3, 5]}
Run Code Online (Sandbox Code Playgroud)
所以我的代码如下所示:
group = dict()
for e1, e2 in lst:
    if e1 in group:
        group[e1].append(e2)
    else:
        group[e1] = [e2]
Run Code Online (Sandbox Code Playgroud)
我不喜欢这段代码的是,我在组字典中查找一个键两次,一次用于命令e1 in group,两次用于命令group[e1] = ...
如果找到键,是否有更好的方法来保留“指针”并且不必再次查看来设置该键的值?
另外,如果有更好的使用库的解决方案,请告诉我。
我想根据两列 (index_start和index_end) 中的数值扩展我的数据框。我的 df 看起来像这样:
item  index_start  index_end    
A          1            3
B          4            7
Run Code Online (Sandbox Code Playgroud)
我希望它扩展以创建从 1 到 3 的 A 行和从 4 到 7 的 B 行,就像这样。
item  index_start  index_end    index
A          1            3         1
A          1            3         2
A          1            3         3
B          4            7         4
B          4            7         5
B          4            7         6
B          4            7         7
Run Code Online (Sandbox Code Playgroud)
不确定如何在 Python/pandas 中实现这一点。
我有两个collections.defaultdict并试图从中删除d1也在d2.
from collections import Counter, defaultdict
d1 = Counter({'hi': 22, 'bye': 55, 'ok': 33})
d2 = Counter({'hi': 10, 'hello': 233, 'nvm': 96})
Run Code Online (Sandbox Code Playgroud)
理想结果:
d3 = set()
d3 = ({'bye':55, 'ok':33})
Run Code Online (Sandbox Code Playgroud)
到目前为止我已经尝试过:
d3 = set()
d3 = d1 - d2
print(d3)
Counter({'bye': 55, 'ok': 33, 'hi': 12}) 
Run Code Online (Sandbox Code Playgroud)
但这保持了相同的值,'hi'即使我想删除所有相似的值。
std::vector我知道可以默认模板类型参数,但是,当我尝试在不提供模板参数的情况下构造 a 时,这不起作用:
#include<vector>
template <typename Int = int>
struct integer{
    Int i;
};
int main(){
    integer num;
    std::vector<integer> vec;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)
此代码返回类型值不匹配(编译器资源管理器链接)。是否可以在不写的情况下解决这个问题std::vector<integer<int>>
price我有一个数据框 df,需要创建一个新列,它是with (int 之前计算的)的乘积metric。
df['cost'] = df['price'] * metric if (df['status'] == 'online')
df['cost'] = 0 if df['status'] == 'offline'
Run Code Online (Sandbox Code Playgroud) 我有一个元组列表,对应于某些点的 ( x,y)坐标(可以是 8 到数百个点):
mylist = [(x0,y0), (x1,y1), ..., (xn,yn)]
Run Code Online (Sandbox Code Playgroud)
我想获得x和y坐标的最小值和最大值(所有x 的最小值,无论它们,等等)。就是优化比例,把点画成矩形区域。
所以我有两个解决方案:
第一种解决方案:创建两个带有坐标[foo[0] for foo in mylist]且相同的列表foo[1]。然后我可以轻松获得最小值和最大值。但是我必须创建列表(为了不进行两次理解,一次为一分钟,一次为最大值)。
第二种解决方案:对列表进行两次排序,一次根据第一个坐标,然后到第二个坐标,每次获取第一个和最后一个值。内存占用少,但需要排序。
最好的解决方案是什么?
python ×9
dataframe ×3
pandas ×3
list ×2
python-3.x ×2
aggregate ×1
c++ ×1
collections ×1
counter ×1
defaultdict ×1
dictionary ×1
group-by ×1
key ×1
loops ×1
sorting ×1
string ×1
tuples ×1