“set”对象没有属性“rstrip”

2024-10-03 23:23:27 发布

您现在位置:Python中文网/ 问答频道 /正文

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('{ }'))

回溯:

^{pr2}$

如果我做print (asalortak),它会变成这样:[{'83', '97', '53', '79', '89', '67', '61', '73', '59' }]

{do>看起来像这样: {'83', '97', '53', '79', '89', '67', '61', '73', '59', '71'},但我需要分开我的号码。在


Tags: pathifdefminmaxintprintset
1条回答
网友
1楼 · 发布于 2024-10-03 23:23:27

I do print (asalortak [0]), it looks like this: {'83', '97', '53', '79', '89', '67', '61', '73', '59', '71'}

这是因为asalortak [0]是一个python set,这个错误已经证实了这一点。在

print((ortakasal[0]).rstrip('{ }'))

AttributeError:“set”对象没有属性“rstrip”

这些元素已经被“分开”。只需迭代元素以打印它们:

^{pr2}$

set中,顺序不稳定/不固定。使用sorted从中生成排序列表(排序的alpha,不是数字,因为数字是字符串)

转换为列表:

lst = list(ortakasal[0])

转换为排序列表:

lst = sorted(ortakasal[0])

您可以使用random.sample(ortakasal[0],2)随机选择2个元素。在

相关问题 更多 >