我正在尝试使用内置函数 enumerate() 来标记一些点或顶点,其中每个点由其在元组列表(或组)中的坐标表示,基本上看起来像 {(4,5), (6,8), (1,2)}
我想将一个从“a”开始的字母按升序分配给这个集合中的每个元组,使用方法enumerate()完全相同,但它的编写方式是返回每个项目的索引值,因此它是一个从0.
除了自己写,还有什么办法enumerate()吗?
我发现这段代码无处不在,用于复制列表或克隆列表。
随处可见的代码:
clone([],[]).
clone([H|T],[H|Z]):- clone(T,Z).
?-clone([1,2,3],Z).
  Z=[1,2,3]
?-clone(2,Z).
  false
Run Code Online (Sandbox Code Playgroud)
这不会复制除lists.以外的任何内容。上述代码的时间复杂度为O(n).
但是 Prolog 试图统一右侧和提升侧,对吗?这可以用更简单的方式编写,对吗?
喜欢clone1(Z,Z).:
clone1(Z,Z).
?-clone1([1,2,3],Z).
  Z=[1,2,3]
?-clone1(1,Z).
  Z=1
?-clone1(!,Z).
  Z =!
?-clone1(@,Z).
  Z=(@)
Run Code Online (Sandbox Code Playgroud)
我觉得clone1(X, X).它更通用,几乎克隆了传递给它的所有内容。它没有克隆%, (, ), ()。clone1(%,Z)失败的消息% - used for commenting。时间复杂度clone1是O(1)我可能是错的。在各个方面,clone1都比clone.
为什么这个克隆/副本不是这样写的,即clone(X, X).我错过了什么?请向我解释我上面提供的两个代码之间的区别。如果两者都做同样的为什么clone1(X, X).不使用并且没有人发布过它。
我已经看到许多解决方案使用,re.split但它并没有解决我的问题。我希望能够拆分我的字符串并将某些字符保留在列表中......很难解释,但这里有一个例子:
文本:
'print("hello world");'
Run Code Online (Sandbox Code Playgroud)
我想要的结果:
["print", "(", "\"", "hello", "world", "\"", ")", ";"]
Run Code Online (Sandbox Code Playgroud)
像 re.split 这样的事情会给我:
["print", "hello", "world"]
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到想要的结果?
为什么 S1 和 S2 在撇号位置方面表现不同?
S1="1/282/03/10"
S2="4/107/03/10"
R1="".join({"N\'" ,S1,"\'" })
R2="".join({"N\'" ,S2,"\'" })
Run Code Online (Sandbox Code Playgroud)
输出:
"N''1/282/03/10"    //R1 
"N'4/107/03/10'"    //R2
Run Code Online (Sandbox Code Playgroud) import os
import sys
pid = os.fork()
print ("second test")
if pid == 0:
    print ("this is the child")
    print ("I'm going to exec another program now")
    os.execl("python", "test.py", * sys.argv)
else:
    print ("the child is pid %d" % pid)
    os.wait()
Run Code Online (Sandbox Code Playgroud)
我到处查看示例,但对于我的生活,我根本无法理解。我认为这会起作用,但我收到此错误:
Traceback (most recent call last):
  File "main.py", line 9, in <module>
    os.execl("python", "test.py", * sys.argv)
  File "/usr/local/lib/python3.8/os.py", line 534, in execl
    execv(file, args)
FileNotFoundError: [Errno 2] No such file or directory
Run Code Online (Sandbox Code Playgroud) 我有一个字符串:
str = '[\'RED\', \'GREEN\', \'BLUE\']'
Run Code Online (Sandbox Code Playgroud)
我想将其解析为
list = ['RED','GREEN','BLUE']
Run Code Online (Sandbox Code Playgroud)
但是,我无法这样做。
我尝试使用 json.loads 解析:
json.loads(str)
Run Code Online (Sandbox Code Playgroud)
它给了我:
{JSONDecodeError}Expecting value: line 1 column 2 (char 1)
Run Code Online (Sandbox Code Playgroud) 我有:
df = pd.DataFrame(
    {
     "A": [["a", "b", "c"], ["d"]], 
     "B": [[1, 2, 3], [4]], 
     "C": [["abc"], ["def"]]
    }
)
     A          B           C
0   [a, b, c]   [1, 2, 3]   [abc]
1   [d]         [4]         [def]
Run Code Online (Sandbox Code Playgroud)
我的预期输出是:
   A  B  C
0  a  1  abc
1  b  2  abc
2  c  3  abc
3  d  4  def
Run Code Online (Sandbox Code Playgroud)
我试过了
df = df.explode("A")
df = df.explode("B")
Run Code Online (Sandbox Code Playgroud)
但它创建了一个“组合产品”并保留了索引。
有一个列表,如:
my_list = [
  [{'score':9, 'name':'Jack'}],
  [{'score':3, 'name':'Danielle'}]
]
Run Code Online (Sandbox Code Playgroud)
我正在尝试遍历此列表,但无法弄清楚如何访问这些值。
for listing in my_list:
  print(listing['score'])
Run Code Online (Sandbox Code Playgroud)
以上是行不通的。我的理解是因为我似乎正在编写仍在第二个列表中的字典。但是,我无法找到获取访问权限的正确方法。
我有一个带有 Flag1 列的数据框,我想检查列标志值 1 是否连续出现最多次数
这是数据帧和输出格式
df = pd.DataFrame({'flag':[1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1]}) 
df_out = pd.DataFrame({'max_count':[3]})
Run Code Online (Sandbox Code Playgroud) 我有一个 Python 数据框和一个字典,如下所示,我需要根据字典过滤数据框。如您所见,字典的键和值是数据帧的两列。我想要一个数据帧的子集,其中包含字典的键和值以及其他列。
df:
| 客户ID | 类别 | 类型 | 送货 | 
|---|---|---|---|
| 40275 | 书 | 买 | 真的 | 
| 40275 | 软件 | 卖 | 错误的 | 
| 40275 | 电子游戏 | 卖 | 错误的 | 
| 40275 | 手机 | 卖 | 错误的 | 
| 39900 | 光盘/DVD | 卖 | 真的 | 
| 39900 | 书 | 买 | 真的 | 
| 39900 | 软件 | 卖 | 真的 | 
| 35886 | 手机 | 卖 | 错误的 | 
| 35886 | 电子游戏 | 买 | 错误的 | 
| 35886 | 光盘/DVD | 卖 | 错误的 | 
| 35886 | 软件 | 卖 | 错误的 | 
| 40350 | 软件 | 卖 | 真的 | 
| 28129 | 软件 | 买 | 错误的 | 
字典是:
d = {
 40275: ['Book','Software'],
 39900: ['Book'],
 35886: ['Software'],
 40350: ['Software'],
 28129: ['Software']
 }
Run Code Online (Sandbox Code Playgroud)
我需要以下数据框:
| 客户ID | 类别 | 类型 | 送货 … | 
|---|