#π Hey I have a problem with multiple list.append
65 messages Β· Page 1 of 1 (latest)
@proud girder
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
WHat are user1 et al?
it is the username that should be assigned to the price
I don't see your 550 price anywhere.
that was an example
Can you describe where the usernames come from, and to which prices they should be associated?
they come from an api
An example not based on some example input doesn't explain things well.
alr wait a sec
[849, 850, 50, 880, 35, 460]
["'sgNARC'", "'bladi.kery'", "'Sorainen'", "'MadMuesli'", "'Varjump'", "'Deaths_Mistress'"]
849: sgNARC
850: bladi.kery
50: Sorainen
and so on
they are in order to each other rn
!d zip
zip(*iterables, strict=False)```
Iterate over several iterables in parallel, producing tuples with an item from each one.
Example:
```py
>>> for item in zip([1, 2, 3], ['sugar', 'spice', 'everything nice']):
... print(item)
...
(1, 'sugar')
(2, 'spice')
(3, 'everything nice')
```...
[849, 850, 50, 880, 35, 460]
["'sgNARC'", "'bladi.kery'", "'Sorainen'", "'MadMuesli'", "'Varjump'", "'Deaths_Mistress'"]
849: sgNARC
850: bladi.kery
50: Sorainen
print(list(zip(prices,names)))
alr so now I have this
(33, "'FungalToenail'")
(50, "'Sorainen'")
(880, "'MadMuesli'")
(35, "'Varjump'")
(460, "'Deaths_Mistress'")
but can I sort it?
you assign that print to a var instead and use var = var.sort()
acutally im wrong in many ways.. there tuples no? so,
tuplevar = sorted(tuplevar)
i think
or is it a list of tuples.
if you use sorted then you don't need to convert it to a list
so where whould all that be placed in here?
for item in zip(prices, names):
print(item)
i meant structure wise,
[(,),(,)]
zip returns an iterator of tuples yes
what do you want to sort it by?
the numbers
i think he meant asc or desc
asc
what if two numbers are identical?
that too, but he did answer my question
Would one name overwrite the other one?
No. Sorting keeps all the items, just shuffles them into the desired order. Zig's asking about details on that shuffling.
So eg [(850,"A"),(850,"B")] may or may not get rearranged.
well it would be
35, "name1"
35, "name2"
??
Are the names with the same number being arranged in alphabetical order ok?
Note that there are at least 3 reasonable responses to this question:
- for the same price, order by the user name
- for the same price, do not move the users around
- I don't care!
This is a policy decision for you, and you can say the order within a single price is unimportant.
yeah
... or maybe it is important. But if you haven't thought about this detail, probably it is, in fact, unimportant.
then what Py.noob said will work. Use the sorted function on the result of zip to get a sorted list of (price, name)
the first one is good
The sorted() function can accept a key= parameter which you can use to govern how things are sorted.
Lacking that, sorted uses the natural comparison of the objects, ascending. For a 2-tuple, the first elements are compared and if they are the same, the second elements are compared.
So you'll get your desired behaviour for free with no key= parameter in this case.
I dont understand where I supposed to put the sorted function in here
for item in zip(prices, names):
print(item)
wrap it around the zip
everything I do gives an error and does not print anything
nice
(35, "'Varjump'")
(35, "'ezolofw'")
(36, "'xMorg'")
(36, "'xristosmtr'")
(50, "'Sorainen'")
(460, "'Deaths_Mistress'")
(880, "'MadMuesli'")
thanks a lot everyone
(:
Note that the natural order of "adf" and "aJf" is "aJf","adf" because of the ordinal values of the characters (uppercase has lower values than lowercase).
Oh so the usernames are being sorted rn too
That was you spec, that for the same price the usernames should be sorted.
If you never have 2 usernames which differ only in their case (eg "fred" and "Fred") this won't matter.
Unless "Fred","alf" is the wrong order.
the sorting of the usernames really does not matter for me here
Aha! And there you were earlier saying that it did!
Anyway, if it doesn't matter, you're good.
oh my bad sorry
alr thanks again for solving the problem everyone, bye for now
!solved
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.