小编Roa*_*ner的帖子

同等对齐字符串

我试图将这些字符串与它们各自的索引对齐.字符串看起来像这样:

char *mystrings[] = {"Fred", "Augustine", "Bob", "Lenny", "Ricardo"};
Run Code Online (Sandbox Code Playgroud)

我的输出如下:

Fred 0
Augustine 1
Bob 2
Lenny 3
Ricardo 4
Run Code Online (Sandbox Code Playgroud)

但我追求的是这样的事情:

Fred       0
Augustine  1
Bob        2
Lenny      3
Ricardo    4
Run Code Online (Sandbox Code Playgroud)

因此索引对齐相同.我只是想让它更具可读性.这是我的代码到目前为止的样子:

#include <stdio.h>
#include <stdlib.h>

int
main(void) {
    int i;
    char *mystrings[] = {"Fred", "Augustine", "Bob", "Lenny", "Ricardo"};

    for (i = 0; i < 5; i++) {
        printf("%s %d\n", mystrings[i], i);
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

有没有办法可以做到这一点?比较索引与最长的字符串,然后像这样的整数空格?

c

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

subprocess.Popen 在 WSL Linux 上花费太长时间

我有这个subprocess.Popen()上下文管理器:

with Popen(
    args=command, shell=False, stdout=PIPE, bufsize=1, universal_newlines=True
) as process:

    # TIMING
    start = timer()
    lines = list(process.stdout)
    end = timer()
    print('Time taken:', end - start) # 53.662078000000065 seconds -> Linux

    for _ in tqdm(iterable=lines, total=len(lines)):
        sleep(0.1)

if process.returncode != 0:
    raise CalledProcessError(returncode=process.returncode, cmd=process.args)
Run Code Online (Sandbox Code Playgroud)

list(process.stdout)在 WSL Linux 环境中运行时,处理时间似乎需要 53 秒。然而,当我在Windows环境中运行它时,只需要0.6秒。我觉得很奇怪为什么时间安排如此不同。

我尝试使用subprocess.run()andsubprocess.check_output()代替,但它们在处理循环之前仍然会导致相同的长时间滞后tqdm()

我在这里错过了什么吗?我尝试查看文档以了解 Windows 与 WSL Linux 环境中使用的差异subprocess.Popen(),但我仍然不确定问题是什么。也许list(process.stdout)这里是不必要的,并且有更好的方法来存储来自标准输出的行。

任何形式的指导都会非常有帮助。

python subprocess windows-subsystem-for-linux

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

如何在 Python 中迭代字符串列表并连接属于标签的字符串?

当迭代 Python 3 中的元素列表时,如何“隔离”感兴趣的元素之间的内容?

我有一个清单:

list = ["<h1> question 1", "question 1 content", "question 1 more content", "<h1> answer 1", "answer 1 content", "answer 1 more content", "<h1> question 2", "question 2 content", "<h> answer 2", "answer 2 content"]
Run Code Online (Sandbox Code Playgroud)

在此列表中,有些元素带有标签 < h >,而其他元素则没有。这个想法是,具有此标签的元素是“标题”,并且直到下一个标签的后续元素是其内容。

如何将属于 header 的列表元素连接起来以获得两个大小相等的列表:

headers = ["<h1> question 1", "<h1> answer 1", "<h1> question 2", "<h> answer 2"]
content = ["question 1 content question 1 more content", "answer 1 content answer 1 more content", "question 2 content", "answer …
Run Code Online (Sandbox Code Playgroud)

python string list python-3.x

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

附加到元组列表

我有一个元组列表,看起来像这样:

    my_list = [(["$"], 1.5)]
Run Code Online (Sandbox Code Playgroud)

我还将这些贵重物品存储为变量:

    val1 = "#"
    val2 = 3.0
Run Code Online (Sandbox Code Playgroud)

我希望能够将val1追加到元组中的列表中,并将val2与元组中的第二个元素相乘.它应该如下所示:

    [(["$", "#"], 4.5)]
Run Code Online (Sandbox Code Playgroud)

到目前为止我有这个:

    for item in my_list:
        for i in item:
            i[0].append(val1)
            i[1] = i[1] * val2
Run Code Online (Sandbox Code Playgroud)

但到目前为止,这是行不通的,有没有另一种方法可以做到这一点?

python

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

python itertools groupby 与过滤器的使用

我有一个列表 = [1, 2, 3, 3, 6, 8, 8, 10, 2, 5, 7, 7] 我正在尝试使用 groupby 将其转换为

1
2
3
3
6
8,8
10
2,
5
7,7
Run Code Online (Sandbox Code Playgroud)

基本上,任何大于 6 的东西,我喜欢将它们分组,否则我想让它们不分组。有关如何使用 itertool groupby 执行此操作的任何提示

我目前的代码:

for key, group in it.groupby(numbers, lambda x: x):
   f = list(group)
   if len(f) == 1:
      split_list.append(group[0])
   else:
      if (f[0] > 6):  #filter condition x>6
         for num in f: 
            split_list.append(num + 100)
       else:
         for num in f:
            split_list.append(num)
Run Code Online (Sandbox Code Playgroud)

python group-by list python-itertools

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

在列表python中找到最长的单词

我试图在非空列表中找到最长的单词.我的函数应该返回最长的单词.如果列表中的元素长度相等,我试图在Unicode排序方面排序最长.例如,我试图返回以下内容:

    >>> highest_word(['a', 'cat', 'sat'])
    'sat'
    >>> highest_word(['saturation', 'of', 'colour'])
    'saturation'
    >>> highest_word(['samIam'])
    'samIam'
Run Code Online (Sandbox Code Playgroud)

到目前为止,我可以得到第一个工作,这是我的代码到目前为止:

    def highest_word(wordlist):
    longestWord = ""
    max_len = 0

    for word in wordlist:

        if len(word) > max_len:
            longestWord = len(word)
            longestWord = word
    return longestWord
Run Code Online (Sandbox Code Playgroud)

任何形式的帮助将不胜感激.

python python-3.x

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

powershell Convertfrom-json 选择对象差异

考虑以下 json 负载(只是“az group list”中的示例片段):

[

    {
        "id": "/subscriptions/1f512sf9-112c-4a7a-a580-665afe4761f4/resourceGroups/dev-rg",
        "location": "northeurope",
        "managedBy": null,
        "name": "dev-rg",
        "properties": {
          "provisioningState": "Succeeded"
        },
        "tags": {
          "Application": "Integrations",
          "Department": "Development Team",
          "DeployedDate": "09/01/2020",
          "Environment": "DEV",
          "FundedBy": "INT",
          "InterfaceId": "IFUS_007.1",
          "Project": "INT"
        },
        "type": "Microsoft.Resources/resourceGroups"
      }
    ]
Run Code Online (Sandbox Code Playgroud)

有人可以解释为什么下面的方法有效吗

PS C:\Windows\system32> az group list | convertfrom-json | select-object @{n='RSG';e={$_.name}}

RSG
---
{dev-rg}
Run Code Online (Sandbox Code Playgroud)

以及为什么下面没有(返回空白)

PS C:\Windows\system32> az group list | convertfrom-json | select-object name

name
----
Run Code Online (Sandbox Code Playgroud)

powershell azure-cli

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

Python:列表数组中的唯一值

图像此输出来自fuzzywuzzy(值可能在另一个序列中):

[('car', 100, 28),
 ('tree', 80, 5),
 ('house', 44, 12),
 ('house', 44, 25),
 ('house', 44, 27)]
Run Code Online (Sandbox Code Playgroud)

我想把这三者一视同仁houses。只有唯一的字符串值才能得到这个结果的有效方法是什么:

编辑:由于所有houses值都相同44,我不在乎列表中的哪一个。最后一个house值无关紧要)

[('car', 100, 28),
 ('tree', 80, 5),
 ('house', 44, 12)]
Run Code Online (Sandbox Code Playgroud)

我在这里看到了很多关于列表唯一性的问题,但答案对我的示例不起作用,主要是因为作者只需要一个列表的解决方案。

我试过这个:

unique = []
for element in domain1:
    if element[0] not in unique:
        unique.append(element)
Run Code Online (Sandbox Code Playgroud)

我想我可以使用第一个值来处理element[0]并检查它们是否存在于unique. 如果我打印,unique我的结果与 after 相同fuzzywuzzy。似乎我的想法没有走在正确的道路上,那么我怎样才能达到我想要的结果呢?

谢谢!

python tuples list

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

排序卡片列表

我有两个清单:

card_candidates = ['9D', '9S', '3S', '0D']
card_order = ['2', '3', '4', '5', '6', '7', '8', '9', '0', 'J', 'Q', 'K', 'A']
Run Code Online (Sandbox Code Playgroud)

我希望能够根据第二个列表顺序对第一个列表进行排序.所以排序后的card_candidates应如下所示:

sorted_candidates = ['3S', '9D', '9S', '0D']
Run Code Online (Sandbox Code Playgroud)

'0'只是10的值,只是想让所有卡的长度相同.如果存在平局,例如"9D"和"9S",则需要对字母进行排序.到目前为止我刚刚做到了这一点:

sorted_candidates = []
for x, y in zip(card_candidates, card_order):
    sorted_candidates.append([x[0] for x in card_candidates])
return sorted(sorted_candidates)
Run Code Online (Sandbox Code Playgroud)

我知道这甚至不是正确的,我只是不知道该怎么做.

python sorting

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

尝试使用 python 访问共享点列表

我可以完全访问我想连接到 Microsoft.sharepoint.c0m 站点列表的资源。

我想知道从 python 中的共享点列表中提取或上传数据的最简单方法是什么?我收到此代码的错误代码 403:

import requests
from requests_ntlm import HttpNtlmAuth
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
auth = HttpNtlmAuth('username', 'password')

r = requests.get("https://sharepoint.com/_api/Web/lists/GetByTitle('ListName')/items",
verify=False,
auth=auth,
)
Run Code Online (Sandbox Code Playgroud)

为什么我收到错误代码 403 ?

python authentication sharepoint python-requests sharepoint-online

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