Python:每日一题 126
本帖最后由 冬雪雪冬 于 2017-11-26 20:15 编辑先我们的玩法做了一下改变:
1. 楼主不再提供答案。
2. 请大家先独立思考”,再参考其他鱼油的解答,这样才有助于自己编程水平的提高。
3. 鼓励大家积极答题,奖励的期限为出题后24小时内。
4. 根据答案的质量给予1~3鱼币的奖励。
题目:
已有一个没有重复的整数列表,如 ,有一个整数目标值,如9
这个目标值可能可以由列表中两个数字相加得到,如 2 + 7,-3 + 12, 4 +5
建立一个函数
def fun(mylist, target):
return 列表
要求返回的列表为如下形式:
[, [-3, 12], ]
如果仅有一个返回
如果没有返回
[]
注:不要有重复的,如和
本帖最后由 SixPy 于 2017-11-20 20:28 编辑
def fun(ls, target):
return [,ls]for x in range(len(ls))
for y in range(x+1,len(ls))
if ls+ls==target]
ls =
print(fun(ls, 9))
#[, [-3, 12], ]
import numpy as np
a = np.array()
idx = np.where(np.triu(a+a.reshape(-1,1),1)==9)
print(np.array(],a]]).T.tolist()) 本帖最后由 第四时空 于 2017-11-20 20:59 编辑
def fun(mylist, target):
mylist = sorted(mylist)
result = []
for i in range(0, len(mylist)):
if mylist > target / 2:
break
for j in range(i + 1, len(mylist)):
if mylist + mylist > target:
break
if mylist + mylist == target:
result.append(, mylist])
return result if len(result) == 1 else result 两两组合,没有重复的列表···故意的嘛{:10_250:}
import itertools as it
def fun(mylist, target):
return
手动做的话,嵌套循环是笛卡尔积,
要排除掉自己加自己,还有位置相反的重复
def fun2(mylist, target):
return [list(k) for k in
{frozenset() for i in mylist for j in mylist if i!=j and i+j==target}] # 使用itertools库
def fun(mylist, target):
from itertools import combinations
result =
if len(result) == 1:
return result
return result
# 不使用itertools库
def fun(mylist, target):
result = []
for i in mylist:
if (target - i) in mylist and mylist.index(i)<mylist.index(target-i):
result.append()
if len(result) == 1:
return result
return result >>> a=[]
>>> for i in range(len(l)):
for j in range(i,len(l)):
if (l+l==9):
a.append(,l])
>>> a
[, [-3, 12], ]
list1=[]
def fun(mylist,target):
for i in range(0,len(mylist)):
for j in range (i+1,len(mylist)):
if mylist + mylist ==target:
list1.append(,mylist])
return list1
mark
def fun(mylist,target):
result = []
for i in range(len(mylist)):
for each in mylist:
if target == mylist + each:
temp = , each]
result.append(temp)
#mylist.remove(each)
return result def fun(ls, target):
result = []
for i in range(len(ls)-1):
for j in range(i+1, len(ls)):
if ls + ls == target:
result.append(, ls])
if len(result) == 1:
return result
else:
return result
list1 =
print(fun(list1, 9))
[, [-3, 12], ] def fun(mylist,target):
length = len(mylist)
newlist = []
for i in range(length):
for j in range(length):
if target == mylist + mylist:
newlist.append(,mylist])
mylist = True
return newlist
mylist=
target = 9
print(fun(mylist,target))
def fun(mylist,target):
returnedlist = []
for each in mylist:
for each1 in mylist:
if each+each1 == target:
returnedlist.append()
return returnedlist 本帖最后由 口可口可 于 2017-11-27 22:10 编辑
def fun(mylist, target):
result_list = []
mylist.sort()
for index1 in range(len(mylist)-1):
for index2 in range(index1+1, len(mylist)):
if mylist + mylist >= target:
if mylist + mylist == target:
result_list.append(, mylist])
else:
break
if len(result_list) == 1:
return result_list
else:
return result_list def fun(a,target):
answer=[]
for i in range(len(a)):
for j in range(i):
if i !=j and a+a== target :
answer.append(,a])
return print(answer)
fun(,9) def fun (mylist,target):
un=0
nilist=[]
while un < len(mylist):
for i in range(len(mylist)):
if i > un:
a=int(mylist)
c=a+int(mylist)
if c == target:
element=]
nilist.append(element)
un += 1
return nilist 本帖最后由 yjsx86 于 2017-11-24 03:12 编辑
L =
def func(mylist, target):
newlist = mylist[:]
rs = []
for i in mylist:
for j in newlist:
if i + j == target:
rs.append()
newlist.remove(i)
return rs if len(rs) == 1 else rs
#测试代码
for x in range(10):
r = func(L,x)
print(r) 我还是个初学者,这个有点难 l1 =
l2 = []
def func(mylist,target):
while True:
num1 = mylist.pop()
for i in mylist:
if num1 + i == target:
l2.append()
if not(len(mylist)):
break
print(l2)
func(l1,23) 貌似参考不到其他人的答案呢{:5_107:} 本帖最后由 sunboyabc 于 2017-11-22 16:12 编辑
def function1(list1=,target=9):
len1 = len(list1)
for i1 in range(len1):
for i2 in range(i1+1,len1):
if list1 + list1 ==9 :
print(,list1])
else:
print([])