标签: append

在python中通过if条件追加列表

这可能是一个非常基本的问题,但我意识到我不明白一些事情。

在 for 循环中追加新内容时,如何提出条件并仍然追加该项目?

例如:

alist = [0,1,2,3,4,5]
new = []
for n in alist:
    if n == 5:
        continue
    else:
        new.append(n+1)

print(new) 
Run Code Online (Sandbox Code Playgroud)

让我明白

[1, 2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)

如何得到

[1, 2, 3, 4, 5, 5] # 4 is incremented, 5 is added 'as is'
Run Code Online (Sandbox Code Playgroud)

本质上,我想告诉 python 不要经历n+1when n==5。

这是唯一的解决方案吗?将 n==5 单独附加到列表中,然后将新的和单独的列表相加?

python for-loop list append python-3.x

1
推荐指数
1
解决办法
3万
查看次数

使用值接收器附加到具有足够容量的切片

有人可以帮助我理解这里发生了什么吗?

package main

import (
    "fmt"
)

func appendString(slice []string, newString string) {
    slice = append(slice, newString)
}

func main() {
    slice := make([]string, 0, 1)
    appendString(slice, "a")
    fmt.Println(slice)
}
Run Code Online (Sandbox Code Playgroud)

我了解切片头以及使用指针接收器的需要。但在这里,由于底层数组有足够的容量,我希望追加能够正常工作(只需将新值添加到底层数组中,原始的[复制]标头按预期工作)

我的假设有什么问题吗?

pointers append go slice

1
推荐指数
1
解决办法
60
查看次数

仅附加表查询的 Ecto JOIN 复杂性

我正在尝试使用仅附加语义查询 Ecto 表,因此我想要给定 ID 的完整行的最新版本。这里描述了该技术,但简而言之:我想使用一个获取 ID 的最新时间的子查询来连接一个表本身。在 SQL 中,这看起来像:

SELECT r.*
FROM rules AS r
JOIN (
  SELECT id, MAX(inserted_at) AS inserted_at FROM rules GROUP BY id
) AS recent_rules
ON (
  recent_rules.id = r.id
  AND recent_rules.inserted_at = r.inserted_at)
Run Code Online (Sandbox Code Playgroud)

我在 Ecto 中很难表达这一点。我尝试过这样的事情:

maxes =
  from(m in Rule,
    select: {m.id, max(m.inserted_at)},
    group_by: m.id)

from(r in Rule,
  join: m in ^maxes, on: r.id == m.id and r.inserted_at == m.inserted_at)
Run Code Online (Sandbox Code Playgroud)

但尝试运行它时,我遇到了限制:

连接中的查询只能有where查询中的条件

建议maxes一定只是一种SELECT _ FROM …

append elixir ecto phoenix-framework

1
推荐指数
1
解决办法
500
查看次数

在 python 中打开或创建文件并附加到它

你如何在Python中执行这一系列操作?

1)如果文件不存在则创建一个文件并插入字符串

2)如果文件存在,则查找是否包含字符串

3)如果字符串不存在,则挂在文件末尾

我目前正在这样做,但我错过了一步

每次我调用该函数时都使用此代码进行编辑似乎该文件不存在并覆盖旧文件

def func():
if not os.path.exists(path):
    #always take this branch
    with open(path, "w") as myfile:
        myfile.write(string)
        myfile.flush()
        myfile.close()
else:
    with open(path) as f:
        if string in f.read():
            print("string found")
        else:
            with open(path, "a") as f1:
                f1.write(string)
                f1.flush()
                f1.close()
    f.close()
Run Code Online (Sandbox Code Playgroud)

python file append

1
推荐指数
1
解决办法
5862
查看次数

有条件附加到 Elixir 中的列表?

在使用 Python 几个月后,我正在编写 Elixir 代码,但我对惯用的 Elixir 的记忆已经模糊了。

这段代码的工作原理:

# Define workers and child supervisors to be supervised
children = [
  # Start the Ecto repository
  supervisor(Ssauction.Repo, []),
  # Start the endpoint when the application starts
  supervisor(SsauctionWeb.Endpoint, []),

  supervisor(Absinthe.Subscription, [SsauctionWeb.Endpoint]),
]

children
= if System.get_env("PERIODIC_CHECK") == "ON" do
    Enum.concat(children, [worker(Ssauction.PeriodicCheck, [])])
  else
    children
  end
Run Code Online (Sandbox Code Playgroud)

但我确信这很尴尬。如何用惯用的方式重写这个?

list append elixir

1
推荐指数
1
解决办法
1940
查看次数

从 R 表在 Excel 工作簿中创建多个工作表?[使用xlsx包]

我按照这些说明进行操作,然后按照这些说明进行操作:

# Write the first data set in a new workbook
write.xlsx(USArrests, file="myworkbook.xlsx",
      sheetName="USA-ARRESTS", append=FALSE)
# Add a second data set in a new worksheet
write.xlsx(mtcars, file="myworkbook.xlsx", sheetName="MTCARS", 
           append=TRUE)
# Add a third data set
write.xlsx(Titanic, file="myworkbook.xlsx", sheetName="TITANIC", 
           append=TRUE)
Run Code Online (Sandbox Code Playgroud)

