from random import randint
import os
path = os.path.abspath('')
dosya = open((path + "\\asaldb.txt"), "r")
asalsayilar = dosya.readlines()
mindeger = int(input("Minumum Değer Kaç ? "))
maxdeger = int(input("Maximum Değer Kaç ? "))
min=0
max=0
minasallar = []
maxasallar = []
ortakasal = []
ortakasal2 = []
def maxasal():
global max
if maxdeger>=int(asalsayilar[max]):
maxasallar.append((asalsayilar[max]).rstrip('\n'))
max=max+1
maxasal()
def minasal():
global min
if int(asalsayilar[min])>=mindeger:
try:
(asalsayilar[max]).rstrip('\n')
minasallar.append((asalsayilar[min]).rstrip('\n'))
min=min+1
minasal()
except:
print("Maximum Asallar :")"""
maxasal()
else:
min=min+1
minasal()
minasal()
def common_member(a, b):
a_set = set(a)
b_set = set(b)
if (a_set & b_set):
ortakasal.append((a_set & b_set))
else:
print("No common elements")
common_member(minasallar, maxasallar)
ortakasal.sort()
print((ortakasal[0]).rstrip('{ }'))
回溯:
Traceback (most recent call last):
File "asal.py", line 54, in <module>
print((ortakasal[0]).rstrip('{ }'))
AttributeError: 'set' object has no attribute 'rstrip'
如果我print (asalortak)
,则会变成这样:[{'83', '97', '53', '79', '89', '67', '61', '73', '59' }]
如果我print (asalortak [0])
,它看起来像这样:
{'83', '97', '53', '79', '89', '67', '61', '73', '59', '71'}
,但我需要将我的数字分开。
答案 0 :(得分:0)
我打印(asalortak [0]),它看起来像这样:{'83','97','53','79','89','67','61','73' ,'59','71'}
那是因为asalortak [0]
是一个python set
,由此错误确认。
print((ortakasal[0]).rstrip('{ }'))
AttributeError:'set'对象没有属性'rstrip'
元素已经“分离”。只需迭代元素即可打印出来:
for e in ortakasal[0]:
print(e)
在set
中,订单不稳定/固定。使用sorted
生成一个排序列表(排序的alpha,而不是数字,因为数字是字符串)
转换为列表:
lst = list(ortakasal[0])
转换为已排序列表:
lst = sorted(ortakasal[0])
您可以使用random.sample(ortakasal[0],2)
随机选择2个元素。