我竭尽所能,但被困在最后,将列表转换为上面给出的有问题的字典,即enter code here
namespace App\Http\Controllers\Auth;
use Socialite;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\Models\User;
use App\Models\socialProvider;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
//use Laravel\Socialite\Facades\Socialite;
class RegisterController extends Controller
这是我如何解决上述问题的方法。
{1.3:[1.2,1.4]}
如何在最后将列表转换为字典?即{1.3:[1.2,1.4]}
答案 0 :(得分:1)
检查此
lst=dict() # create an empty dictionary
l=[1.3]
k=[1.2,1.4,1.5,1.6,2.3,3.4,3.6,3.8,4.2,5.4,5.8]
for x in range(len(l)):
lst[str(l[x])] = [] # create a new dict entry with a key from l and an empty list
for z in range(len(k)):
b=k[z]-l[x]
if b>0.1:
l.append(k[z])
elif b<=0.1:
print(lst[str(l[x])].append(k[z])) # add to the empty list inside the dictionary of corresponding entry in l that you are checking with
print(lst)
print(l)
{'1.3': [1.2, 1.4]}
[1.3, 1.5, 1.6, 2.3, 3.4, 3.6, 3.8, 4.2, 5.4, 5.8]
答案 1 :(得分:0)
我最终使用了fromkey()
方法,它解决了我的问题
new_dict=[]
compare_value = 0.1
list_main=[1.3]
test_set=[1.2,1.4,1.5,1.7,2.3,3.4,3.6,3.8,4.2,5.4,5.8]
for x in range(len(list_main)):
for z in range(len(test_set)):
diff=test_set[z]-list_main[x]
if diff>compare_value:
list_main.append(test_set[z])
elif diff<=compare_value:
new_dict.append(test_set[z])
dictionary=dict.fromkeys(list_main, new_dict)
print('dictionary----',dictionary)
print('list-------',list_main)