这是我最后所做的:

openxlsx::write.xlsx(Cost_Changes_0021_Table,'C:/Users/santi/Documents/Cost Changes xlsx/0021_Cost_Changes.xlsx', sheetName ='Dept 0021 Prod Level', row.names= FALSE, append = FALSE)
openxlsx::write.xlsx(Cost_Changes_0021_Vendor_Lvl_Table,'C:/Users/santi/Documents/Cost Changes xlsx/0021_Cost_Changes.xlsx', sheetName ='Dept 0021 Vendor Level', append= TRUE, row.names= FALSE)
Run Code Online (Sandbox Code Playgroud)

我不明白为什么运行该脚本后没有得到多张纸。我的代码第二行中发生的情况会覆盖第一张工作表,然后当我真正需要工作簿中的两个选项卡时我只看到一个选项卡 C:/Users/santi/Documents/Cost Changes xlsx/0021_Cost_Changes.xlsx

r append xlsx

1
推荐指数
1
解决办法
9505
查看次数

迭代列表列表,维护列表结构

假设我有以下名单:

names = [['Matt', 'Matt', 'Paul'], ['Matt']]
Run Code Online (Sandbox Code Playgroud)

我只想返回列表中的“Matts”,但我也想维护列表结构的列表。所以我想返回:

[['Matt', 'Matt'], ['Matt']]
Run Code Online (Sandbox Code Playgroud)

我有这样的东西,但这会将所有内容一起附加到一个大列表中:

matts = [name for namelist in names for name in namelist if name=="Matt"]
Run Code Online (Sandbox Code Playgroud)

我知道这样的事情是可能的,但我想避免迭代列表和附加。这可能吗?

names = [['Matt', 'Matt', 'Paul'], ['Matt']]
matts = []
for namelist in names:
    matts_namelist = []
    for name in namelist:
        if name=="Matt":
            matts_namelist.append(name)
        else:
            pass
    matts.append(matts_namelist)
        
Run Code Online (Sandbox Code Playgroud)

python loops list append

1
推荐指数
1
解决办法
719
查看次数

Sed 模式匹配然后附加到行

我在下面有一些行,我试图将“Check”附加到以 Apples 开头的行中。有人知道我如何才能在与苹果同一行而不是新的行上获得“检查”并打印输出吗?我靠自己一事无成。

谢谢

我拥有的:

Grocery store bank and hardware store
Apples Bananas Milk
Run Code Online (Sandbox Code Playgroud)

我想要的是:

Grocery store bank and hardware store
Apples Bananas Milk Check
Run Code Online (Sandbox Code Playgroud)

我尝试过的:

sed -i '/^Apples/a Check' file
Run Code Online (Sandbox Code Playgroud)

我得到了什么:

Grocery store bank and hardware store
Apples Bananas Milk
Check
Run Code Online (Sandbox Code Playgroud)

sed append

1
推荐指数
1
解决办法
3930
查看次数

分割字符串并将其附加到向量

我有一个字符向量,其中字符串元素有或没有斜杠(/)。

sem_tags <- ('F2', 'A1/B1', 'X3.2', 'M3fn', 'E2/Q2')

我希望那些带有斜杠的标签存储为两个单独的标签,以便最终我得到

new_sem_tags <- ('F2', 'A1', 'B1', 'X3.2', 'M3fn', 'E2', 'Q2')

这是我到目前为止所尝试的。

new_sem_tags <- c()
for (i in 1:length(sem_tags)){
  if (grepl("/", i)){
    append(new_sem_tags, str_split(i, '/'))
  } else {
    appendnew_sem_tags, i)
  }
  i = i+1
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,这不起作用。当我单独尝试每个功能时,例如,拆分和附加,它们似乎可以完成工作。然而,我注意到附加返回输出但不“存储”输出。这可能是问题所在吗?如果是,我该如何克服?

提前致谢!

string split r append

1
推荐指数
1
解决办法
838
查看次数

PowerShell 将对象附加到变量列表

对 PowerShell 相当陌生,想要了解如何将对象附加到变量列表。以下是错误消息:

Method invocation failed because [System.IO.FileInfo] does not contain a method named 'op_Addition'.
At C:\Users\Username\Desktop\Sandbox\New folder\BoxProjectFiles.ps1:117 char:4
+             $MechDWGFile += $file
+             ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
Run Code Online (Sandbox Code Playgroud)

和代码:

LogWrite "`n-------------Mechanical Drawing(s)------------"
foreach ($file in $MechDWGList)
{
    # Where the file name contains one of these filters
    foreach($filter in $MechDWGFilterList)
    {
        if($file.Name -like $filter)
        {
            $MechDWGFile += $file # this is where the error is happening, so I know it is probably …
Run Code Online (Sandbox Code Playgroud)

variables powershell append

1
推荐指数
1
解决办法
1万
查看次数

标签 统计

append ×10

list ×3

python ×3

elixir ×2

r ×2

ecto ×1

file ×1

for-loop ×1

go ×1

loops ×1

phoenix-framework ×1

pointers ×1

powershell ×1

python-3.x ×1

sed ×1

slice ×1

split ×1

string ×1

variables ×1

xlsx ×1