这是事情:
lst = [1, 2, 3]
i = [x if x == 2 else "I don't need that!" for x in lst]
print(i)
Run Code Online (Sandbox Code Playgroud)
输出:
["I don't need this item!", 2, "I don't need this item!"]
Run Code Online (Sandbox Code Playgroud)
正如您在输出中看到的,我有我不想拥有的第一个和最后一个项目。
我尝试了各种方法,例如删除else语句(这是不可能的),替换0为pass语句(它也不起作用)。
甚至有可能list在list理解时使用条件获取刚需要的项目吗?还是只有filter功能才有可能?
需要的输出:
[2]
Run Code Online (Sandbox Code Playgroud) python list-comprehension filter conditional-statements python-3.x
这里有 3 个列表:
\n\ntest1 = [\'a\',\'a\',\'a\',\'a\',\'a\']\ntest2 = [\'b\',\'b,\'b\']\ntest3 = [\'c\',\'c\',\'c\',\'c\',\'c\',\'c\',\'c\',\'c\']\nRun Code Online (Sandbox Code Playgroud)\n\n如何输出包含最多元素的列表?下面的解决方案来自@Rifat Alptekin \xc3\x87etin:Python:如何找到其中包含最多元素的列表,但这会输出由字典定义的“字符串”。如何输出包含最多元素的列表?
\n\nlists=[test1, test2, test3]\nlistnames=["list1", "list2", "list3"]\n\nmost = listnames[np.argmax([len(l) for l in lists])]\nRun Code Online (Sandbox Code Playgroud)\n\n电流输出
\n\nmost = \'list3\'\nRun Code Online (Sandbox Code Playgroud)\n\n所需输出
\n\nmost = test3\nRun Code Online (Sandbox Code Playgroud)\n 给定文件名列表,我们希望将所有扩展名为 hpp 的文件重命名为扩展名 h。为此,我们希望生成一个名为 newfilenames 的新列表,其中包含新文件名。使用您迄今为止学到的任何方法填充代码中的空白,例如 for 循环或列表推导式。
filenames = ["program.c", "stdio.hpp", "sample.hpp", "a.out", "math.hpp", "hpp.out"]
# Generate newfilenames as a list containing the new filenames
# using as many lines of code as your chosen method requires.
___
print(newfilenames)
# Should be ["program.c", "stdio.h", "sample.h", "a.out", "math.h", "hpp.out"]
Run Code Online (Sandbox Code Playgroud)
我只知道如何找到带有 .hpp 扩展名的名称,但我不知道如何用 .h 替换它
newfilenames=[x for x in filenames if x.endswith('.hpp')]
Run Code Online (Sandbox Code Playgroud) 我拥有的:
a=[{'name':'a','vals':1,'required':'yes'},{'name':'b','vals':2},{'name':'d','vals':3}]
b=[{'name':'a','type':'car'},{'name':'b','type':'bike'},{'name':'c','type':'van'}]
Run Code Online (Sandbox Code Playgroud)
我试过的:
[[i]+[j] for i in b for j in a if i['name']==j['name']]
Run Code Online (Sandbox Code Playgroud)
我得到了什么:
[[{'name': 'a', 'type': 'car'}, {'name': 'a', 'vals': 1}], [{'name': 'b', 'type': 'bike'}, {'name': 'b', 'vals': 2}]]
Run Code Online (Sandbox Code Playgroud)
我想要的是:
[{'name': 'a', 'type': 'car','vals': 1},{'name': 'b', 'type': 'bike','vals': 2}]
Run Code Online (Sandbox Code Playgroud)
笔记:
我正在编写这个函数,首先我使用的是循环。循环需要时间,因为我尝试了列表理解。它没有用。此功能允许 10 秒。请检查一下。该函数返回公司的排序列表,其中公司的比率为 5% 或更高。
def mostActive(customers):
# Write your code here
tot = len(customers)
set_cust = set(customers)
cust_dict = {i: customers.count(i)/tot for i in set_cust}
cus_list = [i for i in list(cust_dict) if cust_dict[i] >= 0.05]
return sorted(cus_list)
Run Code Online (Sandbox Code Playgroud)
Omega
Alpha
Omega
Alpha
Omega
Alpha
Omega
Alpha
Omega
Alpha
Omega
Alpha
Omega
Alpha
Omega
Alpha
Omega
Alpha
Omega
Beta
Run Code Online (Sandbox Code Playgroud)
预期输出:
Alpha
Beta
Omega
Run Code Online (Sandbox Code Playgroud) 对于这些列表:
a=['applepearstrawberry','applepearbanana','orangekiwiapple','xxxxxxxxxxx']
b=['apple','pear']
Run Code Online (Sandbox Code Playgroud)
如何返回包含“apple”或“pear”的任何元素的列表。
期望的输出
c=['applepearstrawberry','applepearbanana','orangekiwiapple']
Run Code Online (Sandbox Code Playgroud)
尝试的解决方案:
c=[]
for i in a:
if b[0] or b[1] in a:
c.append(i)
print(c)
Run Code Online (Sandbox Code Playgroud)
谢谢
返回第 N 个偶数(想象一个偶数列表,返回输入数字在人眼位置的数字)
示例(输入 --> 输出)
1 --> 0 (the first even number is 0)
3 --> 4 (the 3rd even number is 4 (0, 2, 4))
Run Code Online (Sandbox Code Playgroud)
(nth_even(3), --> 4)
(nth_even(1), --> 0)
(nth_even(2), --> 2)
(nth_even(100), --> 198)
(nth_even(1298734), --> 2597466)
Run Code Online (Sandbox Code Playgroud)
我的代码在处理更大的数字时超时,所以我需要一个更快的方法。
def nth_even(n):
data = [num for num in range(0, n*2) if num % 2 == 0]
return data[n-1]
Run Code Online (Sandbox Code Playgroud) 我对 Python 还很陌生,但我越来越痴迷于尽可能缩短我的代码,但我不知道如何用我的这段代码来做到这一点。是否可以使用列表理解来缩短以下代码?另外,我如何编辑我的代码,以便空格和缩进在堆栈溢出时正确显示?
user_input = input("Please guess a number: ")
correct_number_list = [4,5,7,1]
if user_input[0] in correct_number_list and user_input[1] in correct_number_list\
and user_input[2] in correct_number_list and user_input[3] in correct_number_list :
in_range = True
else:
in_range = False
Run Code Online (Sandbox Code Playgroud)
这只是代码的一小部分,但它伤害了我的眼睛,因为我知道必须有更好的方法来编写它。在我的原始代码中,我随机生成 4 个随机数并将它们存储在列表中并让用户猜测这些数字。任何有关如何缩短此代码的帮助或建议,即使它与列表理解无关,也会很棒。先感谢您
我有以下列表:
lst = [[1,3], [3,4], [2,7], [6,5]]
Run Code Online (Sandbox Code Playgroud)
如何创建“oneliner”来获取每个“列”的 max() 列表?
因此,对于上面的示例,结果列表应如下所示:[6,7],其中 6 是“第 0 列”的最大值,7 是“第 1 列”的最大值。
rdict = {}
f = open(filename)
for line in f:
d = line.split()
name = d[0]
rating = int(d[2])
if name in rdict:
pass
else:
zlist = []
for i in range[150]:
rdict[name] = #a list of ratings
Run Code Online (Sandbox Code Playgroud)
我想将值(从这个以外的数据!)添加到范围150的rdict.我将rdict设置为什么?